搜尋套件、儲存庫與 APP 分類

GitHub 儲存庫timobaehr/visibility_aware_state

visibility_aware_state

一個了解其可見性的 StatefulWidgets 的狀態。

visibility_aware_state 專案封面
星標
15
分支
4
最近推送(UTC)
2022年12月7日
專案狀態
未封存

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

visibility_aware_state

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.

Features

Notice when:

  • the app is put in the background or brought out of the background
  • the widget initially becomes visible
  • the widget becomes invisible (a new page is pushed)
  • the widget is removed from stack
  • the widget becomes visible again (e.g. back button on Android is pressed)

Close the current screen via finish() like in Android.

Check if the widget is currently visible using bool isVisible().

Getting started

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.

Example

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();

}

Contributions

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.

Timo Bähr GitHub avatar
GitHub 作者

Timo Bähr ↗

Wolters KluwerMannheim官方網站 ↗
語言Dart

此儲存庫發佈的套件

使用的依賴

依賴清單 2 項

  • flutter{"sdk":"flutter"}
  • flutter_test開發依賴{"sdk":"flutter"}

如何選擇適合專案的儲存庫?

先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。

  1. 透過儲存庫名稱、作者或描述搜尋,再依技術分類縮小範圍。
  2. 綜合星標、分支與最近推送比較專案;星標反映社群關注,不保證品質。
  3. 使用前在 GitHub 確認 README、授權條款與 Issues。

來源與維護

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗