[英]How to open Drawer on clicking Icon inside an AppBar in Flutter?
I have my files as follows:我的文件如下:
homepage.dart主页.dart
return Scaffold{
appBar: CustomAppBar(),
drawer: CustomDrawer(),
}
CustomAppBar.dart CustomAppBar.dart
class HomePageAppBar extends StatelessWidget with PreferredSizeWidget {
return AppBar{
leading: InkWell(
onTap: () {
// Need to open drawer when clicked
},
child: FaIcon(
FontAwesomeIcons.bars,
color: Theme.of(context).iconTheme.color,
size: Theme.of(context).iconTheme.size,
),
),
}
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
CustomDrawer.dart CustomDrawer.dart
return Drawer{
}
Now since my AppBar and Drawer are in separate files, I cannot use Scaffold.of(context).openDrawer() to open the drawer.现在,由于我的 AppBar 和 Drawer 位于不同的文件中,我无法使用 Scaffold.of(context).openDrawer() 打开抽屉。 I tried setting a GlobalKey to my Drawer but was now able to access it from my CustomAppBar file.
我尝试为我的抽屉设置一个 GlobalKey,但现在可以从我的 CustomAppBar 文件访问它。
I tried converting the CustomAppBar.dart file to a Scaffold so that it contains my appbar and my drawer and used Scaffold.of(context).openDrawer().我尝试将 CustomAppBar.dart 文件转换为脚手架,以便它包含我的应用程序栏和我的抽屉并使用 Scaffold.of(context).openDrawer()。 This caused the drawer to open and display only in the appbar area (Probably because of using PreferredSizeWidget).
这导致抽屉打开并仅显示在应用栏区域(可能是因为使用了 PreferredSizeWidget)。
How can I make the drawer open on clicking the AppBar's icon given that they are placed in separate files?鉴于它们被放置在单独的文件中,如何在单击 AppBar 的图标时打开抽屉? I want to keep my AppBar and Drawer in separate files.
我想将我的 AppBar 和 Drawer 保存在单独的文件中。
See the documentation https://api.flutter.dev/flutter/material/showModalBottomSheet.html见文档https://api.flutter.dev/flutter/material/showModalBottomSheet.html
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.amber,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet'),
ElevatedButton(
child: const Text('Close BottomSheet'),
onPressed: () => Navigator.pop(context),
)
],
),
),
);
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.