Git Product home page Git Product logo

Comments (5)

yjouyang avatar yjouyang commented on May 4, 2024 4

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

from flutter.widgets.

jamesderlin avatar jamesderlin commented on May 4, 2024 1

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.

from flutter.widgets.

yjouyang avatar yjouyang commented on May 4, 2024

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?

from flutter.widgets.

CivelXu avatar CivelXu commented on May 4, 2024

Is there a good solution to this problem?

from flutter.widgets.

oscarshaitan avatar oscarshaitan commented on May 4, 2024

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

from flutter.widgets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.