简体   繁体   English

单击 AppBar 打开抽屉

[英]Open drawer on clicking AppBar

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.如果您现在创建此抽屉,您会自动在应用栏的前导 position 上获得菜单图标。 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.我试图自己在领先的 position 上制作一个图标按钮,但即使使用“Scafold.of(context).openDrawer()”,这个按钮也无法打开抽屉,它无法打开它。

Is there any option to replace the icon for the drawer button?是否有任何选项可以替换抽屉按钮的图标?

Use a Key in your Scaffold and show the drawer by calling myKey.currentState.openDrawer() , here is a working code:Scaffold使用Key并通过调用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(),
        ),
      ),
    );
  }
}

Alternative to the accepted answer which does not require a GlobalKey :不需要GlobalKey的已接受答案的替代方案:

class _TestState extends State<Test> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      drawer: new Drawer(),
      appBar: new AppBar(
        leading: Builder(
        builder: (context) => IconButton(
            icon: new Icon(Icons.settings),
            onPressed: () => Scaffold.of(context).openDrawer(),
          ),
        ),
      ),
    );
  }
}

Using GlobalKey:使用全局密钥:

final GlobalKey<ScaffoldState> _key = GlobalKey(); // Create a key

@override
Widget build(BuildContext context) {
  return Scaffold(
    key: _key, // Assign the key to Scaffold.
    drawer: Drawer(),
    floatingActionButton: FloatingActionButton(
      onPressed: () => _key.currentState!.openDrawer(), // <-- Opens drawer
    ),
  );
}

Using Builder:使用生成器:

@override
Widget build(BuildContext context) {
  return Scaffold(
    drawer: Drawer(),
    floatingActionButton: Builder(builder: (context) {
      return FloatingActionButton(
        onPressed: () => Scaffold.of(context).openDrawer(), // <-- Opens drawer.
      );
    }),
  );
}

you need initialize scaffoldKey after that,之后你需要初始化scaffoldKey

Open drawer and close drawer打开抽屉和关闭抽屉

 GestureDetector(
          onTap: () {
            if(scaffoldKey.currentState.isDrawerOpen){
              scaffoldKey.currentState.openEndDrawer();
            }else{
              scaffoldKey.currentState.openDrawer();
            }
          },
          child:  LeadingIcon(icon: Icons.menu),//your button
        ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM