listview - Flutter:在 ListView 中动画项目删除

标签 listview dart flutter

我正在从 Stream 构建一个 ListView。我需要为该列表的删除和插入设置动画,但不知道如何。

我看过 Flutter 的这个示例,但它与流没有任何关系:https://flutter.io/catalog/samples/animated-list/

非常感谢任何帮助:)

new StreamBuilder(

    stream: feed.stream, // this is a Stream<List<Product>>

    builder: (context, snapshot) {
      if (!snapshot.hasData)
        return const Text('Loading products');
      return new ListView.builder(
          itemCount: snapshot.data.length,
          itemBuilder: (context, index) {
            Product product = snapshot.data[index];
            return new ProductWidget(product);
          });
    });

最佳答案

作为 ListView 的替代品,您可以使用 AnimatedList:

enter image description here

// Remove "Pig" from the list
int removeIndex = 2;

// remove the item from the data list backing the AnimatedList
String removedItem = _data.removeAt(removeIndex);

// This builder is just so that the animation has something
// to work with before it disappears from view since the original
// has already been deleted.
AnimatedListRemovedItemBuilder builder = (context, animation) {
  // A method to build the Card widget.
  return _buildItem(removedItem, animation);
};

// notify the AnimatedList that the item was removed
_listKey.currentState.removeItem(removeIndex, builder);

This article解释更多。完整代码如下:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: Text('Update AnimatedList data')),
        body: BodyWidget(),
      ),
    );
  }
}

class BodyWidget extends StatefulWidget {
  @override
  BodyWidgetState createState() {
    return new BodyWidgetState();
  }
}

class BodyWidgetState extends State<BodyWidget> {
  // the GlobalKey is needed to animate the list
  final GlobalKey<AnimatedListState> _listKey = GlobalKey(); // backing data
  List<String> _data = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        SizedBox(
          height: 400,
          child: AnimatedList(
            key: _listKey,
            initialItemCount: _data.length,
            itemBuilder: (context, index, animation) {
              return _buildItem(_data[index], animation);
            },
          ),
        ),
        RaisedButton(
          child: Text(
            'Insert single item',
            style: TextStyle(fontSize: 20),
          ),
          onPressed: () {
            _onButtonPress();
          },
        )
      ],
    );
  }

  Widget _buildItem(String item, Animation animation) {
    return SizeTransition(
      sizeFactor: animation,
      child: Card(
        child: ListTile(
          title: Text(
            item,
            style: TextStyle(fontSize: 20),
          ),
        ),
      ),
    );
  }

  void _onButtonPress() {
    // replace this with method choice below
    _insertSingleItem();
  }

  void _insertSingleItem() {
    String item = "Pig";
    int insertIndex = 2;
    _data.insert(insertIndex, item);
    _listKey.currentState.insertItem(insertIndex);
  }

  void _insertMultipleItems() {
    final items = ['Pig', 'Chichen', 'Dog'];
    int insertIndex = 2;
    _data.insertAll(insertIndex, items);
    // This is a bit of a hack because currentState doesn't have
    // an insertAll() method.
    for (int offset = 0; offset < items.length; offset++) {
      _listKey.currentState.insertItem(insertIndex + offset);
    }
  }

  void _removeSingleItems() {
    int removeIndex = 2;
    String removedItem = _data.removeAt(removeIndex);
    // This builder is just so that the animation has something
    // to work with before it disappears from view since the
    // original has already been deleted.
    AnimatedListRemovedItemBuilder builder = (context, animation) {
      // A method to build the Card widget.
      return _buildItem(removedItem, animation);
    };
    _listKey.currentState.removeItem(removeIndex, builder);
  }

  void _removeMultipleItems() {
    int removeIndex = 2;
    int count = 2;
    for (int i = 0; i < count; i++) {
      String removedItem = _data.removeAt(removeIndex);
      AnimatedListRemovedItemBuilder builder = (context, animation) {
        return _buildItem(removedItem, animation);
      };
      _listKey.currentState.removeItem(removeIndex, builder);
    }
  }

  void _removeAllItems() {
    final length = _data.length;
    for (int i = length - 1; i >= 0; i--) {
      String removedItem = _data.removeAt(i);
      AnimatedListRemovedItemBuilder builder = (context, animation) {
        return _buildItem(removedItem, animation);
      };
      _listKey.currentState.removeItem(i, builder);
    }
  }

  void _updateSingleItem() {
    final newValue = 'I like sheep';
    final index = 3;
    setState(() {
      _data[index] = newValue;
    });
  }
}

关于listview - Flutter:在 ListView 中动画项目删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51909797/

相关文章:

Android-获取当前位置但经纬度返回0.0(计算错误)

android - 从 ListView 和数据库中删除项目

android - 来自此 Android 客户端应用程序 <empty> 的请求被阻止 - Google API 错误 Flutter

Android QuickContactBadge 带有类似 Android Lollipop 的字母

java - 使用 convertView 和自定义数据 listView 时出现 NullPointerException

flutter - 在 ListView 中使用具有共享首选项的“收藏夹”按钮

map - Dart ,一种替换 map 中键和值的好方法吗?

class - Flutter Equatable Class 属性必须全部为 final

flutter - Flutter States_Rebuilder仅更新列表中的录音带计数

flutter - sqlite中的多个参数 flutter 朔迷离