本文介绍了单击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打开抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:15