我正在开发一个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
}
}
}
请记住,如果您的小部件处于活动状态,每次打开应用程序或进入后台并返回应用程序时都会调用它。
如果您只想在小部件首次加载时侦听,请使用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
更新: 空安全合规性
WidgetsBinding.instance?.addObserver(this);
和 WidgetsBinding.instance?.removeObserver(this);
中添加 ?
来实现空安全。 - kosiara - Bartosz KosarzyckiNavigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
),
).then((value) {
_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
每当您的小部件出现或从屏幕中消失时,都会收到通知。
类似于Android上的onResume() / onPause()和iOS上的viewDidAppear() / viewDidDisappear()。
焦点检测器会在某些情况下为您触发回调,例如:
导航到/从其他屏幕;
在您的小部件可见时打开/关闭设备屏幕;
在您的小部件可见时切换到/从另一个应用程序;
将您的小部件滚动进/出屏幕;
@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(),
);