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

Libraries

app_events
A Flutter package for observing app lifecycle and network connectivity state.
enums/app_state_enum
enums/network_state_enum
event_bus_listener
observers/app_state_observer
observers/connectivity_state_observer