Skip to content

Instantly share code, notes, and snippets.

@evaisse
Last active June 19, 2024 12:15
Show Gist options
  • Save evaisse/4359d087db04d4c5318e35ade36cc8e3 to your computer and use it in GitHub Desktop.
Save evaisse/4359d087db04d4c5318e35ade36cc8e3 to your computer and use it in GitHub Desktop.
Example of ValueNotifier & ValueListenerBuilder to replace Rx from Getx.
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