搜索插件、代码仓与 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.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。