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.
My approach was to play the video in didUpdateWidget
but didUpdateWidget
is called every-time even the widget screen is not visible.
Note: I'm not asking about didChangeAppLifecycleState
from WidgetsBindingObserver
as it gives onResume
etc callbacks for the app lifecycle not a particular widget screen.
It's probably not the simplest and definitely not perfect, but a while back I implemented events like those with routes. Basically,
EventRoute<T>
is a drop-in replacement forMaterialPageRoute<T>
that provides optional callbacks for when the Widget is created, pushed to the foreground, pushed to the background and when it gets popped off.event_route.dart:
And then to push your route you do:
The context is a little icky though...
I struggled to get a video to pause when not viewing the main screen of my app. I applied this
VisibilityDetector
and grabbed thevisiblePercentage
to force a pause or resume:All of the problems are solved.
Put an observer on the navigator from the root of the widget tree (materialappwidget).
If you need more explanation please follow this link: https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
I have implemented in my project and its working great @Sp4Rx