lifecycle_mixin 1.0.0 lifecycle_mixin: ^1.0.0 copied to clipboard
This package allows you to manage your project using lifecycle through a Mixin.
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,
),
),
),
),
);
}
}