带自定义按钮的导航抽屉

时间:2019-09-17 21:22:45

标签: android flutter dart navigation

我目前正在尝试为我的应用设置一个搜索栏,如下图所示。目前,我有一个可以很好地用作搜索栏的小部件,但是我正在尝试实现使用该按钮的导航抽屉。有什么方法可以将导航抽屉绑定到它吗? 我需要在appBar中重新创建此小部件吗?

我不确定实现此目标的最佳方法是什么,我很乐意提出一些有关如何进行的建议!

enter image description here

谢谢!

3 个答案:

答案 0 :(得分:2)

使用GlobalKey并通过调用myKey.currentState.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',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: ListenToDrawerEvent(),
    );
  }
}


class ListenToDrawerEvent extends StatefulWidget {
  @override
  ListenToDrawerEventState createState() {
    return new ListenToDrawerEventState();
  }
}

class ListenToDrawerEventState extends State<ListenToDrawerEvent> {
  GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  static final List<String> _listViewData = [
    "Inducesmile.com",
    "Flutter Dev",
    "Android Dev",
    "iOS Dev!",
    "React Native Dev!",
    "React Dev!",
    "express Dev!",
    "Laravel Dev!",
    "Angular Dev!",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text("Listen to Drawer Open / Close Example"),
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: () {
            _scaffoldKey.currentState.openDrawer();
          },
        ),
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.all(10.0),
          children: _listViewData
              .map((data) => ListTile(
            title: Text(data),
          ))
              .toList(),
        ),
      ),
      body: Column(
        children: <Widget>[
          Center(
            child: Text('Main Body'),
          ),
          RaisedButton(
            padding: const EdgeInsets.all(8.0),
            textColor: Colors.white,
            color: Colors.blue,
            onPressed: () {_scaffoldKey.currentState.openDrawer();},
            child: new Text("open drawer"),
          )
        ],
      ),
    );
  }
}

enter image description here

答案 1 :(得分:1)

 AppBar(
   leading: Builder(
     builder: (BuildContext context) {
       return IconButton(
         icon: const Icon(Icons.menu),
         onPressed: () { Scaffold.of(context).openDrawer(); },
         tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
       );
     },
   ),
)

另请参阅:

答案 2 :(得分:0)

   if (_scaffoldKey.currentState.isDrawerOpen)
            _scaffoldKey.currentState.openEndDrawer();
          else {
            _scaffoldKey.currentState.openDrawer();
          }
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

您可以根据抽屉的哪一侧进行操作!!!!