Ale*_*tte 2 navigation android dart flutter
我目前正在尝试为我的应用程序设置一个搜索栏,如下图所示。目前我有一个可以很好地用作搜索栏的小部件,但我正在尝试实现一个使用该按钮的导航抽屉。有没有办法将导航抽屉绑定到它?我需要在 appBar 中重新创建这个小部件吗?
我不确定实现此目的的最佳方法是什么,我希望能提供一些有关如何进行的建议!
谢谢!
使用 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"),
)
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
AppBar(
leading: Builder(
builder: (BuildContext context) {
return IconButton(
icon: const Icon(Icons.menu),
onPressed: () { Scaffold.of(context).openDrawer(); },
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
);
},
),
)
Run Code Online (Sandbox Code Playgroud)
也可以看看:
| 归档时间: |
|
| 查看次数: |
4363 次 |
| 最近记录: |