Skip to content

Instantly share code, notes, and snippets.

@kaboc
Created May 30, 2022 11:53
Show Gist options
  • Select an option

  • Save kaboc/b8e2c50deef7b17d1a9494e4b0e7ee81 to your computer and use it in GitHub Desktop.

Select an option

Save kaboc/b8e2c50deef7b17d1a9494e4b0e7ee81 to your computer and use it in GitHub Desktop.
Minimal counter example (with Grab + Pot)
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