Comments (5)
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.
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 VisibilityDetector
s 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.
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.
Is there a good solution to this problem?
from flutter.widgets.
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)
- ScrollablePositionedList has the same issue, Re-add the ability to return null
- SliverVisibiltyDetector reports a SliverPersistentHeader as not visible when pinned
- [ScrollablePositionedList] - How to know the currentIndex?
- ItemScrollController.scrollTo() allows negative index input
- Nested listing performance issue ! HOT 1
- SliverVisibiltyDetector should consider SliverConstraints overlap HOT 1
- ItemScrollController attached HOT 1
- VisibilityDetector fires event unexpectedly HOT 1
- [ScrollablePositionedList] Scrollbar unexpected jumping HOT 2
- [scrollable_positioned_list]: add jumpTo method to ScrollOffsetController
- [ScrollablePositionedList] PageStorage cast bug
- [ScrollablePositionedList] with StickyHeader
- Unsupported operation: add
- Twitchy scroll animation
- Inconsistent Expansion Direction of AnimatedContainer in ScrollablePositionedList
- [visibility_detector] Incorrect Visibility info Bug after updating to 4.0.0 or later
- A RenderViewport exceeded its maximum number of layout cycles
- R
- RTL support scrollable_positioned_list HOT 1
- Allow or expose scroll controller to be used outside.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter.widgets.