Search packages, repositories and app categories

Package detailsv3.1.0+1

focus_detector_v2

Detects when your widget appears or disappears from the screen.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
19
Likes
51
Pub points
160

Install package

flutter pub add focus_detector_v2

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

FocusDetectorV2 is a Flutter package that detects when a widget gains or loses focus, indicating whether it appears or disappears from the screen. It provides real-time feedback on visibility and focus state, making it ideal for managing UI behavior based on user interaction or screen presence. The package supports multiple platforms including Android, iOS, Web, macOS, Windows, and Linux, ensuring cross-platform consistency. It's lightweight and integrates seamlessly into existing Flutter projects.

Use cases

  • Tracking widget visibility in real time
  • Managing animations based on focus state
  • Optimizing performance by pausing inactive widgets
  • Implementing focus-aware UI components
  • Handling screen transitions in multi-screen apps

Key features

  • Real-time focus detection
  • Cross-platform compatibility
  • Simple integration with existing widgets
  • Supports both gain and loss of focus events
  • Lightweight and efficient

Suitable for

  • Developers building responsive UIs
  • Apps requiring focus-based logic
  • Interactive forms and input fields
  • Games or media players needing focus awareness
  • Accessibility-focused applications

Considerations

  • May require careful handling in complex widget trees
  • Not suitable for detecting general screen orientation changes
  • Focus state may vary across different platform behaviors
  • Limited to widget-level focus, not app-level visibility
  • Ensure proper disposal to avoid memory leaks

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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.

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package