Flutter上的小部件的onResume()和onPause() [英] onResume() and onPause() for widgets on Flutter
问题描述
现在,小部件仅具有initeState()和dispose(),该initeState()在第一次创建小部件时被触发,而dispose()在小部件被破坏时被触发.有没有一种方法可以检测小部件何时返回到前台?当一个小部件因为另一个小部件刚刚被前景显示而将要移到后台时? 相当于在Android上触发了onResume和onPause,在ios上触发了viewWillAppear和viewWillDisappear
Right now, a widget only has initeState() that gets triggered the very first time a widget is created, and dispose(), which gets triggered when the widget is destroyed. Is there a method to detect when a widget comes back to the foreground? and when a widget is about to go to the background because another widget just was foregrounded? It's the equivalent of onResume and onPause being triggered for Android, and viewWillAppear and viewWillDisappear for ios
推荐答案
最常见的情况是您正在运行动画并且不想在后台消耗资源.在这种情况下,应使用TickerProviderStateMixin
扩展State
并将State
用作AnimationController
的vsync
参数.当您的State
可见时,Flutter只会处理动画控制器的侦听器.
The most common case where you'd want to do this is if you have an animation running and you don't want to consume resources in the background. In that case, you should extend your State
with TickerProviderStateMixin
and use your State
as the vsync
argument for the AnimationController
. Flutter will take care of only calling the animation controller's listeners when your State
is visible.
如果您希望当PageRoute
被其他内容遮挡时处置位于PageRoute
中的State
,则可以传递 PageStorage
如果您不希望完全重置,请保留用户的进度信息.
If you want the State
s that live in your PageRoute
to be disposed when the PageRoute
is obscured by other content, you can pass a maintainState
argument of false
to your PageRoute
constructor. If you do this, your State
will reset itself (and its children) when it's hidden and will have to re-construct itself in initState
using the properties passed in as constructor arguments to its widget
. You can use a model or controller class, or PageStorage
, to hold the user's progress information if you don't want a complete reset.
这是一个演示这些概念的示例应用程序.
Here is a sample app that demonstrates these concepts.
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,
),
],
),
);
}
}
这篇关于Flutter上的小部件的onResume()和onPause()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!