单击 AppBar 打开抽屉 [英] Open drawer on clicking AppBar

查看:23
本文介绍了单击 AppBar 打开抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你创建了一个脚手架,有一个抽屉选项.如果你现在创建这个抽屉,你会自动获得应用栏前导位置的菜单图标.但我想要一个可以打开抽屉的其他图标.我试图自己在前导位置制作一个图标按钮,但是这个按钮即使使用Scafold.of(context).openDrawer()"也无法打开抽屉.

If you create an Scafold there is an option for drawer. If you now create this drawer you get automaticly the menu icon on the leading position of the appbar. But i want an other icon there which opens the drawer. I tried to make an iconbutton myself on the leading position but this button can‘t open the drawer even with „Scafold.of(context).openDrawer()" it can‘t open it.

是否有任何选项可以替换抽屉按钮的图标?

Is there any option to replace the icon for the drawer button?

推荐答案

Scaffold 中使用 Key 并通过调用 myKey.currentState 来显示抽屉.openDrawer(),这是一个工作代码:

Use a Key in your Scaffold and show the drawer by calling myKey.currentState.openDrawer(), here is a working code:

import "package:flutter/material.dart";

class Test extends StatefulWidget {
  @override
  _TestState createState() => new _TestState();
}

class _TestState extends State<Test> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      drawer: new Drawer(),
      appBar: new AppBar(
        leading: new IconButton(
          icon: new Icon(Icons.settings),
          onPressed: () => _scaffoldKey.currentState.openDrawer(),
        ),
      ),
    );
  }
}

这篇关于单击 AppBar 打开抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆