Last active
January 12, 2022 19:49
-
-
Save MCarlomagno/4a918d3b698874496b8886628cabdc43 to your computer and use it in GitHub Desktop.
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'; | |
// 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