【问题标题】:How to use Drawer without Scaffold.drawer?如何在没有 Scaffold.drawer 的情况下使用 Drawer?
【发布时间】:2019-01-24 06:49:43
【问题描述】:

我注意到 Scaffold.drawer 的 Drawer 只有在存在 Scaffold 的 AppBar 时才会显示。

但是,我使用了位于 BottomNavigationBar 中的 BottomAppBar,而不是 AppBar。

如何让 Drawer 与 BottomAppBar 一起使用? 这是我的代码下面没有出现抽屉

class homieclass extends State<homie>{

@覆盖 小部件构建(BuildContext 上下文){ 返回材料应用程序( debugShowCheckedModeBanner:假, 家:新脚手架(

    backgroundColor: Colors.white70.withOpacity(0.9),
    floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    floatingActionButton: FloatingActionButton(onPressed: (){},backgroundColor: Colors.redAccent,child: ImageIcon(new AssetImage("ast/hello123.png")),),
    bottomNavigationBar: BottomAppBar(child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,mainAxisSize: MainAxisSize.max,children: <Widget>[
        IconButton(icon: Icon(Icons.menu), onPressed: (){}),IconButton(icon: Icon(Icons.message), onPressed: (){}),
    ],
    ),
    ),
    body: new Column(
      children: <Widget>[new SizedBox(height: 50.0, ),
        Container(margin: EdgeInsets.only(left: 0.0),child: new Text("Events",textAlign: TextAlign.left,style: TextStyle(fontFamily: 'ssfr',fontSize: 35.0,fontWeight: FontWeight.bold),),)
        , Container(margin: EdgeInsets.only(left: 10.0,right: 10.0) ,width: 360.0,height: 40.0,decoration: new BoxDecoration(color: Colors.blueGrey.withOpacity(0.2),
          border: new Border.all(color: Colors.blueGrey.withOpacity(0.0), width: 2.0),
          borderRadius: new BorderRadius.circular(10.0),),child: new Row(children: <Widget>[SizedBox(width: 10.0,),Icon(Icons.search,color: Colors.blueGrey.withOpacity(0.9),),Text(" Search",style: TextStyle(fontFamily: 'ssft',color: Colors.blueGrey,fontSize: 20.0),)],),)
      ,new SizedBox(height: 10.0,),new SizedBox(
        height: 5.0,
        child: new Center(
          child: new Container(
            margin: new EdgeInsetsDirectional.only(start: 1.0, end: 1.0),
            height: 2.0
            ,
            color: Colors.redAccent.withOpacity(0.8),
          ),
        ),
      ),],
    ),drawer: new Drawer(
    child: new ListView(
      children: <Widget>[ListTile(title: Text("hello"),)],
    ),
  ),

  ),
);

}

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您可以从颤振本身的openDrawerglobal key 中完成这项工作。

    1. Scaffold.of(context).openDrawer() / Scaffold.of(context).openEndDrawer();
    2. scaffoldKey.currentState.openDrawer(); /scaffoldKey.currentState..openEndDrawer();

    Case 1

    bottomNavigationBar:BottomAppBar(
          elevation: 10,
          shape: CircularNotchedRectangle(),
          child: Container(
            height: 80,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      Scaffold.of(context).openDrawer();
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.dashboard),
                        Text(
                          'DASHBOARD',
                          style: TextStyle(color: Colors.black),
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: GestureDetector(
                    onTap: () {},
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.home),
                        Text(
                          'CHALLENGES',
                          style: TextStyle(color: Colors.black),
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: GestureDetector(
                    onTap: (){},
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.public),
                        Text(
                          'Public',
                          style: TextStyle(color: Colors.black),
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: GestureDetector(
                    onTap: () {},
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.sentiment_satisfied),
                        Text(
                          'Settings',
                          style: TextStyle(color: Colors.black),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          color: AppColors.WHITE,
        );
    

    Case 2 或者你可以脚手架键

    class BottomBar {
      final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
      Widget build(BuildContext context) {
        return Scaffold(
            key: scaffoldKey,
            bottomNavigationBar: BottomAppBar(
              elevation: 10,
              shape: CircularNotchedRectangle(),
              child: Container(
                height: 80,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          scaffoldKey.currentState.openDrawer();
                          // scaffoldKey.currentState.openEndDrawer(); // use to open end drawer
                        },
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.dashboard),
                            Text(
                              'DASHBOARD',
                              style: TextStyle(color: Colors.black),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {},
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.home),
                            Text(
                              'CHALLENGES',
                              style: TextStyle(color: Colors.black),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {},
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.public),
                            Text(
                              'Public',
                              style: TextStyle(color: Colors.black),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {},
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.sentiment_satisfied),
                            Text(
                              'Settings',
                              style: TextStyle(color: Colors.black),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              color: AppColors.WHITE,
            ));
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用抽屉,但您必须提供DrawerController,并安排抽屉覆盖您的其他内容。使用Stack 很容易做到这一点。堆栈保持一个不透明的容器很重要,否则当绘图滑入和滑出时,您将获得渲染工件。 Scaffold 不需要这样做,但令人讨厌的是,它还会在绘制移动时重建其他内容(正是他们试图避免的那种事情)。

      import 'package:flutter/gestures.dart';
      import 'package:flutter/material.dart';
      import 'package:flutterui/util/layout_util.dart';
      
      void main() => runApp(MyApp());
      
      class MyApp extends StatelessWidget {
        @override
        Widget build(BuildContext context) =>
            MaterialApp(title: 'Flutter Playground',
                home: Material(child:DrawerStack(body: body(), drawer: _drawer())));
      }
      
      Drawer _drawer() =>Drawer(
        child: SafeArea(
          child: Center(
            child: Column(
              children: [
                Text('endDrawer content'),
                Builder(builder:(context) => RaisedButton(
                  child: Text('Click', semanticsLabel: 'Click 2'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                )),
              ],
            ),
          ),
        ),
      );
      
      Widget body() => Container(
        decoration: BoxDecoration(color: Color.fromARGB(255, 255, 255, 255)),
              child: SafeArea(
                  child: Center(
                      child: Column(children: [
            Text('Body'), // style:TextStyle(fontSize: 14.0,color: Colors.black)),
            Builder(builder:(context) => RaisedButton(
                child: Text('Open drawer'),
                onPressed: () {
                  (context.ancestorWidgetOfExactType(DrawerStack) as DrawerStack).openDrawer();
      //            DrawerStack.of(context).openDrawer();
                })),
          ]))));
      
      
      class DrawerStack extends StatelessWidget {
        final GlobalKey<DrawerControllerState> _drawerKey =
        GlobalKey<DrawerControllerState>();
        final drawerScrimColor = Color.fromARGB(90, 100, 100, 128);
        final double drawerEdgeDragWidth = null;
        final DragStartBehavior drawerDragStartBehavior = DragStartBehavior.start;
      
        final Widget body;
        final Drawer drawer;
      
        DrawerStack({Key key, this.body, this.drawer}) : super(key: key);
      
        void openDrawer() {
          _drawerKey.currentState?.open();
        }
      
        @override
        Widget build(BuildContext context) => Stack(
          children: [
            // body
            body,
      
            DrawerController(
              key: _drawerKey,
              alignment: DrawerAlignment.end,
              child: drawer,
              drawerCallback: (_){},
              dragStartBehavior: drawerDragStartBehavior,
              //widget.drawerDragStartBehavior,
              scrimColor: drawerScrimColor,
              edgeDragWidth: drawerEdgeDragWidth,
            ),
          ],
        );
      }
      
      

      【讨论】:

        【解决方案3】:

        它非常适合我。这是一个工作示例,底部栏中有一个专用的“显示抽屉”按钮(抽屉也可以从左侧拖入):

        import 'package:flutter/material.dart';
        
        void main() => runApp(MyApp());
        
        class MyApp extends StatelessWidget {
          @override
          Widget build(BuildContext context) {
            return MaterialApp(
              title: 'Flutter Playground',
              home: TestPage(),
            );
          }
        }
        
        class TestPage extends StatelessWidget {
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              body: Center(
                child: Text('Body'),
              ),
              bottomNavigationBar: Builder(builder: (BuildContext context) {
                return BottomAppBar(
                  color: Colors.orange,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      IconButton(icon: Icon(Icons.menu), onPressed: () {
                        Scaffold.of(context).openDrawer();;
                      }),
                      IconButton(icon: Icon(Icons.message), onPressed: () {}),
                    ],
                  ),
                );
                },),
              drawer: Drawer(
                child: SafeArea(
                  right: false,
                  child: Center(
                    child: Text('Drawer content'),
                  ),
                ),
              ),
            );
          }
        }
        

        Flutter 版本:最新的主版本(虽然我也很确定它适用于 beta 版本)

        【讨论】:

        • 感谢您展示 .. 但我收到以下错误:方法 openDrawer 在 null 上被调用。我创建了{{ global key final scaffoldkey = GlobalKey();}} 。然后在 onpressed = { scaffoldkey.currentState.openDrawer();} 中写了这个。有什么想法吗?
        • 这就是我将BottomAppBar 包装在Builder 小部件中的原因,以便它的context 可以访问脚手架。如果您不想使用Builder,而是使用GlobalKey,则必须确保将Scaffoldid 参数设置为该ID:Scaffold(id: scaffoldKey, ...)
        • 如果回答对你有帮助,请点赞并采纳。
        • 这并没有真正回答如何在没有 Scaffold 的情况下使用 Drawer。
        猜你喜欢
        • 2018-08-02
        • 2021-10-30
        • 2019-11-15
        • 2013-06-30
        • 2015-08-09
        • 2018-08-21
        • 2017-06-11
        • 2017-12-02
        • 2014-06-24
        相关资源
        最近更新 更多