Flutter,如何在Dismissible中使用confirmDismiss? [英] Flutter, how to use confirmDismiss in Dismissible?

查看:451
本文介绍了Flutter,如何在Dismissible中使用confirmDismiss?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的:

Dismissible(
    key: Key(state.threads[index].toString()),
    onDismissed: (direction) {
        setState(() {
            state.threads.removeAt(index);
        });
    },
);

工作正常.我可以通过向左滑动来消除项目.但是,我想确认该操作,我应该理解和阅读的内容应该是

Works fine. I can dismiss items with left swipe. However I'd want to confirm the action and what I understood and read I should use is

confirmDismiss:

但是,作为一个初学者,由于缺乏示例,加上文档字面意义上我无法理解的任何内容.如何实现呢?

However as a beginner and with lack of examples, plus the documentation literally explaining nothing for me that I understand. How to achieve this?

推荐答案

confirmDismiss属性中,您可以返回AlertDialog()(或您喜欢的任何类型的对话框),然后列出可能的结果(例如,删除和取消),然后返回true(删除)或false(取消),然后确定是要删除该项目还是需要将其保留在列表中.

In the confirmDismiss attribute you can return an AlertDialog() (or whatever type of dialog you prefer) and then list the possible outcomes (e.g., delete and cancel) in buttons and return either true (delete) or false (cancel) which then determines if the item has to be removed or needs to stay in the list.

示例:

confirmDismiss: (DismissDirection direction) async {
  return await showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: const Text("Confirm"),
        content: const Text("Are you sure you wish to delete this item?"),
        actions: <Widget>[
          FlatButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: const Text("DELETE")
          ),
          FlatButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: const Text("CANCEL"),
          ),
        ],
      );
    },
  );
},

您可以将逻辑提取到一种方法中,以使代码更具可读性.

You can extract the logic into a method to make the code more readable.

这篇关于Flutter,如何在Dismissible中使用confirmDismiss?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆