Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check when my widget screen comes to visibility in flutter like onResume in Android

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.

like image 589
Sp4Rx Avatar asked Sep 09 '19 14:09

Sp4Rx


People also ask

How do you get the position of a widget in Flutter?

For the widget's offset position, call RenderBox 's localToGlobal method whose return type is Offset . The Offset class has some properties. For getting the offset position in x and y axis, you can read the dx and dy properties respectively. The returned offset is the top left position of the widget.

How do I show widgets from another widget in Flutter?

In Flutter, the overlay lets you print visual elements on top of other widgets by inserting them into the overlay's stack. You insert a widget into the overlay using an OverlayEntry and you use Positioned and AnimatedPositioned to choose where the entry is positioned within the overlay.

How to show or hide a widget in flutter?

In Flutter, it tends to be done effectively utilizing Visibility a widget. The widget you need to show or hide must be the child of Visibility widget. In the constructor, pass visibility alternative whose value is a boolean and is put away as the state. Then, at that point, update the value to show or hide the child.

Why do I see empty space when I insert a widget?

This limits your ability to interact with the widget and hides it but will keep it in place. So, you will see empty space wherever the widget is inserted. For Offscreen: we wrap the widget in an Offstage widget which will render the widget off screen so that it won’t be visible and it won’t take up any screen real estate.

What does it mean when a widget is rendered off screen?

Rendered off screen. Is not visible and is not interactive. It will still incur a cost but it is generally OK because it is useful for doing widget size calculations where you don’t need to show the widget all of the time.

How do I hide a widget from the screen?

If you need to keep it in place and do a swap or something similar then you can use an Invisible strategy and toggle the visibility as needed. If you need to eliminate the “white space”, then you can use Offscreen. Do I need to still factor in the size of my widget even if it isn’t visible?


2 Answers

I struggled to get a video to pause when not viewing the main screen of my app. I applied this VisibilityDetector and grabbed the visiblePercentage to force a pause or resume:

VisibilityDetector(     key: Key('visible-video--key-${this.randomkeygenerator}-1'),     onVisibilityChanged: (visibilityInfo) {       var visiblePercentage = visibilityInfo.visibleFraction * 100;        if (visiblePercentage < 1){ //the magic is done here         if(_video_controller != null) {           if(disposed_vid == false) {             _video_controller.pause();           }         }        }else{         if(_video_controller != null) {           if(disposed_vid == false) {             _video_controller.play();           }         }       }       debugPrint(           'Widget ${visibilityInfo.key} is ${visiblePercentage}% visible');     },     child: VideoPlayer(_video_controller)),     @override   void dispose() {     // If the video is playing, pause it.     _video_controller .pause();     _video_controller .dispose();     disposed_vid = true;     super.dispose();   } 
like image 39
Petro Avatar answered Oct 08 '22 04:10

Petro


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

// 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(); } 
like image 95
Amit Avatar answered Oct 08 '22 02:10

Amit