简体   繁体   中英

How to fix 'A dismissed Dismissible widget is still part of the tree.' in flutter

I display a CupertinoPopupSurface and use a Dismissible widget to dismiss the dialog with a gesture. When I swipe down I call Navigator.of(context).pop(); to close the dialog and return to the previous screen but I am getting and error which says Dismissible is still part of the tree.

I tried providing a unique key Dismissible(key: UniqueKey(), ...) but it still is not working.

 CupertinoPopupSurface(
   isSurfacePainted: false,
   child: Container(
     padding: const EdgeInsets.only(top: 100),
     child: Dismissible(
       key: UniqueKey(),
       direction: DismissDirection.down,
       onDismissed: (direction) {
         Navigator.of(context).pop();
       },
       child: Container(
           decoration: BoxDecoration(
             borderRadius:
                 BorderRadius.vertical(top: Radius.circular(40)),
           ),
           child: ZoneFilter(
               visit: visit, accountsViewModel: accountsViewModel)),
     ),
   ),
 );

I would like to call Navigator.of(context).pop(); and get rid of the Dismissible widget.

Your Dismissible widget is inside your Popup, not around it as it should be. You can make it work by changing it to the code below. Fair warning though, it will feel slow because the darkened background of the dialog will only disappear once the dismiss animation finishes.

return Dismissible(
    key: UniqueKey(),
    direction: DismissDirection.down,
    onDismissed: (direction) => Navigator.of(context).pop();,
    child:  CupertinoPopupSurface(
      isSurfacePainted: false,
      child: Container(
        padding: const EdgeInsets.only(top: 100),
        child: Container(
          decoration: BoxDecoration(
            borderRadius:
            BorderRadius.vertical(top: Radius.circular(40)),
          ),
          child: ZoneFilter(
            visit: visit, accountsViewModel: accountsViewModel)),
          ),
        ),
      ),
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM