我想在AlertBox操作发生之前添加一个小的onDismissed()
所以,用户从列表中滑动该项目以删除它,但在操作发生之前,我想问他是否确定。
是否可以使用当前的Dismissible小部件?
编辑:

onDismissed: (direction) {
    final User _deletedItem = this._items[index];

    showDialog(
      context: context,
      barrierDismissible: false, // user must tap a button
      builder: (BuildContext context) {
        return AlertDialog(
          content: Text(
              'Are you sure you want to delete this?'),
          actions: <Widget>[
            FlatButton(
              child: Text('Cancel'),
              onPressed: () {
                Navigator.of(context).pop(false);
              },
            ),
            FlatButton(
              child: Text('OK'),
              onPressed: () {
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      },
    ).then((answer) {
      if (answer == true) {
        setState(() {
          this._items.removeAt(index);
        });

        Scaffold.of(context).showSnackBar(
          SnackBar(
            content: Text("Deleted"),
            duration: Duration(milliseconds: 2000),
          ),
        );
      } else {
        setState(() {
          this._items.insert(index, _deletedItem);
        });
      }
    });
  },

最佳答案

抱歉,目前无法阻止使用标准的“dismissible”滑动项目。
如果你想达到你想要的目标,你可能想要实现你的定制方式,利用“有孩子”的方式,然后监听“事件”。
我建议的更简单的方法是让用户滑动(并让项目离开),然后立即显示一个GestureDetector告诉用户是否要撤消该删除操作,然后您可以通过再次将项目放回列表中来处理撤消操作。
编辑:
要执行删除和撤消操作,可以执行此操作,

GlobalKey<ScaffoldState> _key = GlobalKey(); // added
List<String> _list = List.generate(10, (index) => "${index}");

@override
Widget build(BuildContext context) {
  return Scaffold(
    key: _key, // added
    appBar: AppBar(title: Text("App")),
    body: ListView.builder(
      itemCount: _list.length,
      itemBuilder: (context, index) {
        return Dismissible(
          key: Key(_list[index]),
          child: ListTile(title: Text(_list[index])),
          background: Container(color: Colors.red),
          onDismissed: (direction) {
            setState(() {
              // added this block
              String deletedItem = _list.removeAt(index);
              _key.currentState..removeCurrentSnackBar()..showSnackBar(
                SnackBar(
                  content: Text("Deleted \"${deletedItem}\""),
                  action: SnackBarAction(
                    label: "UNDO",
                    onPressed: () => setState(() => _list.insert(index, deletedItem),) // this is what you needed
                  ),
                ),
              );
            });
          },
        );
      },
    ),
  );
}

截图
flutter - 如何在Dismissible的onDismissed() Action 被触发之前添加 Action ?-LMLPHP

关于flutter - 如何在Dismissible的onDismissed() Action 被触发之前添加 Action ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54282454/

10-10 20:13