如何检查我的小部件屏幕何时在像 Android 中的 onResume 这样的颤动中可见 [英] How to check when my widget screen comes to visibility in flutter like onResume in Android
问题描述
在android中,如果活动可见,则调用onResume
.Flutter中onResume
的等价方法是什么?
In android if an activity is visible onResume
is called. What is the equivalent method of onResume
in Flutter?
我需要知道我的小部件屏幕何时可见,以便我可以根据它自动播放视频.我可能会转到另一个小部件屏幕,当我回来时它应该会自动播放.
I need the know when my widget screen is visible so I can auto-play a video based on that. I might go to another widget screen an when I come back it should auto-play.
我的方法是在 didUpdateWidget
中播放视频,但是即使小部件屏幕不可见,每次也会调用 didUpdateWidget
.
My approach was to play the video in didUpdateWidget
but didUpdateWidget
is called every-time even the widget screen is not visible.
注意:我不是从 WidgetsBindingObserver
询问 didChangeAppLifecycleState
,因为它为应用程序生命周期不是特定的小部件屏幕.
Note: I'm not asking about didChangeAppLifecycleState
from WidgetsBindingObserver
as it gives onResume
etc callbacks for the app lifecycle not a particular widget screen.
推荐答案
所有问题都解决了.
从小部件树 (materialappwidget) 的根部开始,在导航器上放置一个观察者.
Put an observer on the navigator from the root of the widget tree (materialappwidget).
如果您需要更多解释,请点击此链接:https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
If you need more explanation please follow this link: https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
我已经在我的项目中实现了它的伟大@Sp4Rx
I have implemented in my project and its working great @Sp4Rx
// Register the RouteObserver as a navigation observer.
final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
void main() {
runApp(MaterialApp(
home: Container(),
navigatorObservers: [routeObserver],
));
}
class RouteAwareWidget extends StatefulWidget {
State<RouteAwareWidget> createState() => RouteAwareWidgetState();
}
// Implement RouteAware in a widget's state and subscribe it to
// the
// RouteObserver.
class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {
@override
void didChangeDependencies() {
super.didChangeDependencies();
routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
void dispose() {
routeObserver.unsubscribe(this);
super.dispose();
}
@override
void didPush() {
// Route was pushed onto navigator and is now topmost route.
}
@override
void didPopNext() {
// Covering route was popped off the navigator.
}
@override
Widget build(BuildContext context) => Container();
}
这篇关于如何检查我的小部件屏幕何时在像 Android 中的 onResume 这样的颤动中可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!