我正在使用“可放行的物品”列表,并希望在一个方向上滑动以删除该物品,但在另一个方向上滑动以启动该物品的编辑。但是,Flutter坚持必须在onDismissed回调中从树中删除Dismissible项。我试过重新插入该项目,但这不起作用。有任何想法吗?下面是从创建列表项的代码中提取的内容:

  return new Dismissible(
    key: new ObjectKey(item),
    direction: DismissDirection.horizontal,
    onDismissed: (DismissDirection direction) {
      setState(() {
        item.deleteTsk();
      });
      if (direction == DismissDirection.endToStart){
        //user swiped left to delete item
        _scaffoldKey.currentState.showSnackBar(new SnackBar(
          content: new Text('You deleted: ${item.title}'),
          action: new SnackBarAction(
            label: 'UNDO',
            onPressed: () { handleUndo(item); }
          )
        ));
      }
      if (direction == DismissDirection.startToEnd){
        //user swiped right to edit so undo the delete required by flutter
        Async.scheduleMicrotask((){handleUndo(item);});
        Navigator.of(context).pushNamed('/tskedit');
      }
    },
  ...

最佳答案

只要更改商品 key ,Dismissible就会认为您的商品已被撤消。假设您的商品类别为MyItem。如果您在MyItem.from类中实现了一个构造函数MyItem,用于复制字段,例如:

class MyItem {
  MyItem({ @required this.title, @required this.color });
  MyItem.from(MyItem other) : title = other.title, color = other.color;
  final String title;
  final Color color;
}

然后,您可以将handleUndo(item)替换为handleUndo(new MyItem.from(item)),以便您的new ObjectKey(item)与以前使用的旧ObjectKey保持唯一(假设您未在operator ==上实现MyItem)。

关于dart - Flutter Dismissible坚持必须从树中删除列表项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45919197/

10-10 07:07