flutter - pushAndRemoveUntil后android的后退按钮不工作
问题描述
我使用 customnavigator 包作为底部导航栏出现在所有页面上。导航键的全局键。我管理所有的事情,比如导航然后弹出页面,但是当我在注销按钮上使用 pushAndRemoveUntill 时出现问题,以便它关闭所有堆栈上的页面正常工作,但是当我移动到注销屏幕时,android 后退按钮停止工作。这是我的底部导航栏页面
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;
});
},
)
],
),
),
),
),
);
}
}```
解决方案
如果您删除,直到,如您所说,所有路线都被清除。因此没有后页可去。
推荐阅读
- java - 据说Java会忽略额外的空格。为什么 c=a++ + ++b 没有空格就不能编译?
- python - 在没有页面加载的会话超时时更新表记录
- linux-kernel - uclinux中/proc/pid/status中的slack字段的用途是什么
- sql - 从 SQL Server 检索具有架构名称的所有表和视图名称
- android - Android Instrumentation 测试中的 ClassNotFoundException
- c# - 如何在嵌套响应中获得一对多关系的多个实体?
- nginx - 301重定向后如何在浏览器的url中保留主机头?
- react-native - React Native 最佳推荐框架或文件夹结构最佳实践
- python - python filter() 函数,使用 None 作为过滤器参数
- c# - c#字典在列表中