Skip to content

Instantly share code, notes, and snippets.

@MCarlomagno
Last active January 12, 2022 19:49
Show Gist options
  • Save MCarlomagno/4a918d3b698874496b8886628cabdc43 to your computer and use it in GitHub Desktop.
Save MCarlomagno/4a918d3b698874496b8886628cabdc43 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
// Global reactive value
final counter = ValueNotifier<int>(0);
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) => Column(
children: [
ElevatedButton(
onPressed: () => counter.value++,
child: Text('Increase'),
),
// ValueListenableBuilder allows the
// state change in the child widget
// in this case the child is just a Text
ValueListenableBuilder(
valueListenable: counter,
builder: (context, value, widget) {
return Text(counter.toString());
},
),
ElevatedButton(
onPressed: () => counter.value--,
child: Text('Decrease'),
),
],
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment