Skip to content

Instantly share code, notes, and snippets.

@Andrious
Created November 19, 2024 01:33
Show Gist options
  • Save Andrious/da73c21c2edff5fdf8ef0bb3bf75c321 to your computer and use it in GitHub Desktop.
Save Andrious/da73c21c2edff5fdf8ef0bb3bf75c321 to your computer and use it in GitHub Desktop.
Counter App using a ChangeNotifier Mixin
//
import 'package:flutter/material.dart';
import 'impl_change_notifier_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: 'Flutter 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 ImplNotifyListenersChangeNotifierMixin {
//
int _counter = 0;
@override
void dispose() {
disposeChangeNotifier();
super.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
setBuilder(
(context) => Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_counter++;
notifyListeners();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment