在哪里可以获得信息以使WidgetsBindingObserver再次起作用?

时间:2018-09-13 07:41:51

标签: flutter

升级到Flutter 0.7.3通道Beta(Dart 2.1.0-dev.1.0.flutter-ccb16f7282)后,WidgetsBindingObserver无法正常工作。

如果AppLifecycleState暂停,不活动或暂停,则在登录前后均有效,它会返回主页。但似乎不适用于新的更新。我的问题是,在哪里可以获取信息以查看如何使WidgetsBindingObserver再次工作。

// Statefull HomePage
class PersonalLoginPage extends StatefulWidget {
  const PersonalLoginPage({ Key key }) : super(key: key);

  @override
  _PersonalLoginPageState createState() => new _PersonalLoginPageState();
}

class _PersonalLoginPageState extends State<PersonalLoginPage> with WidgetsBindingObserver {
  AppLifecycleState _appLifecycleState;
  bool _appStatePause;


  // TODO: initState function
  @override
  void initState() {
    print("initState Starting Now .......................");
    super.initState();
    authenticateUser();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    Timer _timer;
    setState(() {
      _appLifecycleState = state;
      if (_appLifecycleState == AppLifecycleState.paused ||
          _appLifecycleState == AppLifecycleState.inactive ||
          _appLifecycleState == AppLifecycleState.suspending) {
        _appStatePause = true;
        print("New Timer Starting Now .......................");
        _timer = Timer.periodic(Duration(seconds: 60), _callback);
      } else {
        _appStatePause = false;
      }
    });
  }

  void _callback(_timer) {
    if (_appStatePause == true) {
      print("Timer Finished without cancel...................");
      setState(() {
        Navigator.push(
          context,
          SlideRightRoute(widget: MyApp()),
        );
      });
    } else {
      _timer.cancel();
      print("Timer cancel now................................");
    }
  }

  // TODO: authenticateUser function
  Future authenticateUser() async {
    ……
    ……
    ……

  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  // TODO: main build Widget
  @override
  Widget build(BuildContext context) {

    return Scaffold(
         body: new Center(
            child: new Text(“Hello World”)
        )
    );
  }

0 个答案:

没有答案