Flutter, how to use confirmDismiss in Dismissible?

前端 未结 3 642
一个人的身影
一个人的身影 2021-02-07 00:41

What I have:

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


        
3条回答
  •  旧时难觅i
    2021-02-07 01:27

    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.

    Example:

    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: [
              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.

提交回复
热议问题