我正在使用Flutter应用程序,需要弹出屏幕。我尝试了initState()方法,但是没有运气。initState()第一次上课时被调用。
onResume()Flutter中是否有等效的Android 方法?
有任何想法吗?
Álv*_*ero 27
如果你转到另一个页面,那么在你回来时被调用
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
),
).then((value) {
_refreshFirstPage();
});
Run Code Online (Sandbox Code Playgroud)
die*_*per 10
您可以使用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
}
}
}
Run Code Online (Sandbox Code Playgroud)
请记住,每次您打开应用程序或进入后台并返回到应用程序时,它将调用。(如果您的小部件处于活动状态)
如果您在第一次加载Widget时只想要一个侦听器,则可以使用进行侦听addPostFrameCallback,例如以下示例:
class YourWidgetState extends State<YourWidget> {
_onLayoutDone(_) {
//do your stuff
}
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback(_onLayoutDone);
super.initState();
}
}
Run Code Online (Sandbox Code Playgroud)
信息:https : //docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html
使用 focus_ detector更多信息可以查看visibility_ detector
\n每次您的小部件在屏幕上出现或消失时都会收到通知。
\n类似于 Android 上的 onResume()/onPause() 和 iOS 上的 viewDidAppear()/viewDidDisappear()。
\n每当发生某些事件获取或给予您的小部件焦点时,焦点检测器就会为您触发回调。例如,此类事件可能是用户:
\n导航至/自另一个屏幕;
\n当您的小部件可见时打开/关闭设备\xe2\x80\x99s 屏幕;
\n当您的小部件可见时切换到/从另一个应用程序切换;
\n将您的小部件滚动进/出屏幕;
\n@override\nWidget build(BuildContext context) =>\n FocusDetector(\n onFocusLost: () {\n logger.i(\n \'Focus Lost.\'\n \'\\nTriggered when either [onVisibilityLost] or [onForegroundLost] \'\n \'is called.\'\n \'\\nEquivalent to onPause() on Android or viewDidDisappear() on iOS.\',\n );\n },\n onFocusGained: () {\n logger.i(\n \'Focus Gained.\'\n \'\\nTriggered when either [onVisibilityGained] or [onForegroundGained] \'\n \'is called.\'\n \'\\nEquivalent to onResume() on Android or viewDidAppear() on iOS.\',\n );\n },\n onVisibilityLost: () {\n logger.i(\n \'Visibility Lost.\'\n \'\\nIt means the widget is no longer visible within your app.\',\n );\n },\n onVisibilityGained: () {\n logger.i(\n \'Visibility Gained.\'\n \'\\nIt means the widget is now visible within your app.\',\n );\n },\n onForegroundLost: () {\n logger.i(\n \'Foreground Lost.\'\n \'\\nIt means, for example, that the user sent your app to the background by opening \'\n \'another app or turned off the device\\\'s screen while your \'\n \'widget was visible.\',\n );\n },\n onForegroundGained: () {\n logger.i(\n \'Foreground Gained.\'\n \'\\nIt means, for example, that the user switched back to your app or turned the \'\n \'device\\\'s screen back on while your widget was visible.\',\n );\n },\n child: Container(),\n );\nRun Code Online (Sandbox Code Playgroud)\n
您可以通过注册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...
}
}
Run Code Online (Sandbox Code Playgroud)
有关WidgetsBindingObserver更多信息,请参阅。
| 归档时间: |
|
| 查看次数: |
1373 次 |
| 最近记录: |