Flutter Scaffold.of(context).openDrawer() 不起作用

标签 flutter dart navigation-drawer

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

这是我的代码复制器:

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

最佳答案

就我而言,这奏效了。

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>();
下课。

关于Flutter Scaffold.of(context).openDrawer() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58095547/

相关文章:

firebase - 在 FireStore 中删除评论的最安全且成本更低的方法

Flutter SizeTransition 和 PageRouteBuilder 不工作

csv - 如何在 flutter 中从设备存储中读取 CSV 文件

flutter - 如何解决此 StackOverflow 错误

Dart 正在应用程序目录中查找包(加载资源失败)

dart - 异步的优缺点是什么,何时使用和何时不使用它以及回调的其他替代方案是什么?

android - 无法捕获工具栏主页按钮单击事件

java - android.view.InflateException : Binary XML file line #17: Error inflating class android. support.design.widget.NavigationView

android - 抽屉导航的自定义适配器

flutter - 使用List.from与List.map将列表转换为新类型之间的区别?