Search packages, repositories and app categories

Package detailsv1.0.5

visibility_aware_state

A state for StatefulWidgets that is aware of its visibility.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
1.1K
Likes
34
Pub points
150

Install package

flutter pub add visibility_aware_state

Package Overview

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

A Flutter package that extends StatefulWidget to manage state based on widget visibility. It automatically detects when a widget becomes visible or hidden in the viewport, enabling efficient resource management and optimized performance. Ideal for animations, video playback, or data fetching that should only occur when the user can see the widget. The package integrates seamlessly with existing StatefulWidgets without requiring significant code changes.

Use cases

  • Optimizing animations based on visibility
  • Managing video or audio playback
  • Lazy loading content
  • Reducing CPU usage for off-screen widgets
  • Handling scroll-based triggers

Key features

  • Automatic visibility detection
  • Minimal boilerplate
  • Compatible with existing StatefulWidgets
  • Supports all major platforms
  • Efficient resource management

Suitable for

  • Developers optimizing app performance
  • Apps with scrollable lists or carousels
  • Media-heavy applications
  • Apps needing lazy loading

Considerations

  • May not work as expected in complex nested layouts
  • Requires careful handling in custom render objects
  • Limited to visibility within the viewport
  • Not suitable for non-UI state tracking
  • Depends on correct widget tree structure

Original README

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

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.

Categories

Source repository

Projects using this package

No data available yet.