flutter - 从不同的有状态小部件打开抽屉菜单

标签 flutter dart

我有 3 个 StatefulWidget、MainPage、CustomAppbar 和 ProfileCustomAppbar。在我的 MainPage 中,我像这样填充我的抽屉:

    Widget build(BuildContext context) {
    return MaterialApp(

      home: Scaffold(
        appBar: (currentPage == profilePage)

            ? ProfileCustomAppbar(
                height: 60,
              )
            : CustomAppbar(
                height: 60,
              ),
        endDrawer: populateDrawer(),
        body: Container(
          decoration: BoxDecoration(color: Colors.white),
          child: currentPage,
        ),

      ),
    );
  }
      populateDrawer() {
        return Theme(
          data: Theme.of(context).copyWith(
              canvasColor:
              Colors.white //This will change the drawer background to blue.
            //other styles
          ),
          child: Drawer(
            child: Column(
              children: <Widget>[
                DrawerHeader(
                  child: Center(
                    child: Image.asset("assets/images/lf_logo.png",
                        height: 100, width: 100),
                  ),
                ),
                Divider(color: Configuration.PrimaryColor),

              ],
            ),
          ),
        );
      }

现在我想从 ProfileCustomAppbar 打开抽屉 这是我调用抽屉的方式:

IconButton(
               icon: Icon(Icons.settings, color: Colors.brown,),
               onPressed: () {
                 Scaffold.of(context).openDrawer();
               },
             )

但是抽屉打不开,怎么办?

最佳答案

您需要将 IconButton 小部件包装在 Builder 小部件中以打开抽屉。引用以下代码:

      Builder(
        builder: (BuildContext context) {
          return new IconButton(
            icon: Icon(Icons.category),
            onPressed: () {
              Scaffold.of(context).openDrawer();
            },
          );
        },
      ),

关于flutter - 从不同的有状态小部件打开抽屉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57249369/

相关文章:

android - Flutter SetState 不更新文本

dart - 如何在 flutter 中截取屏幕外的小部件的屏幕截图?

flutter - 如何不围绕文本创建边框,而是围绕框边框创建边框?

jquery - 在 Dart 2 中为 HTML 元素创建类似 jQuery 的动画

android-studio - Android Studio 代码折叠和 Dart 2 中可选的 new 关键字

Flutter检查器在Visual Studio Code中错误/卡住

flutter - 如何使用从页面到自身的 GetX 导航

dart - 如何在 Flutter 中清除 AnimatedList 中的所有项目

html - 获取IFrameElement的节点

flutter - 如何正确实例化 didChangeDependencies 中的 bloc?