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

Bob*_*oby 2 dart flutter

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

    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),

              ],
            ),
          ),
        );
      }
Run Code Online (Sandbox Code Playgroud)

现在我想从这里打开抽屉,ProfileCustomAppbar我是这样称呼抽屉的:

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

但抽屉打不开,请问如何修复?

小智 5

您需要将 IconButton 小部件包装在 Builder 小部件内才能打开抽屉。参考下面的代码:

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