rx_notifier 1.1.0 rx_notifier: ^1.1.0 copied to clipboard
Extension to ValueNotifier by transparently applying functional reactive programming (TFRP)
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:rx_notifier/rx_notifier.dart';
import 'home_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
// home: MyHomePage(title: 'Flutter Demo Home Page'),
home: DefaultTextStyle(style: TextStyle(fontSize: 50), child: HomePage()),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _counter = RxNotifier<int>(0);
int get counter => _counter.value;
set counter(int value) => _counter.value = value;
_incrementCounter() {
counter++;
}
@override
void initState() {
super.initState();
rxObserver(() {
print(counter);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
RxBuilder(
filter: () => counter != 5,
builder: (BuildContext context) {
return Text('$counter');
},
),
CounterWidget(
counter: _counter,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class CounterWidget extends StatelessWidget with RxMixin {
final ValueListenable<int> counter;
@override
bool filter() => counter.value != 3;
CounterWidget({Key? key, required this.counter}) : super(key: key);
@override
Widget build(BuildContext context) {
return Text('${counter.value}');
}
}