【问题标题】:Flutter Scaffold.of(context).openDrawer() doesn't workFlutter Scaffold.of(context).openDrawer() 不起作用
【发布时间】:2019-09-25 09:42:31
【问题描述】:

我想在按下 BottomMenu 中的自定义按钮后打开一个抽屉我在使用 Scaffold.of(context).openDrawer() 时遇到问题,它不起作用。 My BottomMenu 是一个单独的 小部件类。据我了解,它不起作用,因为它是一个单独的上下文。如何获得正确的上下文?或者也许有人知道另一种解决方案。

这里是我的代码复制器:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Drawer'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      bottomNavigationBar: BottomMenu(),
      endDrawer: SizedBox(
        width: double.infinity,
        child: Drawer(
          elevation: 16,
          child: Container(
            color: Colors.black,
            child: ListView(
              padding: EdgeInsets.zero,
              children: <Widget>[
                ListTile(
                    title: Text('Some context here',
                        style: TextStyle(color: Colors.white))),
                ListTile(
                    title: Text('Some context here',
                        style: TextStyle(color: Colors.white))),
              ],
            ),
          ),
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Call Drawer form menu reproducer',
            )
          ],
        ),
      ),
    );
  }
}

class BottomMenu extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: Wrap(
        alignment: WrapAlignment.center,
        children: <Widget>[
          Divider(color: Colors.black, height: 1),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 2),
            child: Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  InkWell(
                    borderRadius: new BorderRadius.circular(20.0),
                    customBorder: Border.all(color: Colors.black),
                    child: Container(
                      padding: EdgeInsets.only(
                          left: 3, right: 6, bottom: 15, top: 11),
                      child: Row(
                        children: <Widget>[
                          Icon(Icons.menu),
                          Text('Show menu', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold)),
                        ],
                      ),
                    ),
                    onTap: () {
                      Scaffold.of(context).openDrawer();
                    },
                  ),
                ],
              ),
          ),
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart navigation-drawer


    【解决方案1】:

    就我而言,这行得通。

    return Scaffold(
          key: _scaffoldKey,
          endDrawerEnableOpenDragGesture: false,  // This!
          appBar: AppBar(
            iconTheme: IconThemeData(color: Colors.white),
            leading: IconButton(
              icon: Icon(Icons.menu, size: 36),
              onPressed: () => _scaffoldKey.currentState.openDrawer(),  // And this!
            ),
          ),
          drawer: DrawerHome(),
          ....
    

    _scaffoldKey必须初始化为,

    final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
    

    在班级下。

    【讨论】:

    • 我尝试了这个解决方案,但 没有 添加 endDrawerEnableOpenDragGesture: false, 并且它仍然有效.. :)
    【解决方案2】:

    我的问题解决了,而不是

    Scaffold.of(context).openEndDrawer()
    

    我给 Scaffold 钥匙,然后按如下状态调用

    _scaffoldkey.currentState.openEndDrawer()
    

    它解决了我的问题,我希望它也适用于你

    【讨论】:

      【解决方案3】:

      问题是您在Scaffold 上指定了endDrawer,但您正在调用Scaffold.of(context).openDrawer()

      openDrawer() 文档状态:

      如果脚手架有一个非空的 Scaffold.drawer,此函数将导致抽屉开始其入口动画。

      由于您的 drawer 为空,因此什么也没有发生。

      相比之下,openEndDrawer() 告诉我们:

      如果脚手架有一个非空的 Scaffold.endDrawer,此函数将导致端侧抽屉开始其入口动画。

      由于您的endDrawer 不为空,您应该使用openEndDrawer() 方法。或者,如果您不关心抽屉从哪一侧滑入,您可以在构建Scaffold 时使用drawer 而不是endDrawer

      【讨论】:

        【解决方案4】:
        Scaffold.of(context).openEndDrawer()
        

        【讨论】:

          【解决方案5】:

          这里有类似的问题。单击按钮,没有任何反应。问题是我正在使用实例化 Scaffold 的小部件的上下文。不是 Scaffold 子项的上下文。

          我是这样解决的:

          // body: Column(
                  //   children: <Widget>[
                  //     Row(
                  //       children: <Widget>[        
                  //         IconButton(
                  //           icon: Icon(Icons.filter_list),
                  //           onPressed: () => Scaffold.of(context).openEndDrawer(), (wrong context)
                  //         ),
                  //       ],
                  //     ),
                  //   ],
                  // )
          

          收件人:

          body: Builder(
                      builder: (context) => Column(
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  IconButton(
                                    icon: Icon(Icons.filter_list),
                                    onPressed: () => Scaffold.of(context).openEndDrawer(),
                                  ),
                                ],
                              ),
                            ],
                          )),
                ),
          

          【讨论】:

            【解决方案6】:

            如果您的 appbar 小部件带有用于启动抽屉的操作按钮并且抽屉永远不会被推动,请记住您需要在 appbar: ... 之后定义 endDrawer: YOURAppDrawerWIDGET(), 否则使用 Scaffold.of(context).openEndDrawer() 将不起作用。

            Scaffold(
                appBar: AppBar(title: Text(_title)),
                endDrawer: AppDrawer(), // <-- this is required or else it will not know what is opening
                body: SingleChildScrollView(
                 ///...
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-06-15
              • 1970-01-01
              • 1970-01-01
              • 2020-03-11
              • 1970-01-01
              • 2020-02-06
              • 1970-01-01
              • 2019-07-05
              相关资源
              最近更新 更多