首页 > 解决方案 > 如何在 Flutter 中使用 AnimatedList Widget

问题描述

在 youtube 上看到了这个视频

我正在尝试实现一个动画列表,

但我对动画并不熟悉。

我应该插入的内容

位置: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 页面 显示消息

此页面已弃用,其内容可能已过时。

事实上似乎根本没有使用这个小部件]

标签: flutterdartflutter-animationflutter-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,
          ),
        ),
      ),
    );
  }
}

推荐阅读