lifecycle_mixin 1.0.0 copy "lifecycle_mixin: ^1.0.0" to clipboard
lifecycle_mixin: ^1.0.0 copied to clipboard

This package allows you to manage your project using lifecycle through a Mixin.

example/lib/main.dart

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Lifecycle Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

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

class _MyHomePageState extends State<MyHomePage> with LifecycleMixin {
  @override
  void initState() {
    setName('Home');
    super.initState();
  }

  @override
  Widget onBuild() {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Material(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text(
                'Send the app to the background or push another page and '
                'watch the console.',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 20,
                ),
              ),
              const SizedBox(
                height: 20,
              ),
              ElevatedButton(
                onPressed: () {
                  final route = MaterialPageRoute(
                    builder: (_) => const OtherPage(),
                  );
                  Navigator.of(context).push(route);
                },
                child: const Text(
                  'PUSH ANOTHER PAGE',
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

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

  @override
  State<OtherPage> createState() => _OtherPageState();
}

class _OtherPageState extends State<OtherPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: const Padding(
        padding: EdgeInsets.all(16),
        child: Center(
          child: Text(
            'Look at the console and return to the first screen.',
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 20,
            ),
          ),
        ),
      ),
    );
  }
}
2
likes
140
pub points
51%
popularity

Publisher

unverified uploader

This package allows you to manage your project using lifecycle through a Mixin.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, visibility_detector

More

Packages that depend on lifecycle_mixin