flutter_app_events 0.0.2 copy "flutter_app_events: ^0.0.2" to clipboard
flutter_app_events: ^0.0.2 copied to clipboard

A Flutter package to listen for app lifecycle events and network connectivity changes, and propagate them using your preferred state management solution.

flutter_app_events #

flutter_app_events is a simple Flutter package to detect app lifecycle events (like foreground, background, paused) and network connectivity changes, then propagate these events through your app with your own state management (e.g. BLoC, Provider).


🚀 Features #

  • ✅ Listen for app lifecycle state changes:
    • foreground, background, resumed, paused
  • ✅ Listen for network connectivity changes:
    • connected, disconnected
  • ✅ Simple, widget-first approach with EventBusListener

📦 Installation #

Add to your pubspec.yaml:

dependencies:
  flutter_app_events:

🧩 Usage #

1️⃣ Wrap your app with EventBusListener #

Important:
✅ Use only one EventBusListener in your widget tree.
✅ Propagate its events using BLoC, Provider, Riverpod, or any other state management solution.

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_app_events/app_events.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App Events Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: MultiBlocProvider(
        providers: [
          BlocProvider(create: (_) => AppStateBloc()),
          BlocProvider(create: (_) => InternetConnectionBloc()),
        ],
        child: const AppWidget(),
      ),
    );
  }
}

class AppWidget extends StatelessWidget {
  const AppWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return EventBusListener(
      onAppEvent: (state) {
        print("App state changed: $state");
        switch (state) {
          case AppState.background:
            context.read<AppStateBloc>().add(AppBackgroundEvent());
            break;
          case AppState.paused:
            context.read<AppStateBloc>().add(AppPausedEvent());
            break;
          case AppState.resumed:
            context.read<AppStateBloc>().add(AppResumedEvent());
            break;
          case AppState.foreground:
            context.read<AppStateBloc>().add(AppForegroundEvent());
            break;
        }
      },
      onConnectivityEvent: (state) {
        print("Network state changed: $state");
        switch (state) {
          case NetworkState.connected:
            context.read<InternetConnectionBloc>().add(InternetConnectedEvent());
            break;
          case NetworkState.disconnected:
            context.read<InternetConnectionBloc>().add(InternetDisconnectedEvent());
            break;
        }
      },
      child: const Dashboard(),
    );
  }
}

⚡ Best Practice #

Use only one EventBusListener widget per app.

Propagate state changes via BLoC, Provider, Riverpod, or your preferred state management to avoid duplicate listeners and ensure predictable state flow.

✨ Author #

Umar Manzoor@mybringback22

3
likes
150
points
24
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter package to listen for app lifecycle events and network connectivity changes, and propagate them using your preferred state management solution.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

connectivity_plus, flutter

More

Packages that depend on flutter_app_events