Search packages, repositories and app categories

Package detailsv2.0.1

focus_detector

Detects when your widget appears or disappears from the screen.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
36.7K
Likes
362
Pub points
130

Install package

flutter pub add focus_detector

Package Overview

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

FocusDetector is a Flutter package that detects when a widget gains or loses focus, allowing developers to respond to visibility changes in real time. It provides callbacks for when a widget appears on screen (focus gained) or disappears (focus lost), making it ideal for animations, tracking user interactions, or managing state based on visibility. The package works across all major platforms including Android, iOS, Web, macOS, Windows, and Linux, ensuring consistent behavior regardless of the target environment. It integrates seamlessly into existing Flutter apps with minimal setup.

Use cases

  • Tracking widget visibility for analytics
  • Triggering animations on appearance
  • Managing state based on focus status
  • Optimizing performance by pausing inactive widgets
  • Implementing responsive UIs that react to focus changes

Key features

  • Real-time focus detection
  • Cross-platform compatibility
  • Simple callback-based API
  • Lightweight and easy to integrate
  • Supports both focus gain and loss events

Suitable for

  • Developers building interactive UIs
  • Apps requiring visibility-aware logic
  • Animations triggered by user focus
  • Performance-sensitive applications
  • Widgets needing lifecycle awareness

Considerations

  • Requires proper widget tree structure
  • May not work as expected with complex nested layouts
  • Not suitable for non-interactive widgets
  • Limited to focus-related events only
  • Best used with stateful widgets or controllers

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.',
        );
      },
      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;

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package