flutterx_live_data 1.6.0 copy "flutterx_live_data: ^1.6.0" to clipboard
flutterx_live_data: ^1.6.0 copied to clipboard

outdated

LiveData is a data holder class that can be observed. This is a Flutter implementation of LiveData in Android Jetpack library

example/lib/main.dart

// ignore_for_file: public_member_api_docs

import 'package:flutter/material.dart';
import 'package:flutter/rendering.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();

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<MutableLiveData<int>>('c', c));
  }
}

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() {
    observe3<String, double, int>(_a, _b, widget.c, _handleChange);
  }

  void _handleChange(String a, double b, int c) => _result.value = 'result is [$a, $b, $c]';

  @override
  Widget build(BuildContext context) =>
      LiveDataBuilder<String>(data: _result, builder: (context, value) => Text(value));
}
13
likes
0
points
792
downloads

Publisher

unverified uploader

Weekly Downloads

LiveData is a data holder class that can be observed. This is a Flutter implementation of LiveData in Android Jetpack library

Repository (GitLab)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on flutterx_live_data