Search code examples
flutterdynamicwidgetdrawerscaffold

How to run function when after endDrawer is closed in flutter


I have product list page from which i opened endDrawer which contains cart item. if i remove cart items it does not update the product list page automatically. how to use .then function in endDrawer in flutter?


Solution

  • Unfortunately you can't just Wrap your end drawer with a DrawerController and get callbacks when it is closed or opened, because Scaffold does make a drawer controller for it self and encapsulates it so you can not modify it.

    The only other way I can think of is putting the drawer controller outside your main scaffold or showing it using Overlay if you are familiar with those.

    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            children: <Widget>[
              Positioned.fill(
                child: Scaffold(
                  appBar: PreferredSize(
                    preferredSize: Size.fromHeight(kToolbarHeight),
                    child: UserAppBar(),
                  ),
                  body: PostsPage(),
                ),
              ),
              Positioned.fill(
                child: DrawerController(
                  child: Drawer(
                    child: Column(),
                  ),
                  drawerCallback: (open){
                    print(open);
                  },
                  alignment: DrawerAlignment.end,
                ),
              ),
            ],
          ),
        );
      }
    }