Created
May 30, 2022 11:53
-
-
Save kaboc/b8e2c50deef7b17d1a9494e4b0e7ee81 to your computer and use it in GitHub Desktop.
Minimal counter example (with Grab + Pot)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:grab/grab.dart'; | |
| import 'package:pot/pot.dart'; | |
| final counterPot = Pot( | |
| () => ValueNotifier(0), | |
| disposer: (notifier) => notifier.dispose(), // This disposer is not called in this example, but I think you get the idea. | |
| ); | |
| void main() => runApp(const App()); | |
| class App extends StatelessWidget { | |
| const App({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| home: Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Counter'), | |
| ), | |
| body: const Center( | |
| child: _CounterText(), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () => counterPot().value++, | |
| child: const Icon(Icons.add), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class _CounterText extends StatelessWidget with Grab { | |
| const _CounterText(); | |
| @override | |
| Widget build(BuildContext context) { | |
| final counter = counterPot(); | |
| final count = context.grab(counter); | |
| return Text('$count'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment