Last active
June 19, 2024 12:15
-
-
Save evaisse/4359d087db04d4c5318e35ade36cc8e3 to your computer and use it in GitHub Desktop.
Example of ValueNotifier & ValueListenerBuilder to replace Rx from Getx.
This file contains 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'; | |
/// | |
/// @see https://api.flutter.dev/flutter/widgets/ValueListenableBuilder-class.html | |
/// @see https://api.flutter.dev/flutter/foundation/ValueNotifier-class.html | |
/// | |
ValueNotifier<int> counter = ValueNotifier<int>(0); | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar( | |
title: const Text('ValueNotifier<T>() sample'), | |
), | |
body: ValueListenableBuilder<int>( | |
valueListenable: counter, | |
builder: (BuildContext context, int value, child) { | |
return Scaffold( | |
body: Center( | |
child: Text('count: ${counter.value} ou $value'), | |
), | |
); | |
}), | |
floatingActionButton: FloatingActionButton( | |
child: const Icon(Icons.plus_one), | |
onPressed: () => counter.value += 1, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment