Skip to content

Instantly share code, notes, and snippets.

@IsmailAlamKhan
Created September 19, 2021 17:50
Show Gist options
  • Save IsmailAlamKhan/b05011ced827e879163a2b69a58cee58 to your computer and use it in GitHub Desktop.
Save IsmailAlamKhan/b05011ced827e879163a2b69a58cee58 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) =>const MaterialApp(title: 'Material App', home: Home());
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
late final notifier = SomeNotifier();
void listner() {
setState(() {});
print('Notifier called notifyListeners');
}
@override
void initState() {
super.initState();
notifier.addListener(listner);
}
@override
void dispose() {
notifier.removeListener(listner);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(child: Text('Count: ${notifier.count}')),
floatingActionButton: FloatingActionButton(child: const Icon(Icons.add), onPressed: notifier.increment),
);
}
}
class SomeNotifier extends ChangeNotifier {
int _count = 0;
int get count => _count;
set count(int value) {
_count = value;
notifyListeners();
}
void increment() => count++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment