lifecycle_aware_state 0.0.4 copy "lifecycle_aware_state: ^0.0.4" to clipboard
lifecycle_aware_state: ^0.0.4 copied to clipboard

Lifecycle callbacks to be aware of different events in the route navigation and state lifecycle in a flutter project

lifecycle_aware_state #

pub package

Lifecycle callbacks to be aware of different events in the route navigation and state lifecycle in a flutter project

Usage #

  1. To use this plugin, add lifecycle_aware_state as a dependency in your pubspec.yaml file.
dependencies:
  lifecycle_aware_state: ^lastVersion
  1. Add navigatorObservers: [LifecycleAwareState.routeObserver] to your MaterialApp object
class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ...
      navigatorObservers: [LifecycleAwareState.routeObserver], // register routeObserver with flutter navigator
      ...
    );
  }
}
  1. Configure flags before flutter bootstrap your app
void main() {
  LifecycleAwareState.logClassSuffixes = ['Page']; // log classes with name ending 'Page'
  LifecycleAwareState.logSuffixedClassesOnly = true; // enable log only for suffixed classes defined above
  runApp(const MyApp());
}
  1. now extend your State class from LifecycleAwareState
class _MyHomePageState extends LifecycleAwareState<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(title: const Text('LifecycleAwareState Demo')),
      body: const Center(
        child: Text('Hello Lifecycle'),
      ),
    );
  }
}

Full Example #

import 'package:flutter/material.dart';
import 'package:lifecycle_aware_state/lifecycle_aware_state.dart';

void main() {
  LifecycleAwareState.logClassSuffixes = ['Page'];
  LifecycleAwareState.logSuffixedClassesOnly = true;
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'LifecycleAwareState Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      navigatorObservers: [LifecycleAwareState.routeObserver],
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends LifecycleAwareState<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(title: const Text('LifecycleAwareState Demo')),
      body: const Center(
        child: Text('Hello Lifecycle'),
      ),
    );
  }

  @override
  void onPause() {
    // Widget is not visible to the user
    super.onPause();
  }

  @override
  void onResume() {
    // Widget becomes visible to the user
    super.onResume();
  }

  @override
  void onReady() {
    // Widget is now built and visible
    super.onReady();
  }

  @override
  void didPop() {
    // This route is popped from navigator
    super.didPop();
  }

  @override
  void didPopNext() {
    // top route has been popped off, and the current route shows up
    super.didPopNext();
  }

  @override
  void didPush() {
    // Called when the current route has been pushed.
    super.didPush();
  }

  @override
  void didPushNext() {
    // Called when a new route has been pushed, and the current route is no longer visible.
    super.didPushNext();
  }
}
7
likes
160
pub points
58%
popularity

Publisher

verified publisherbuildtoapp.com

Lifecycle callbacks to be aware of different events in the route navigation and state lifecycle in a flutter project

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on lifecycle_aware_state