Flutter 按下并移动后,android的后退按钮不起作用
我使用customnavigator软件包在所有页面上显示底部导航栏,使用global key在所有页面上显示navigationkey。我管理所有事情,比如导航,然后弹出页面,但我遇到了一个问题 当我使用pushAndRemoveUntill on logout按钮关闭堆栈上的所有页面时,它正常工作,但当我移动到注销屏幕时,android back按钮停止工作。 这是我的底部导航栏页面Flutter 按下并移动后,android的后退按钮不起作用,flutter,dart,Flutter,Dart,我使用customnavigator软件包在所有页面上显示底部导航栏,使用global key在所有页面上显示navigationkey。我管理所有事情,比如导航,然后弹出页面,但我遇到了一个问题 当我使用pushAndRemoveUntill on logout按钮关闭堆栈上的所有页面时,它正常工作,但当我移动到注销屏幕时,android back按钮停止工作。 这是我的底部导航栏页面 final String userid; ProductHome({this.userid});
final String userid;
ProductHome({this.userid});
@override
_ProductHomeState createState() => _ProductHomeState();
}
class _ProductHomeState extends State<ProductHome> {
var _myTabScreens = [
Chat(),
WriteMessage(),
ProductMain(),
Search(),
Profile(),
];
@override
void initState() {
super.initState();
var myFocusNode = FocusNode();
WidgetsBinding.instance.addPostFrameCallback((_){
FocusScope.of(context).requestFocus(myFocusNode);
});
}
int _currentIndex=2;
Widget page=ProductMain();
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void _selectTab(Widget tabItem, int index) {
if(page == ProductMain() ){
navigatorKey.currentState.popUntil((route) => route.isFirst);
} else {
setState(() {
page = _myTabScreens[index];
_currentIndex = index;
});
}
}
//form made here
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
final isFirstRouteInCurrentTab =!await navigatorKey.currentState.maybePop();
if (isFirstRouteInCurrentTab) {
if (_currentIndex != 2) {
_selectTab(ProductMain(), 2);
return false;
}
}
return isFirstRouteInCurrentTab;
},
child: Scaffold(
floatingActionButton: Padding(
padding: EdgeInsets.only(top: 31),
child: SizedBox(
height: 60,
width: 70,
child: FloatingActionButton(
backgroundColor: Colors.transparent,
elevation: 0,
onPressed: () {
},
child: Container(
height: 68,
width: 68,
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 4),
shape: BoxShape.circle,
gradient:_currentIndex==2?LinearGradient(
begin: const Alignment(0.7, -0.5),
end: const Alignment(0.6, 0.5),
colors: [
Color(0xFF53a78c),
Color(0xFF70d88b),
],
):LinearGradient(
begin: const Alignment(0.7, -0.5),
end: const Alignment(0.6, 0.5),
colors: [
Color(0xffDDE2E3),
Color(0xffDDE2E3),
],
),
),
child: IconButton(icon:Icon(Icons.home),onPressed: (){
setState(() {
navigatorKey.currentState.popUntil((route) => route.isFirst);
page=_myTabScreens[2];
_currentIndex=2;
});
},),
),
),
),
),
body: CustomNavigator(
navigatorKey:navigatorKey,
home: page,
pageRoute: PageRoutes.materialPageRoute,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
// shape: CircularNotchedRectangle(),
child: Container(
color: Colors.white,
height: 7.8*SizeConfig.heightMultiplier,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
iconSize: 3*SizeConfig.heightMultiplier,
padding: EdgeInsets.only(left: 28.0),
icon: Icon(Icons.message,color: _currentIndex==0?Colors.green:Color(0xffDDE2E3)),
onPressed: () {
if(_currentIndex==0){
navigatorKey.currentState.maybePop();
}else{
navigatorKey.currentState.popUntil((route) => route.isFirst);
}
setState(() {
page=_myTabScreens[0];
_currentIndex=0;
});
},
),
IconButton(
iconSize: 3*SizeConfig.heightMultiplier,
padding: EdgeInsets.only(right: 28.0),
icon: Icon(Icons.edit,color: _currentIndex==1?Colors.green:Color(0xffDDE2E3)),
onPressed: () {
navigatorKey.currentState.popUntil((route) => route.isFirst);
setState(() {
page=_myTabScreens[1];
_currentIndex=1;
});
},
),
IconButton(
iconSize: 3*SizeConfig.heightMultiplier,
padding: EdgeInsets.only(left: 28.0),
icon: Icon(Icons.search,color: _currentIndex==3?Colors.green:Color(0xffDDE2E3)),
onPressed: () {
navigatorKey.currentState.popUntil((route) => route.isFirst);
setState(() {
page=_myTabScreens[3];
_currentIndex=3;
});
},
),
IconButton(
iconSize: 3*SizeConfig.heightMultiplier,
padding: EdgeInsets.only(right: 28.0),
icon:Icon(Icons.person,color: _currentIndex==4?Colors.green:Color(0xffDDE2E3),),
onPressed: () {
navigatorKey.currentState.popUntil((route) => route.isFirst);
setState(() {
page=_myTabScreens[4];
_currentIndex=4;
});
},
)
],
),
),
),
),
);
}
}```
最终字符串userid;
ProductHome({this.userid});
@凌驾
_ProductHomeState createState()=>\u ProductHomeState();
}
类_ProductHomeState扩展状态{
变量_myTabScreens=[
Chat(),
WriteMessage(),
ProductMain(),
搜索(),
Profile(),
];
@凌驾
void initState(){
super.initState();
var myFocusNode=FocusNode();
WidgetsBinding.instance.addPostFrameCallback((){
FocusScope.of(上下文).requestFocus(myFocusNode);
});
}
int _currentIndex=2;
Widget page=ProductMain();
GlobalKey navigatorKey=GlobalKey();
void\u selectTab(小部件选项卡项,int索引){
如果(第==ProductMain()页){
navigatorKey.currentState.popUntil((route)=>route.isFirst);
}否则{
设置状态(){
页面=_myTabScreens[索引];
_currentIndex=索引;
});
}
}
//这里制作的表格
@凌驾
小部件构建(构建上下文){
返回式示波器(
onWillPop:()异步{
final isFirstRouteInCurrentTab=!await navigatorKey.currentState.maybePop();
如果(isFirstRouteInCurrentTab){
如果(_currentIndex!=2){
_选择tab(ProductMain(),2);
返回false;
}
}
返回isFirstRouteInCurrentTab;
},
孩子:脚手架(
浮动操作按钮:填充(
填充:仅限边缘设置(顶部:31),
孩子:大小盒子(
身高:60,
宽度:70,
子:浮动操作按钮(
背景颜色:颜色。透明,
海拔:0,
已按下:(){
},
子:容器(
身高:68,
宽度:68,
装饰:盒子装饰(
边框:边框。全部(颜色:Colors.white,宽度:4),
形状:BoxShape.circle,
梯度:_currentIndex==2?线性梯度(
开始:常量对齐(0.7,-0.5),
结束:常量对齐(0.6,0.5),
颜色:[
颜色(0xFF53a78c),
颜色(0xFF70d88b),
],
):LinearGradient(
开始:常量对齐(0.7,-0.5),
结束:常量对齐(0.6,0.5),
颜色:[
颜色(0xffDDE2E3),
颜色(0xffDDE2E3),
],
),
),
子项:图标按钮(图标:图标(Icons.home)),ON按下:(){
设置状态(){
navigatorKey.currentState.popUntil((route)=>route.isFirst);
页面=_myTabScreens[2];
_currentIndex=2;
});
},),
),
),
),
),
正文:CustomNavigator(
导航工作:导航工作,
主页:第页,
pageRoute:PageRoutes.MaterialPage路由,
),
floatingActionButtonLocation:floatingActionButtonLocation.centerDocked,
bottomNavigationBar:BottomAppBar(
//形状:CircularNotchedRectangle(),
子:容器(
颜色:颜色,白色,
高度:7.8*SizeConfig.高度倍增器,
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
图标按钮(
iconSize:3*SizeConfig.高度倍增器,
填充:仅限边缘设置(左侧:28.0),
图标:图标(Icons.message,颜色:_currentIndex==0?颜色。绿色:颜色(0xffDDE2E3)),
已按下:(){
如果(_currentIndex==0){
navigatorKey.currentState.maybePop();
}否则{
navigatorKey.currentState.popUntil((route)=>route.isFirst);
}
设置状态(){
页面=_myTabScreens[0];
_currentIndex=0;
});
},
),
图标按钮(
iconSize:3*SizeConfig.高度倍增器,
填充:仅限边缘设置(右侧:28.0),
图标:图标(Icons.edit,颜色:_currentIndex==1?颜色。绿色:颜色(0xffDDE2E3)),
已按下:(){
navigatorKey.currentState.popUntil((route)=>route.isFirst);
设置状态(){
页面=_myTabScreens[1];
_currentIndex=1;
});
},
),
图标按钮(
iconSize:3*SizeConfig.高度倍增器,
填充:仅限边缘设置(左侧:28.0),
图标:图标(图标。搜索,颜色:_currentIndex==3?颜色。绿色:颜色(0xffDDE2E3)),
已按下:(){
navigatorKey.currentState.popUntil((route)=>route.isFirst);
设置状态(){
页面=_myTabScreens[3];
_当前指数=3;
});
},
),
图标按钮(
iconSize:3*SizeConfig.高度倍增器,
填充:仅限边缘设置(右侧:28.0),
图标:图标(Icons.person,颜色:_currentIndex==4?颜色。绿色:颜色(0xffDDE2E3),),
已按下:(){
navigatorKey.currentState.popUntil((route)=>route.isFirst);
设置状态(){
页面=_