在应用程序中,启动后出现启动屏幕,然后我到达登录屏幕,该屏幕上没有应用程序栏或后退按钮,但我可以从设备后退按钮后退。当我从登录屏幕按下背面时,我不想回到初始屏幕。我尝试了许多解决方案,但是它们无法正常工作。
class Login extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'xyz',
home: LoginPage(),
debugShowCheckedModeBanner: false,
);
}
}
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
if (Navigator.canPop(context)) {
Navigator.pop(context); // i tried both the way
Navigator.of(context).pop();
} else {
SystemNavigator.pop();
}
},
child:Scaffold(
body: Container(.........................//here is my desiging stuff
我还尝试了return Future.value(false);
的真假值–
启动画面代码
@override
void initState (){
super.initState();
// TODO initial state stuff
new Future.delayed(
const Duration(seconds: 2),
() => Navigator.push(
context,
MaterialPageRoute(builder: (context) => Login()),
));
}
答案 0 :(得分:0)
只需将空函数传递给-WillPopScope
小部件。
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {}, // Empty Function.
child: Scaffold(
body: Container(), //here is my desiging stuff
));
}
}
OR
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
return false;
},
child: Scaffold(
body: Container(),
appBar: AppBar(),
),
);
}
如果您不想转到启动画面。
最好使用:
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) => Login()));
答案 1 :(得分:0)
只需将代码更改为:
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: ()async => false,
child:Scaffold(
body: Container(.........................//here is my desiging stuff
答案 2 :(得分:0)
我遇到了同样的问题,这是因为错误地用MaterialApp
包围了WillPopScope
(而不是包围了Scaffold
)。
感谢@ anmol-majhail,这是我的正确解决方案:
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fetch Data Example',
home: WillPopScope(
child: Scaffold(...),
onWillPop: () async {
return false;
}),
);
}