Flutter, how to use confirmDismiss in Dismissible?

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

What I have:

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


        
3条回答
  •  余生分开走
    2021-02-07 01:25

    Here is an example from the flutter repo.

    Tested on 1.12 version of flutter.

    Future _showConfirmationDialog(BuildContext context, String action) {
      return showDialog(
        context: context,
        barrierDismissible: true,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Do you want to $action this item?'),
            actions: [
              FlatButton(
                child: const Text('Yes'),
                onPressed: () {
                  Navigator.pop(context, true); // showDialog() returns true
                },
              ),
              FlatButton(
                child: const Text('No'),
                onPressed: () {
                  Navigator.pop(context, false); // showDialog() returns false
                },
              ),
            ],
          );
        },
      );
    }
    

    Add inside Dismissible widget:

    confirmDismiss: (DismissDirection dismissDirection) async {
      switch(dismissDirection) {
        case DismissDirection.endToStart:
          return await _showConfirmationDialog(context, 'archive') == true;
        case DismissDirection.startToEnd:
          return await _showConfirmationDialog(context, 'delete') == true;
        case DismissDirection.horizontal:
        case DismissDirection.vertical:
        case DismissDirection.up:
        case DismissDirection.down:
          assert(false);
      }
      return false;
    }
    

提交回复
热议问题