应用程序退出而未在设备上按回键时调用任何回调,颤振

时间:2018-11-28 07:55:49

标签: flutter

应用程序入口点:

void main() {
  runWhat();}

void runWhat() async{

//getLoggedInSharedPrefs() gets logged in state from SharedPrefs
  await getLoggedInSharedPrefs().then((isLoggedIn) {
    if(isLoggedIn) {
      runApp(Home()); // User is Logged in go to Home; 
    } else {
      runApp(new MyApp()); // Login Screen - separate from Home
    }
  });
}

Home中,我想提醒用户按回去,并提醒他们是否要退出应用程序。但是_onWillPopdispose都没有被呼叫

主页是与MyApp分开的屏幕,而不是MyApp的正文

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    HomeState homeState() => new HomeState();
    return homeState();
  }
}

class HomeState extends State<Home> {

    @override
    Widget build(BuildContext context) {
      return WillPopScope(
        onWillPop: _onWillPop,
         child: new MaterialApp(.....

    @override
      void dispose() {
        print('dispose: $this');
        super.dispose();
      }

     Future<bool> _onWillPop() {
        print("Poppoing Home on will popo");
        return showDialog(
              context: context,
              builder: (context) => new AlertDialog(
                    title: new Text('Home - Are you sure?'),
                    content: new Text('Do you want to exit'),
                    actions: <Widget>[
                      new FlatButton(
                        onPressed: () => Navigator.pop(context),
                        child: new Text('No'),
                      ),
                      new FlatButton(
                        onPressed: () => exit(0),
                        child: new Text('Yes'),
                      ),
                    ],
                  ),
            ) ??
            false;
      }


... }

2 个答案:

答案 0 :(得分:1)

您需要按照WillPopScopeMaterialApp之内的顺序重新排列设置应用程序的方式,如Scaffold

应用程序类

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData.dark(),
        home: Scaffold(
          body: HomePage(),
        ),
      );
  }
}

您的页面

import 'dart:async';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return new WillPopScope(
              onWillPop: _onWillPop,
               child:new Center(
              child: new Text("Home Page"),
            ),
          );
  }

  Future<bool> _onWillPop() {
    return showDialog(
          context: context,
          builder: (context) => new AlertDialog(
                title: new Text('Are you sure?'),
                content: new Text('Do you want to exit an App'),
                actions: <Widget>[
                  new FlatButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    child: new Text('No'),
                  ),
                  new FlatButton(
                    onPressed: () => Navigator.of(context).pop(true),
                    child: new Text('Yes'),
                  ),
                ],
              ),
        ) ??
        false;
  }
}

Demo

答案 1 :(得分:0)

从@SnakeyHips获取提示,我如下修改了我的代码,但我需要Scaffold对标签导航保持状态

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(fontFamily: 'Georgia'),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {

  ....

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: new WillPopScope(
    onWillPop: _onWillPop,
    .... 
  }

 }