【发布时间】:2018-11-03 04:37:05
【问题描述】:
我正在开发一个 Flutter 应用程序,需要弹出屏幕。我尝试了initState() 方法,但没有运气。 initState() 在我第一次开课时被调用。
我们在 Flutter 中是否有等效于 Android 的 onResume() 方法?
有什么想法吗?
【问题讨论】:
我正在开发一个 Flutter 应用程序,需要弹出屏幕。我尝试了initState() 方法,但没有运气。 initState() 在我第一次开课时被调用。
我们在 Flutter 中是否有等效于 Android 的 onResume() 方法?
有什么想法吗?
【问题讨论】:
您可以使用WidgetsBindingObserver 并检查AppLifeCycleState,如下例所示:
class YourWidgetState extends State<YourWidget> with WidgetsBindingObserver {
@override
void initState() {
WidgetsBinding.instance?.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance?.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
//do your stuff
}
}
}
请记住,每次打开应用程序或进入后台并返回应用程序时都会调用它。 (如果您的小部件处于活动状态)
如果你只是想在你的 Widget 第一次加载时监听,你可以使用 addPostFrameCallback 监听,就像这个例子:
class YourWidgetState extends State<YourWidget> {
_onLayoutDone(_) {
//do your stuff
}
@override
void initState() {
WidgetsBinding.instance?.addPostFrameCallback(_onLayoutDone);
super.initState();
}
}
信息:https://docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html
更新:安全合规性为零
【讨论】:
addPostFrameCallback 和LayoutBuilder 的build() 方法是否同时接收控制/焦点?
WidgetsBinding.instance?.addObserver(this); 和WidgetsBinding.instance?.removeObserver(this); 中添加? 以实现空安全......
如果你转到另一个页面,然后在你回来时被调用
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
),
).then((value) {
_refreshFirstPage();
});
【讨论】:
_refreshFirstPage();。我希望有更好的方法来做到这一点。
您可以通过注册didChangeAppLifecycleState 观察者来完成此操作:
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(final AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
setState(() {
// ...your code goes here...
});
}
}
@override
Widget build(final BuildContext context) {
// ...your code goes here...
}
}
更多信息请参见WidgetsBindingObserver。
【讨论】:
使用focus_detector更多信息可以看visibility_detector
@override
Widget build(BuildContext context) =>
FocusDetector(
onFocusLost: () {
logger.i(
'Focus Lost.'
'\nTriggered when either [onVisibilityLost] or [onForegroundLost] '
'is called.'
'\nEquivalent to onPause() on Android or viewDidDisappear() on iOS.',
);
},
onFocusGained: () {
logger.i(
'Focus Gained.'
'\nTriggered when either [onVisibilityGained] or [onForegroundGained] '
'is called.'
'\nEquivalent to onResume() on Android or viewDidAppear() on iOS.',
);
},
onVisibilityLost: () {
logger.i(
'Visibility Lost.'
'\nIt means the widget is no longer visible within your app.',
);
},
onVisibilityGained: () {
logger.i(
'Visibility Gained.'
'\nIt means the widget is now visible within your app.',
);
},
onForegroundLost: () {
logger.i(
'Foreground Lost.'
'\nIt means, for example, that the user sent your app to the background by opening '
'another app or turned off the device\'s screen while your '
'widget was visible.',
);
},
onForegroundGained: () {
logger.i(
'Foreground Gained.'
'\nIt means, for example, that the user switched back to your app or turned the '
'device\'s screen back on while your widget was visible.',
);
},
child: Container(),
);
【讨论】: