bon_notifiers 2.0.0
bon_notifiers: ^2.0.0 copied to clipboard
A collection of custom notifiers and notifier mixins
import 'dart:async';
import 'package:bon_notifiers/bon_notifiers.dart';
import 'package:flutter/material.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),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
///The default counter app rewritten to work with async notifier
///
///This will show a counter, that counts up with a delay of two seconds between calls.
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _counter = AsyncValueNotifier<int>(data: 0);
void _incrementCounter() {
_counter.update((value) async {
await Future.delayed(Duration(seconds: 2));
if (value == 10) throw Exception('Value was 10');
return value + 1;
});
}
@override
void dispose() {
_counter.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return 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:'),
AsyncValueListenableBuilder(
asyncListenable: _counter,
resultBuilder: (context, count, _) {
return Text(
'$count',
style: Theme.of(context).textTheme.headlineMedium,
);
},
errorBuilder: (context, error, _) {
return Text('An error occured: $error');
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}