如何在Flutter中使用AnimatedList小部件

时间:2019-06-10 07:53:42

标签: flutter dart flutter-animation flutter-animatedlist

我看到了这个video on 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 page显示消息

  

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

实际上似乎根本没有使用此小部件]

1 个答案:

答案 0 :(得分:-1)

我找到了问题here的答案

您可以找到我的代码--> HERE <--

及以下

    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,
          ),
        ),
      ),
    );
  }
}