flutterx_live_data 1.1.5-dev flutterx_live_data: ^1.1.5-dev copied to clipboard
LiveData is a data holder class that can be observed. This is a Flutter implementation of LiveData in Android Jetpack library
import 'package:flutter/material.dart';
import 'package:flutterx_live_data/flutterx_live_data.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutterx LiveData Demo',
theme: ThemeData(primarySwatch: Colors.teal),
home: const LiveDataExample());
}
class LiveDataExample extends StatefulWidget {
const LiveDataExample({Key? key}) : super(key: key);
@override
State<LiveDataExample> createState() => _LiveDataExampleState();
}
class _LiveDataExampleState extends State<LiveDataExample> {
final MutableLiveData<int> myCounter = MutableLiveData(initialValue: 0);
final MutableLiveData<int> myCounter2 = MutableLiveData(initialValue: 0);
final MutableLiveData<bool> x = MutableLiveData.late();
bool _c = false;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('LiveData example')),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
ElevatedButton(onPressed: () => myCounter2.value++, child: const Text('PRESS')),
ElevatedButton(
onPressed: () => setState(() {
_c = !_c;
x.value = _c;
}),
child: const Text('Invert C')),
LiveDataBuilder<int>(data: myCounter, builder: (context, value) => Text('pressed $value times')),
LiveDataBuilder<int>(data: myCounter2, builder: (context, value) => Text('pressed2 $value times')),
LiveDataBuilder<bool>(
data: x,
builder: (context, value) => Text(value ? 'fab' : 'button'),
placeholder: Container(color: Colors.red, width: 100, height: 100)),
CombinedLiveDataBuilder.with2<int, int, int>(
x1: myCounter,
x2: myCounter2,
transform: (a, b) => a + b,
builder: (context, value) => Text('total: pressed $value times')),
A(c: _c ? myCounter : myCounter2),
]),
),
floatingActionButton: FloatingActionButton(onPressed: () => myCounter.value++, child: const Icon(Icons.add)));
}
class A extends StatefulWidget {
final MutableLiveData<int> c;
const A({Key? key, required this.c}) : super(key: key);
@override
State<StatefulWidget> createState() => _AState();
}
class _AState extends State<A> with StateObserver {
final MutableLiveData<String> _a = MutableLiveData(initialValue: '');
final MutableLiveData<double> _b = MutableLiveData(initialValue: 0);
final MutableLiveData<String> _result = MutableLiveData(initialValue: '');
final LiveList<String> x = LiveList();
@override
void initState() {
super.initState();
x.addAll([]);
Future.delayed(const Duration(seconds: 1), () async {
for (var i = 0; i < 100; i++) {
_a.value = i.toString();
await Future.delayed(const Duration(milliseconds: 300));
_b.value = i * 5;
}
});
}
@override
void registerObservers(BuildContext context) {
observe3<String, double, int>(_a, _b, widget.c, (a, b, c) => _result.value = 'result is [$a, $b, $c]');
}
@override
Widget build(BuildContext context) =>
LiveDataBuilder<String>(data: _result, builder: (context, value) => Text(value));
}