搜索插件、代码仓与 APP 分类

插件详情v3.1.0+1

focus_detector_v2

检测您的小部件何时出现在屏幕上或从屏幕上消失。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
19
喜欢数
51
Pub 分数
160

安装插件

flutter pub add focus_detector_v2

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

FocusDetectorV2 是一个 Flutter 包,可检测小部件何时获得或失去焦点,从而指示其是否出现在屏幕上。它提供实时的可见性和焦点状态反馈,非常适合根据用户交互或屏幕存在性来管理 UI 行为。该包支持 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台,确保跨平台一致性。它轻量且可无缝集成到现有的 Flutter 项目中。

适合场景

  • 实时跟踪小部件的可见性
  • 基于焦点状态管理动画
  • 通过暂停不活跃的小部件来优化性能
  • 实现关注感知的UI组件
  • 处理多屏幕应用中的屏幕切换

主要特点

  • 实时焦点检测
  • 跨平台兼容性
  • 与现有小部件的简单集成
  • 支持获得和失去焦点事件
  • 轻便高效

适合对象

  • 开发响应式 UI 的开发者
  • 需要基于焦点的逻辑的应用程序
  • 交互式表单和输入字段
  • 需要焦点意识的游戏或媒体播放器
  • 注重可访问性的应用程序

使用建议

  • 在复杂的 widget 树中可能需要仔细处理
  • 不适合用于检测一般的屏幕方向变化
  • 焦点状态可能因不同平台的行为而异
  • 仅限小部件级别的焦点,无法实现应用级别的可见性
  • 确保正确释放以避免内存泄漏

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Focus Detector Logo

Pub.dev Badge GitHub Build Badge Gitter Badge Effective Dart Badge MIT License Badge Flutter Platform Badge

Focus Detector

Get notified every time your widget appears or disappears from the screen.

Similar to onResume()/onPause() on Android and viewDidAppear()/viewDidDisappear() on iOS.

Focus Detector fires callbacks for you whenever something happens to take or give your widget focus. Such an event might be, for instance, the user:

  • Navigating to/from another screen;
  • Turning the device’s screen on/off while your widget is visible;
  • Switching to/from another app while your widget is visible;
  • Scrolling your widget in/out the screen;

Usage

@override
Widget build(BuildContext context) =>
    FocusDetector(
      onFocusLost: () {
        logger.i(
          'Focus Lost.'
          '\nTriggered when either [onVisibilityLost] or [onForegroundLost] '
          'is called.'
          '\nEquivalent to onPause() on Android or viewDidDisappear() on iOS.',
        );
      },
      onFocusGained: () {
        logger.i(
          'Focus Gained.'
          '\nTriggered when either [onVisibilityGained] or [onForegroundGained] '
          'is called.'
          '\nEquivalent to onResume() on Android or viewDidAppear() on iOS.',
        );
      },
      onVisibilityLost: () {
        logger.i(
          'Visibility Lost.'
          '\nIt means the widget is no longer visible within your app.',
        );
      },
      onVisibilityGained: () {
        logger.i(
          'Visibility Gained.'
          '\nIt means the widget is now visible within your app.',
        );
      },
      onForegroundLost: () {
        logger.i(
          'Foreground Lost.'
          '\nIt means, for example, that the user sent your app to the background by opening '
          'another app or turned off the device\'s screen while your '
          'widget was visible.',
        );
      },
      onForegroundGained: () {
        logger.i(
          'Foreground Gained.'
          '\nIt means, for example, that the user switched back to your app or turned the '
          'device\'s screen back on while your widget was visible.',
        );
      },
      isWidgetTest: false,
      child: Container(),
    );

Usage Scenarios

  • Turn on and off resource-consuming features, such as camera, location or bluetooth;
  • Sync your data with a remote API or local database;
  • Pause and resume video/audio playback or streaming;

Migrating from 'focus_detector'

This is a forked version of Focus Detector. It was not updated for more than a year, so I made the V2 for my personal projects. To migrate from the V1, update the pubspec.yml dependency from focus_detector: ^2.0.1 to focus_detector_v2: ^3.1.0 and adjust your import inside dart files.

Contribution

Contributions of any kind are welcome. Feel free to improve the library by creating a pull request or opening an issue.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目