visibility_aware_state
가시성을 인지하는 StatefulWidgets를 위한 상태입니다.
가시성을 인지하는 StatefulWidgets를 위한 상태입니다.
{"sdk":"flutter"}{"sdk":"flutter"}아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
A state for StatefulWidgets that is aware of its visibility.
That can be useful for screen view tracking or e.g. if your app shows files it can be used to automatically refresh the files when the app is reopened again.
In Android, Activity classes had the onResume() method. In Flutter widgets this is missing. This
library offers void onVisibilityChanged(WidgetVisibility visibility) to do something similar.
Notice when:
Close the current screen via finish() like in Android.
Check if the widget is currently visible using bool isVisible().
In the pubspec.yaml of your flutter project, add the following dependency:
dependencies:
...
visibility_aware_state: <latest_version>
In your library add the following import:
import 'package:visibility_aware_state/visibility_aware_state.dart';
For help getting started with Flutter, view the online documentation.
class Example extends StatefulWidget {
@override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends VisibilityAwareState<Example> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Example app'),
),
body: Center(
child: Text('Welcome to visibility_aware_state example app.'),
),
);
}
@override
void onVisibilityChanged(WidgetVisibility visibility) {
// TODO: Use visibility
super.onVisibilityChanged(visibility);
}
}
Here is a specific example where the visibility is used for screen view tracking:
import 'package:flutter/widgets.dart';
import 'package:visibility_aware_state/visibility_aware_state.dart';
import 'tracker.dart';
abstract class ScreenState<T extends StatefulWidget>
extends VisibilityAwareState<T> with Screen {
@override
void onVisibilityChanged(WidgetVisibility visibility) {
if (visibility == WidgetVisibility.VISIBLE) {
Tracker().trackScreen(this);
}
super.onVisibilityChanged(visibility);
}
@override
String screenName() {
return runtimeType.toString();
}
}
mixin Screen {
String screenName();
}
Feel free to contribute to this project.
If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue. If you fixed a bug or implemented a new feature, please send a pull request.