Flutter:设置AnimatedList动画时长
Flutter: set AnimatedList animation duration
有没有办法在 AnimatedList 中设置动画的持续时间?
AnimatedList(
key: _animList,
initialItemCount: _myList.length,
itemBuilder: (context, index, animation) {
// duration = Duration(seconds: 1); <--- ????????
return SlideTransition(
position: animation.drive(
Tween(begin: Offset(1, 0), end: Offset(0, 0))
.chain(CurveTween(curve: Curves.bounceIn))),
child: Container(color: Colors.red, height: 100, width: 100));
});
所以......经过短暂的研究,我意识到你只能设置 Duration 当你想插入列表或从列表中删除时,这是通过创建 GlobalKey 的 AnimatedListState...
我写了一个插入的示例代码
class Pool extends StatelessWidget {
final keys = GlobalKey<AnimatedListState>();
var list = List.generate(3, (i) => "Hello $i");
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: AnimatedList(
key: keys,
initialItemCount: list.length,
itemBuilder: (context, index, animation) {
return SlideTransition(
position: animation.drive(
Tween<Offset>(begin: Offset(1, 0), end: Offset(0, 0))
.chain(CurveTween(curve: Curves.ease))),
child: ListTile(
title: Text(list[index]),
),
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
list.insert(0, "NothingYay");
keys.currentState.insertItem(0, duration: Duration(seconds: 2));
},
),
);
}
}
输出:
有没有办法在 AnimatedList 中设置动画的持续时间?
AnimatedList(
key: _animList,
initialItemCount: _myList.length,
itemBuilder: (context, index, animation) {
// duration = Duration(seconds: 1); <--- ????????
return SlideTransition(
position: animation.drive(
Tween(begin: Offset(1, 0), end: Offset(0, 0))
.chain(CurveTween(curve: Curves.bounceIn))),
child: Container(color: Colors.red, height: 100, width: 100));
});
所以......经过短暂的研究,我意识到你只能设置 Duration 当你想插入列表或从列表中删除时,这是通过创建 GlobalKey 的 AnimatedListState...
我写了一个插入的示例代码
class Pool extends StatelessWidget {
final keys = GlobalKey<AnimatedListState>();
var list = List.generate(3, (i) => "Hello $i");
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: AnimatedList(
key: keys,
initialItemCount: list.length,
itemBuilder: (context, index, animation) {
return SlideTransition(
position: animation.drive(
Tween<Offset>(begin: Offset(1, 0), end: Offset(0, 0))
.chain(CurveTween(curve: Curves.ease))),
child: ListTile(
title: Text(list[index]),
),
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
list.insert(0, "NothingYay");
keys.currentState.insertItem(0, duration: Duration(seconds: 2));
},
),
);
}
}
输出: