Flutter 抽屉打开并单击底部导航按钮时出错
我对颤振是新手,下面是代码,如果抽屉打开,我在“BottomNavigationBar onTap”中的“setState”之前调用了“Navigator.of(context).pop()”,工作正常 但若抽屉并没有打开并且代码运行,它将弹出当前页面,而不是用新的主体推送重建的页面Flutter 抽屉打开并单击底部导航按钮时出错,flutter,Flutter,我对颤振是新手,下面是代码,如果抽屉打开,我在“BottomNavigationBar onTap”中的“setState”之前调用了“Navigator.of(context).pop()”,工作正常 但若抽屉并没有打开并且代码运行,它将弹出当前页面,而不是用新的主体推送重建的页面 class BottomNavigationBarScreen extends StatefulWidget { static String routeName = '/bottomnavigationscre
class BottomNavigationBarScreen extends StatefulWidget {
static String routeName = '/bottomnavigationscreen';
@override
_BottomNavigationBarScreenState createState() =>
_BottomNavigationBarScreenState();
}
class _BottomNavigationBarScreenState extends State<BottomNavigationBarScreen> {
int _currentIndex = 0;
final _optionButton = [
HomeScreen(),
SearchScreen(),
ScanScreen(),
ShoppingCartScreen(),
];
// void onTap(int index) {
// setState(() {
// _currentIndex = index;
// });
// }
@override
Widget build(BuildContext context) {
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
return Scaffold(
key: _scaffoldKey,
body: SafeArea(child: _optionButton[_currentIndex]),
drawer: AppDrawerWidget(),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
onTap: (int index) {
Navigator.of(context).pop();
setState(() {
_currentIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.home,
color: Theme.of(context).iconTheme.color,
),
title: Text(''),
),
BottomNavigationBarItem(
icon: Icon(
Icons.search,
color: Theme.of(context).iconTheme.color,
),
title: Text('')),
BottomNavigationBarItem(
icon: Icon(
IconData(59392, fontFamily: 'icons'),
color: Theme.of(context).iconTheme.color,
),
title: Text('')),
BottomNavigationBarItem(
icon: Icon(
Icons.shopping_cart,
color: Theme.of(context).iconTheme.color,
),
title: Text('')),
]),
);
}
}
class BottomNavigationBarScreen扩展StatefulWidget{
静态字符串routeName='/bottomnavigationscreen';
@凌驾
_BottomNavigationBarScreenState createState()=>
_BottomNavigationBarScreenState();
}
类_BottomNavigationBarScreenState扩展状态{
int _currentIndex=0;
最终选项按钮=[
主屏幕(),
SearchScreen(),
扫描屏幕(),
ShoppingCartScreen(),
];
//void onTap(整数索引){
//设置状态(){
//_currentIndex=指数;
// });
// }
@凌驾
小部件构建(构建上下文){
GlobalKey _scaffoldKey=GlobalKey();
返回脚手架(
钥匙:_scaffoldKey,
正文:安全区(子项:_optionButton[_currentIndex]),
抽屉:AppDrawerWidget(),
底部导航栏:底部导航栏(
类型:BottomNavigationBarType.fixed,
currentIndex:_currentIndex,
onTap:(int索引){
Navigator.of(context.pop();
设置状态(){
_currentIndex=索引;
});
},
项目:[
底部导航气压计(
图标:图标(
我的家,
颜色:Theme.of(context).iconTheme.color,
),
标题:文本(“”),
),
底部导航气压计(
图标:图标(
Icons.search,
颜色:Theme.of(context).iconTheme.color,
),
标题:文本(“”)),
底部导航气压计(
图标:图标(
Iconda(59392,fontFamily:“图标”),
颜色:Theme.of(context).iconTheme.color,
),
标题:文本(“”)),
底部导航气压计(
图标:图标(
图标。购物车,
颜色:Theme.of(context).iconTheme.color,
),
标题:文本(“”)),
]),
);
}
}
您可以使用\u scaffoldKey.currentState.isDrawerOpen
检查抽屉是否打开在您的情况下,不需要
Navigator.of(context.pop()代码>除非您的AppDrawerWidget中有特殊操作
当抽屉打开时,点击抽屉外侧将自动关闭抽屉
您可以复制粘贴运行下面的完整代码
代码片段
onTap: (int index) {
if (_scaffoldKey.currentState.isDrawerOpen) {
Navigator.of(context).pop();
}
print("onTap");
setState(() {
_currentIndex = index;
});
},
工作演示
完整代码
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',
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: BottomNavigationBarScreen(),
);
}
}
class BottomNavigationBarScreen extends StatefulWidget {
static String routeName = '/bottomnavigationscreen';
@override
_BottomNavigationBarScreenState createState() =>
_BottomNavigationBarScreenState();
}
class _BottomNavigationBarScreenState extends State<BottomNavigationBarScreen> {
int _currentIndex = 0;
final _optionButton = [
HomeScreen(),
SearchScreen(),
ScanScreen(),
ShoppingCartScreen(),
];
// void onTap(int index) {
// setState(() {
// _currentIndex = index;
// });
// }
@override
Widget build(BuildContext context) {
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text("Listen to Drawer Open / Close Example"),
),
body: SafeArea(child: _optionButton[_currentIndex]),
drawer: AppDrawerWidget(),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
onTap: (int index) {
if (_scaffoldKey.currentState.isDrawerOpen) {
Navigator.of(context).pop();
}
print("onTap");
setState(() {
_currentIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.home,
color: Theme.of(context).iconTheme.color,
),
title: Text(''),
),
BottomNavigationBarItem(
icon: Icon(
Icons.search,
color: Theme.of(context).iconTheme.color,
),
title: Text('')),
BottomNavigationBarItem(
icon: Icon(
IconData(59392, fontFamily: 'icons'),
color: Theme.of(context).iconTheme.color,
),
title: Text('')),
BottomNavigationBarItem(
icon: Icon(
Icons.shopping_cart,
color: Theme.of(context).iconTheme.color,
),
title: Text('')),
]),
);
}
}
class AppDrawerWidget extends StatefulWidget {
@override
_AppDrawerWidgetState createState() => _AppDrawerWidgetState();
}
class _AppDrawerWidgetState extends State<AppDrawerWidget> {
@override
void initState() {
super.initState();
print("open");
}
@override
void dispose() {
print("close");
super.dispose();
}
@override
Widget build(BuildContext context) {
return Drawer(
child: Column(
children: <Widget>[
Text("test1"),
Text("test2"),
Text("test3"),
],
),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("HomeScreen");
}
}
class SearchScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("SearchScreen");
}
}
class ScanScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("ScanScreen");
}
}
class ShoppingCartScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("ShoppingCartScreen");
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
//这是应用程序的主题。
//
//尝试使用“flutter run”运行应用程序。您将看到
//应用程序有一个蓝色工具栏。然后,在不退出应用程序的情况下,重试
//将下面的primarySwatch更改为Colors.green,然后调用
//“热重新加载”(在运行“颤振运行”的控制台中按“r”,
//或者只需将更改保存到颤振IDE中的“热重新加载”。
//请注意,计数器没有重置回零;应用程序
//未重新启动。
主样本:颜色。蓝色,
),
主页:底部导航栏屏幕(),
);
}
}
类BottomNavigationBarScreen扩展StatefulWidget{
静态字符串routeName='/bottomnavigationscreen';
@凌驾
_BottomNavigationBarScreenState createState()=>
_BottomNavigationBarScreenState();
}
类_BottomNavigationBarScreenState扩展状态{
int _currentIndex=0;
最终选项按钮=[
主屏幕(),
SearchScreen(),
扫描屏幕(),
ShoppingCartScreen(),
];
//void onTap(整数索引){
//设置状态(){
//_currentIndex=指数;
// });
// }
@凌驾
小部件构建(构建上下文){
GlobalKey _scaffoldKey=GlobalKey();
返回脚手架(
钥匙:_scaffoldKey,
appBar:appBar(
标题:文本(“听抽屉打开/关闭示例”),
),
正文:安全区(子项:_optionButton[_currentIndex]),
抽屉:AppDrawerWidget(),
底部导航栏:底部导航栏(
类型:BottomNavigationBarType.fixed,
currentIndex:_currentIndex,
onTap:(int索引){
if(_scaffoldKey.currentState.isDrawerOpen){
Navigator.of(context.pop();
}
打印(“onTap”);
设置状态(){
_currentIndex=索引;
});
},
项目:[
底部导航气压计(
图标:图标(
我的家,
颜色:Theme.of(context).iconTheme.color,
),
标题:文本(“”),
),
底部导航气压计(
图标:图标(
Icons.search,
颜色:Theme.of(context).iconTheme.color,
),
标题:文本(“”)),
底部导航气压计(
图标:图标(
Iconda(59392,fontFamily:“图标”),
颜色:Theme.of(context).iconTheme.color,
),
标题:文本(“”)),
底部导航气压计(
图标:图标(
图标。购物车,
颜色:Theme.of(context).iconTheme.color,
),
标题:文本(“”)),
]),
);
}
}
类AppDrawerWidget扩展StatefulWidget{
@凌驾
_AppDrawerWidgetState createState()=>\u AppDrawerWidgetState();
}
类\u AppDrawerWidgetS