0
votes

I have a scenario which I want to remove drawer programmatically.
I use a stream builder to get the value and decide to show or remove the drawer, for example when stream value is true I return my drawer widget otherwise return null to remove drawer but stream builder won't allow me to return null and if I return empty Container flutter won't remove drawer icon from app bar, so how can I achieve my goal and remove the drawer with a stream builder.
here is my code

new Scaffold(
      appBar: PreferredSize(
        child: MyAppBar(
          title: "Settings",
        ),
        preferredSize: Size.fromHeight(55),
      ),
      drawer: StreamBuilder(
        stream: settingService.getSettings,
        builder: (BuildContext context, snapshot) {
          if (snapshot.hasData) {
            final drawer= snapshot.data;
            if (drawer.visibility) {
              return myAppDrawerWidget(
                activeIndex: 13,
              );
            }
          }
          
        },
      ),
);
1

1 Answers

0
votes

Add automaticallyImplyLeading in AppBar()

  `automaticallyImplyLeading`: false, // this will hide Drawer hamburger icon

Code:

Scaffold(
      appBar: PreferredSize(
        child: AppBar(
          title: Text('Settings'),
          automaticallyImplyLeading: _isDrawerShowing,
        ),
        preferredSize: Size.fromHeight(55),
      ),
      drawer: StreamBuilder(
        stream: settingService.getSettings,
        builder: (BuildContext context, snapshot) {
          if (snapshot.hasData) {
            final drawer= snapshot.data;
            setState(() {
              _isDrawerShowing = drawer.visibility;
            });
            if (drawer.visibility) {
              return myAppDrawerWidget(
                activeIndex: 13,
              );
            }
          }
        },
      ),
    );

OR

return null when you don't have to show Drawer

  drawer: null,