单击AppBar打开抽屉
问题内容:
如果创建一个脚手架,则有一个抽屉选项。如果现在创建此抽屉,则会自动在应用程序栏的开头位置获得菜单图标。但是我想在那里打开抽屉的其他图标。我试图自己使图标按钮处于领先位置,但即使使用“ Scafold.of(context).openDrawer()”,该按钮也无法打开抽屉。
是否可以替换抽屉按钮的图标?
问题答案:
使用Key
你的Scaffold
,并通过调用显示抽屉myKey.currentState.openDrawer()
,这里是工作的代码:
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(),
),
),
);
}
}