flutter - 如何在 Flutter 中使用 AnimatedList Widget
问题描述
我正在尝试实现一个动画列表,
但我对动画并不熟悉。
我应该插入的内容
位置:animation.drive(),
和
removeItem(_index,(context,animation)=> /// 我应该在这里做什么 ),);
这是代码(只是“启动应用程序”的一些更改)
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
List<int> _list = [];
final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
void _addItem() {
final int _index = _list.length;
_list.insert(_index,_index);
_listKey.currentState.insertItem(_index);
}
void _removeItem() {
final int _index = _list.length-1;
_listKey.currentState.removeItem(_index,(context,animation)=> /// what I'm supposed to do here
),);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: AnimatedList(
key: _listKey,
initialItemCount: 0,
itemBuilder: (BuildContext context, int index, Animation animation) {
return SlideTransition(
position: animation.drive(
/// what I'm supposed to do here
),
child: Card(child: Text(_list[index].toString()),));
},),
),
floatingActionButton: Column(children: <Widget>[
FloatingActionButton(
onPressed:()=> _addItem(),
tooltip: 'Increment',
child: Icon(Icons.add),),
FloatingActionButton(
onPressed: ()=>_removeItem(),
tooltip: 'Decrement',
child: Icon(Icons.remove),),
],),
);
}
}
寻找有关 SliderTransition 的教程我看到了这个:
SlideTransition(
position: Tween<Offset>(
begin: const Offset(-1, 0),
end: Offset.zero,
).animate(animation),
虽然我有这个:
SlideTransition(
position: animation.drive(
// what i supposed to go here ??
),
有人可以帮忙吗?
是只有这个缺失的部分还是我错过了其他东西?
先感谢您
[编辑:AnimatedList 页面 显示消息
此页面已弃用,其内容可能已过时。
事实上似乎根本没有使用这个小部件]
解决方案
我在这里找到了我的问题的答案
你可以找到我的代码--> 这里 <--
及以下
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
List<int> _list = [];
final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
void _addItem() {
final int _index = _list.length;
_list.insert(_index,_index);
_listKey.currentState.insertItem(_index);
}
void _removeItem() {
final int _index = _list.length-1;
_listKey.currentState.removeItem(_index,(context,animation)=> Container()); /// what I'm supposed to do here
_list.removeAt(_index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: AnimatedList(
key: _listKey,
initialItemCount: 0,
itemBuilder: (BuildContext context, int index, Animation animation) {
return _buildItem(_list[index].toString(),animation);
},),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed:()=> _addItem(),
tooltip: 'Increment',
child: Icon(Icons.add),),
FloatingActionButton(
onPressed: ()=>_removeItem(),
tooltip: 'Decrement',
child: Icon(Icons.remove),),
],),
],),
);
}
Widget _buildItem(String _item, Animation _animation) {
return SizeTransition(
sizeFactor: _animation,
child: Card(
child: ListTile(
title: Text(
_item,
),
),
),
);
}
}
推荐阅读
- android - 如何使数据类变量可选?(Android Studio - Kotlin)
- php - preg_split 在正则表达式行开始
- django - django.db.utils.DataError:(1406,“第 1 行的“条形码”列的数据太长”)
- php - 先提取
- 来自帖子内容的标签条目?
- r - 将作为函数输出的列表子集,而不将输出声明为变量
- c# - 如何在 PHP 文件中接收 json 数据?
- angular - 错误:运行时编译器未加载角度 SSR Google App Engine
- ios - 使用新数据更新领域文件
- c - 从链表中的“node *”分配给“int *”的不兼容指针类型
- data-warehouse - 在数据仓库中,度量应该基于事实还是维度?