Android应用程序退出按下后退按钮,而不是导航到上一个屏幕-Flutter



这是main.dart

void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
return MaterialApp(
title: 'List',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
home: HomeScreen());
}
}

这是homescreen.dart

class HomeScreen extends StatefulWidget {
HomeScreen({Key key}) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int _currentindex = 0;
final List<Widget> screen = [
DashboardNavigator(),
RegisterPage(),
];
Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure ?'),
content: new Text('Do you want to exit ?'),
actionsPadding: EdgeInsets.only(right:10.0,bottom:10.0),
actions: <Widget>[
new GestureDetector(
onTap: () => Navigator.of(context).pop(false),
child: Text("No",
style:TextStyle(
color:Colors.red
)),
),
SizedBox(height: 16),
new GestureDetector(
onTap: () => Navigator.of(context).pop(true),
child: Text("Yes",
style:TextStyle(
color:Colors.green
)),
),
],
),
) ??
false;
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop:_onBackPressed,
child:Scaffold(
body: SafeArea(
child: IndexedStack(
index:_currentindex,
children: screen,
)
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentindex,
onTap: (int index) {
this.setState(() {
_currentindex = index;
}
);
},
items:[
BottomNavigationBarItem(icon: Icon(Icons.home,color:Colors.green),title:Text('Home')),
BottomNavigationBarItem(icon: Icon(Icons.settings_ethernet,color:Colors.green),title:Text('Console')),
]
)
));
}
}

这是导航器小部件

class DashboardNavigator extends StatefulWidget {
DashboardNavigator({Key key}) : super(key: key);
@override
_DashboardNavigatorState createState() => _DashboardNavigatorState();
}
class _DashboardNavigatorState extends State<DashboardNavigator> {
String url;
@override
Widget build(BuildContext context) {
return Navigator(
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute(
settings: settings,
builder: (BuildContext context) {
switch(settings.name) {
case '/':
return DashBoard();
case '/listhub':
return ListHub();
case '/listhub/listfarmer':
return ListFarmer(url);
}
},
);
}
);
}
}

我使用Navigator.pushNamed函数在这些屏幕之间导航

onPressed: () => Navigator.pushNamed(context,"/listhub"),

但当我点击android后退按钮时,它会关闭应用程序,而不是正在导航到上一页。。。我在每一页中都尝试了WillpopScope,但它不起作用。

请建议我使用android后退按钮和appBar后退按钮导航回上一个屏幕的正确方式。

我相信这是因为当操作系统级别的后退手势和_onBackPressed功能从MaterialApp的导航器中弹出时,您正在按下自己的Navigator。由于后者没有任何路由推送,它的行为与预期一致,并退出了应用程序。

这个Github问题用几个不同的解决方案详细介绍了这个问题。您可以在main中创建一个全局键,并使用navigatorKey参数将其传递给materialApp,使用其键参数将其传给DashboardNavigator。这样,无论您在哪里按下或弹出,MaterialApp和DashboardNavigator都将使用相同的导航器。

这就是我通过引用以下内容使其工作的方式:

class _MyAppState extends State<MyApp> {
final _navigatorKey = GlobalKey<NavigatorState>();//define a navigation key.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My APP',
navigatorKey: _navigatorKey,//this is the navigation key
onGenerateRoute: (_) => null,
builder: (context, child) {
return Navigator(
key: _navigatorKey,//same navigation key
pages: [
MaterialPage(
key: const ValueKey('homepage'),
child: HomPage(),//homepage widget
....
],
onPopPage: (route, result) {
//pop logic here. 
}
);
}
);
}
}

这里的讨论还提到了使用WillPopScope包装器,也许你也可以尝试一下。

相关内容

最新更新