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

套件詳情v1.0.5

visibility_aware_state

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

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1140
喜歡數
34
Pub 分數
150

安裝套件

flutter pub add visibility_aware_state

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 套件,擴展 StatefulWidget 以根據元件可見性來管理狀態。它能自動偵測元件何時進入或離開視口,進而實現高效資源管理與最佳化效能。非常適合動畫、影片播放或資料擷取,這些操作僅在使用者可看見元件時才應執行。此套件可無縫整合至現有的 StatefulWidgets 中,無需大幅修改程式碼。

適用情境

  • 根據可見性優化動畫
  • 管理影片或音訊播放
  • 懶加載內容
  • 減少離屏小部件的CPU使用率
  • 處理基於滾動的觸發器

主要特色

  • 自動可見性檢測
  • 最小化樣板程式碼
  • 與現有 StatefulWidgets 相容
  • 支援所有主要平台
  • 高效資源管理

適合對象

  • 開發者優化應用效能
  • 具有可滾動列表或幻燈片輪播的應用程式
  • 媒體密集型應用
  • 需要懶加載的應用

使用建議

  • 在複雜的嵌套佈局中可能無法按預期運作
  • 在自定渲染物件中需要謹慎處理
  • 僅限於視口內可見
  • 不適合用於非UI狀態追蹤
  • 依賴於正確的 widget 樹結構

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

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.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。