Skip to content

Instantly share code, notes, and snippets.

@kaboc
Last active September 9, 2023 14:32
Show Gist options
  • Select an option

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

Select an option

Save kaboc/c9695dac00b9a3410417cecb865a707f to your computer and use it in GitHub Desktop.
Minimal counter example (with Grab)
import 'package:flutter/material.dart';
import 'package:grab/grab.dart';
final counter = ValueNotifier(0);
void main() => runApp(const App());
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: const Center(
child: _CounterText(),
),
floatingActionButton: FloatingActionButton(
onPressed: () => counter.value++,
child: const Icon(Icons.add),
),
),
);
}
}
class _CounterText extends StatelessWidget with Grab {
const _CounterText();
@override
Widget build(BuildContext context) {
final count = context.grab(counter);
return Text('$count');
}
}
@kaboc
Copy link
Author

kaboc commented Dec 7, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment