我创建了两个页面,一个是登录页面,一个是主页,但我想从我正在使用的主页关闭应用程序,Willpopscope
但它对我不起作用,我已经尝试了所有方法,但该onwillpop
方法没有调用,请帮助我,谢谢。
class main_page extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.amber),
home: my_page(),
);
}
}
class my_page extends StatefulWidget {
@override
_my_pageState createState() => _my_pageState();
}
class _my_pageState extends State<my_page> {
@override
void initState() {
check_login();
super.initState();
}
Future check_login() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
if (preferences.getBool("islogin") == false)
setState(() {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => MyApp(),
));
});
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () …
Run Code Online (Sandbox Code Playgroud)