Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VisibilityDetector not working with AppLifecycleState #53

Open
yjouyang opened this issue Jan 17, 2020 · 6 comments
Open

VisibilityDetector not working with AppLifecycleState #53

yjouyang opened this issue Jan 17, 2020 · 6 comments
Labels
enhancement New feature or request p: visibility_detector Related to package:visibility_detector

Comments

@yjouyang
Copy link

When my app go to background or come back to foreground, nothing is called in VisibilityDetector. Is there any way to do this?

@yjouyang
Copy link
Author

I've done something like this:

extension VisibilityDetector on Widget {
  Widget toVisibilityDetector(String key, Function(bool visible) callback) {
    final appLifecycleObserver = _AppLifecycleObserver(callback);
    var visible = false;
    return google.VisibilityDetector(
      key: Key(key),
      child: this,
      onVisibilityChanged: (google.VisibilityInfo info) {
        final isNowVisible = info.visibleFraction > 0;
        if (isNowVisible == visible) {
          return;
        }
        visible = isNowVisible;
        callback(visible);
        if (visible) {
          WidgetsBinding.instance.addObserver(appLifecycleObserver);
        } else {
          WidgetsBinding.instance.removeObserver(appLifecycleObserver);
        }
      },
    );
  }
}

class _AppLifecycleObserver extends WidgetsBindingObserver {

  final Function(bool visible) callback;

  _AppLifecycleObserver(this.callback);

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    callback(state == AppLifecycleState.resumed);
  }
}

Any suggestions?

@jamesderlin jamesderlin added the enhancement New feature or request label Feb 5, 2020
@jamesderlin
Copy link
Collaborator

Sorry, I somehow missed this when it was filed.

VisibilityDetector reports only in-app changes to widget visibility, not changes due to the entire application going to the background or returning to the foreground. You will need to deal with AppLifecycleState changes yourself. (That said, your approach of creating a _AppLifecycleObserver object and an extra callback per VisibilityDetector seems wasteful. A better approach might be to keep track of which VisibilityDetectors are expected to be visible and have a single AppLifecycleState observer trigger callbacks for all of them.)

I'm currently leaning toward leaving this as the application's responsibility, but I'll have to think about this for a bit. Feel free to convince me in either direction.

@yjouyang
Copy link
Author

People usually want to detect the visibility of view because they want to reload data when the view becomes visible. So whether the view becomes visible because the application come to foreground or because it is added to the widget tree, they are not so different, in which case data should be reloaded

@jamesderlin jamesderlin added the p: visibility_detector Related to package:visibility_detector label Apr 16, 2020
@CivelXu
Copy link

CivelXu commented Feb 3, 2021

Is there a good solution to this problem?

@oscarshaitan
Copy link

you should use
didChangeAppLifecycleState
on the StatefulWidget

that works fine in my case to controll a stream, is pretty well explained here
https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver-class.html

@jillellamudisurya
Copy link

@jamesderlin This thing might need to add in the documentation, because everyone will think that the visibility will get reported if app went to background state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p: visibility_detector Related to package:visibility_detector
Projects
None yet
Development

No branches or pull requests

5 participants