【问题标题】:How to return null from stream builder如何从流生成器返回 null
【发布时间】:2020-07-15 17:51:40
【问题描述】:

我有一个想以编程方式移除抽屉的场景。
我使用流构建器来获取值并决定显示或删除抽屉,例如,当流值为真时,我返回我的抽屉小部件,否则返回 null 以删除抽屉,但流构建器不允许我返回 null,如果我return empty 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,
              );
            }
          }
          
        },
      ),
);

【问题讨论】:

    标签: flutter


    【解决方案1】:

    AppBar()中添加automaticallyImplyLeading

      `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,
                  );
                }
              }
            },
          ),
        );
    

    当你不必显示Drawer时返回null

      drawer: null,
    

    【讨论】:

    • 您的解决方案存在问题,它没有删除抽屉,只是隐藏了抽屉图标,并且可以通过屏幕滑动打开抽屉。
    • 流生成器不允许我返回 null 我如何设置抽屉:当我的流值为 false 时为 null
    猜你喜欢
    • 2021-12-29
    • 2020-06-24
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 2017-06-10
    • 2017-07-05
    相关资源
    最近更新 更多