Flutter 如何在flatter中正确地将一个小部件堆叠到另一个小部件上?
我想要实现的是当按下按钮时,用平滑的动画将一个小部件堆叠到另一个小部件上。下面是我试图实现的目标的屏幕截图(从左到右)。谢谢你的帮助Flutter 如何在flatter中正确地将一个小部件堆叠到另一个小部件上?,flutter,dart,animation,Flutter,Dart,Animation,我想要实现的是当按下按钮时,用平滑的动画将一个小部件堆叠到另一个小部件上。下面是我试图实现的目标的屏幕截图(从左到右)。谢谢你的帮助 您可以使用Stack小部件 访问了解更多信息您可以使用堆栈小部件 访问了解更多信息您可以使用卡片、动画定位和其他一些小部件尝试以下操作 import 'dart:developer'; import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp
您可以使用
Stack
小部件
访问了解更多信息您可以使用
堆栈
小部件
访问了解更多信息您可以使用卡片、动画定位和其他一些小部件尝试以下操作
import 'dart:developer';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stack Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Stack Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool showed = false;
double incrementBoxHeight = 50;
void incrementCounter() {
setState(() {
_counter++;
});
}
void decrementCounter() {
setState(() {
if (_counter > 0) {
_counter--;
}
});
}
void togleDetail() {
setState(() {
showed = !showed;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Card(
child: Stack(
children: [
Image(image: AssetImage('asset/img/indice.jpeg')),
AnimatedPositioned(
duration: Duration(seconds: 1),
bottom: showed ? incrementBoxHeight : 0,
right: 0,
left: 0,
child: GestureDetector(
onTap: togleDetail,
child: Container(
height: 50, //you can do it with mediaQuery
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 5.0,
spreadRadius: 2.0,
),
],
color: Colors.blueAccent,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
child: Row(
children: [
SizedBox(
width: 10,
),
Column(
children: [
Text(
"Topt",
style: TextStyle(fontSize: 20),
),
Text(
"8 TMT",
style: TextStyle(fontSize: 20, color: Colors.red),
)
],
),
],
),
),
),
),
Visibility(
visible: showed,
child: Positioned(
bottom: 0,
right: 0,
left: 0,
child: Container(
height: incrementBoxHeight, //you can do it with mediaQuery
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 5.0,
spreadRadius: 2.0,
),
],
color: Colors.blueAccent,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
GestureDetector(
onTap: decrementCounter,
child: CircleAvatar(
child: Icon(
Icons.remove,
size: 30,
),
),
),
Text(
_counter.toString(),
style: TextStyle(fontSize: 48),
),
GestureDetector(
onTap: incrementCounter,
child: CircleAvatar(
child: Icon(
Icons.add,
size: 30,
)),
)
],
),
),
),
),
],
)),
),
);
}
}
导入“dart:developer”;
进口“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“堆栈演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“堆栈演示”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
bool=false;
双增量BoxHeight=50;
void incrementCounter(){
设置状态(){
_计数器++;
});
}
无效递减计数器(){
设置状态(){
如果(_计数器>0){
_计数器--;
}
});
}
void togleDetail(){
设置状态(){
显示=!显示;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
孩子:卡片(
子:堆栈(
儿童:[
图像(图像:AssetImage('asset/img/indice.jpeg')),
动画定位(
持续时间:持续时间(秒数:1),
底部:显示?递增框高度:0,
右:0,,
左:0,,
儿童:手势检测器(
onTap:togleDetail,
子:容器(
高度:50,//您可以使用mediaQuery进行此操作
装饰:盒子装饰(
boxShadow:[
箱形阴影(
颜色:颜色,黑色,
半径:5.0,
扩展半径:2.0,
),
],
颜色:Colors.blueAccent,
borderRadius:仅限borderRadius(
左上:半径。圆形(10),
右上角:半径。圆形(10),
),
),
孩子:排(
儿童:[
大小盒子(
宽度:10,
),
纵队(
儿童:[
正文(
“Topt”,
样式:TextStyle(字体大小:20),
),
正文(
“8 TMT”,
样式:TextStyle(字体大小:20,颜色:Colors.red),
)
],
),
],
),
),
),
),
可见度(
可见:显示,
孩子:定位(
底部:0,
右:0,,
左:0,,
子:容器(
height:incrementBoxHeight,//您可以使用mediaQuery执行此操作
装饰:盒子装饰(
boxShadow:[
箱形阴影(
颜色:颜色,黑色,
半径:5.0,
扩展半径:2.0,
),
],
颜色:Colors.blueAccent,
borderRadius:仅限borderRadius(
左上:半径。圆形(10),
右上角:半径。圆形(10),
),
),
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceAround,
儿童:[
手势检测器(
onTap:递减计数器,
孩子:圆环星(
子:图标(
图标。删除,
尺码:30,
),
),
),
正文(
_counter.toString(),
样式:TextStyle(字体大小:48),
),
手势检测器(
onTap:递增计数器,
孩子:圆环星(
子:图标(
Icons.add,
尺码:30,
)),
)
],
),
),
),
),
],
)),
),
);
}
}
这里是结果,你可以编辑来实现你的ui,因为这只是你界面的一个一般结构
您可以使用卡片、动画定位和其他一些小部件尝试以下操作
import 'dart:developer';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stack Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Stack Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool showed = false;
double incrementBoxHeight = 50;
void incrementCounter() {
setState(() {
_counter++;
});
}
void decrementCounter() {
setState(() {
if (_counter > 0) {
_counter--;
}
});
}
void togleDetail() {
setState(() {
showed = !showed;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Card(
child: Stack(
children: [
Image(image: AssetImage('asset/img/indice.jpeg')),
AnimatedPositioned(
duration: Duration(seconds: 1),
bottom: showed ? incrementBoxHeight : 0,
right: 0,
left: 0,
child: GestureDetector(
onTap: togleDetail,
child: Container(
height: 50, //you can do it with mediaQuery
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 5.0,
spreadRadius: 2.0,
),
],
color: Colors.blueAccent,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
child: Row(
children: [
SizedBox(
width: 10,
),
Column(
children: [
Text(
"Topt",
style: TextStyle(fontSize: 20),
),
Text(
"8 TMT",
style: TextStyle(fontSize: 20, color: Colors.red),
)
],
),
],
),
),
),
),
Visibility(
visible: showed,
child: Positioned(
bottom: 0,
right: 0,
left: 0,
child: Container(
height: incrementBoxHeight, //you can do it with mediaQuery
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 5.0,
spreadRadius: 2.0,
),
],
color: Colors.blueAccent,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
GestureDetector(
onTap: decrementCounter,
child: CircleAvatar(
child: Icon(
Icons.remove,
size: 30,
),
),
),
Text(
_counter.toString(),
style: TextStyle(fontSize: 48),
),
GestureDetector(
onTap: incrementCounter,
child: CircleAvatar(
child: Icon(
Icons.add,
size: 30,
)),
)
],
),
),
),
),
],
)),
),
);
}
}
导入“dart:developer”;
进口“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“堆栈演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:我的主页(