gpt4 book ai didi

android - 带自定义按钮的抽屉导航

转载 作者:行者123 更新时间:2023-12-03 03:07:36 24 4
gpt4 key购买 nike

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

我不确定实现此目标的最佳方法是什么,我希望获得有关如何进行的一些建议!

enter image description here

谢谢!

最佳答案

使用 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

关于android - 带自定义按钮的抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57982228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com