onResume()和onPause()用于Flutter上的小部件
目前,一个小部件只有initeState(),它首次被创build时触发,dispose(),当小部件被销毁时被触发。 有没有一种方法来检测一个小部件何时回到前台? 当一个小部件即将到达背景,因为另一个小部件刚刚被预先弄好了? 这相当于Android的onResume和onPause被触发,而viewWillAppear和viewWillDisappear for ios
最常见的情况是你想要这样做,如果你有一个animation运行,你不想在后台消耗资源。 在这种情况下,您应该使用TickerProviderStateMixin
扩展您的State
,并使用您的State
作为AnimationController
TickerProviderStateMixin
参数。 当您的State
可见时,Flutter将只负责调用animation控制器的监听器。
如果您希望当您的PageRoute
中的状态被其他内容遮挡时被丢弃,您可以将一个为false
的maintainState
parameter passing给PageRoute
构造函数。 如果你这样做的话,你的State
会在它隐藏的时候重置自己(和它的子),并且必须使用作为构造函数parameter passing给它的widget
的属性在initState
重新构造自己。 如果您不想完全重置,则可以使用模型或控制器类或PageStorage
来保存用户的进度信息。
这里是一个演示这些概念的示例应用程序。
import 'package:flutter/material.dart'; void main() { runApp(new MaterialApp( onGenerateRoute: (RouteSettings settings) { if (settings.name == '/') { return new MaterialPageRoute<Null>( settings: settings, builder: (_) => new MyApp(), maintainState: false, ); } return null; } )); } class MyApp extends StatefulWidget { MyAppState createState() => new MyAppState(); } class MyAppState extends State<MyApp> with TickerProviderStateMixin { AnimationController _controller; @override void initState() { print("initState was called"); _controller = new AnimationController(vsync: this) ..repeat(min: 0.0, max: 1.0, period: const Duration(seconds: 1)) ..addListener(() { print('animation value ${_controller.value}'); }); super.initState(); } @override void dispose() { print("dispose was called"); _controller.dispose(); super.dispose(); } int _counter = 0; @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('home screen') ), body: new Center( child: new RaisedButton( onPressed: () { setState(() { _counter++; }); }, child: new Text('Button pressed $_counter times'), ), ), floatingActionButton: new FloatingActionButton( child: new Icon(Icons.remove_red_eye), onPressed: () { Navigator.push(context, new MaterialPageRoute( builder: (BuildContext context) { return new MySecondPage(counter: _counter); }, )); }, ), ); } } class MySecondPage extends StatelessWidget { MySecondPage({ this.counter }); final int counter; Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('Certificate of achievement'), ), body: new Column( crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ new Icon(Icons.developer_mode, size: 200.0), new Text( 'Congrats, you clicked $counter times.', style: Theme.of(context).textTheme.title, textAlign: TextAlign.center, ), new Text( 'All your progress has now been lost.', style: Theme.of(context).textTheme.subhead, textAlign: TextAlign.center, ), ], ), ); } }
有一个抽象类调用者WidgetsBindingObserver
https://docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html
在
@override void didChangeAppLifecycleState(AppLifecycleState state) { setState(() { _notification = state; }); }
有“国家”,可以作为pipe理
switch(state.index){ case 0:// resumed break; case 1:// inactive break; case 2:// paused break; }