gpt4 book ai didi

dart - Flutter Dismissible 坚持必须从树中删除列表项

转载 作者:IT老高 更新时间:2023-10-28 12:40:08 27 4
gpt4 key购买 nike

我正在使用 Dismissible 项目列表,并希望在一个方向上滑动以删除该项目,但在另一个方向上滑动以启动对该项目的编辑。但是,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');
}
},
...

最佳答案

您可以为此使用 Dismissible 小部件的 confirmDismiss 功能。

如果您不希望小部件被关闭,那么您只需要从 confirmDismiss 返回 false

不要使用 onDismissed 来做你的滑动后处理,使用 confirmDismiss 代替,它会为你提供滑动方向就像 onDismissed.

这里是confirmDismiss函数的官方文档:

Gives the app an opportunity to confirm or veto a pending dismissal. If the returned Future completes true, then this widget will be dismissed, otherwise it will be moved back to its original location. If the returned Future completes to false or null the [onResize]

这是一个例子:

Dismissible(
confirmDismiss: (direction) async {
if (direction == DismissDirection.startToEnd) {
/// edit item
return false;
} else if (direction == DismissDirection.endToStart) {
/// delete
return true;
}
},
key: Key(item.key),
child: Text(item.name),
)

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com