watch_x 0.0.1
watch_x: ^0.0.1 copied to clipboard
A lightweight reactive state management for Flutter with fine-grained field watchers.
import 'package:flutter/material.dart';
import 'package:watch_x/watch_x.dart'; // -> import
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
// initialise your watchers with default values
WatchX watchx = WatchX({
"counter": 0,
// add more watchers here as needed
});
void _incrementCounter() {
// increament the counter
watchx.watch("counter")!.value++;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
// listen for a state change of the watchers
child: XListenableBuilder(
watchers: watchx.watchers,
builder: (context, values) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text(
'${values["counter"]}', // access the counter value
style: Theme.of(context).textTheme.headlineMedium,
),
],
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}