gpt4 book ai didi

flutter - 如何为列表中的容器设置动画

转载 作者:IT王子 更新时间:2023-10-29 06:37:59 25 4
gpt4 key购买 nike

我有一个容器列表,我想为容器设置动画。

比如改变他的高/宽,改变容器的内容。

到目前为止我所做的是:

containerHeight =  MediaQuery.of(context).size.height * 0.20;
body: Container(
child: ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(15.0),
child: Container(
color: Colors.black,
height: containerHeight,
child: IconButton(
icon: Icon(Icons.list),
color: Colors.white,
onPressed: () {
setState(() {
containerHeight = MediaQuery.of(context).size.height * 0.35;
});
},
),
),
);
},
),

我打算使用 AnimatedContainer,但即使在使用它之前,如果我按下按钮也没有任何反应

最佳答案

这就是我让它工作的方式。我只是用动画容器替换了容器

class _HomePageState extends State<HomePage> {
List<String> dataList = ['Andrew', 'Test', 'Data', 'Random'];
double containerHeight;
bool expanded = false;

@override
Widget build(BuildContext context) {
containerHeight = expanded
? MediaQuery.of(context).size.height * 0.20
: MediaQuery.of(context).size.height * 0.30;
return Scaffold(
appBar: AppBar(
title: Text('Test App'),
),
body: ListView.builder(
itemCount: dataList.length,
itemBuilder: (context, index) {
return AnimatedContainer(
curve: Curves.easeOut,
duration: Duration(milliseconds: 400),
color: Colors.red,
height: containerHeight,
margin: EdgeInsets.all(8),
child: Center(
child: Text(
dataList.elementAt(index),
style: TextStyle(
color: Colors.white,
),
),
),
);
},
),
floatingActionButton: FloatingActionButton(onPressed: () {
setState(() {
expanded = !expanded;
});
}),
);
}
}

输出

enter image description here

编辑

如果您只想折叠您正在点击的元素,最好创建一个可以保存容器当前状态的类。

class DataModel {
String title;
bool expanded;

DataModel(this.title) {
expanded = false;
}
}

在这里,expanded 将跟踪容器应该是大还是小。

现在稍微重构一下之前的代码

class _HomePageState extends State<HomePage> {


// Initialize DATA MODEL list with some random values.
List<DataModel> dataList = ['Andrew', 'Test', 'Data', 'Random']
.map<DataModel>((s) => DataModel(s))
.toList();

double containerHeight;

@override
Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(
title: Text('Test App'),
),
body: ListView.builder(
itemCount: dataList.length,
itemBuilder: (context, index) {

DataModel item = dataList.elementAt(index);

// Check if the item is expanded or not and set size accordingly
containerHeight = item.expanded
? MediaQuery.of(context).size.height * 0.20
: MediaQuery.of(context).size.height * 0.30;

return GestureDetector(
onTap: (){
// On tap reverse the expanded state of the data which will resize
// the widget as setState is being called
setState(() {
item.expanded = !item.expanded;
});
},
child: AnimatedContainer(
curve: Curves.easeOut,
duration: Duration(milliseconds: 400),
color: Colors.red,
height: containerHeight,
margin: EdgeInsets.all(8),
child: Center(
child: Text(
dataList.elementAt(index).title,
style: TextStyle(
color: Colors.white,
),
),
),
),
);
},
),
);
}
}

关于flutter - 如何为列表中的容器设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56564095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com