如何从流生成器返回空值

时间:2020-07-15 17:51:40

标签: flutter

我有一种情况,我想以编程方式删除抽屉。
我使用流构建器来获取值并决定显示或删除抽屉,例如,当流值为true时,我返回抽屉小部件,否则返回null来移除抽屉,但流构建器不允许我返回null,如果我返回空的Container flutter不会从应用程序栏中删除抽屉图标,因此如何实现我的目标并使用流构建器删除抽屉。
这是我的代码

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 个答案:

答案 0 :(得分:0)

automaticallyImplyLeading()中添加AppBar

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

代码:

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

不必显示Drawer时返回null

  drawer: null,