streamable_state 0.0.2 copy "streamable_state: ^0.0.2" to clipboard
streamable_state: ^0.0.2 copied to clipboard

A simple state management solution made from two classes

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:streamable_state/streamable.dart';

void main() => runApp(const MyApp());

/// Launching widget
class MyApp extends StatefulWidget {
  /// Default constructor
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  /// The class responsible for storing the data. Can be stored anywhere.
  final controller = CountController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextButton(
                onPressed: controller.add,
                style: ButtonStyle(
                  padding: MaterialStateProperty.resolveWith(
                    (states) => const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                  ),
                ),
                child: const Text("add"),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),

                /// The widget to handle the state updates
                child: Streamed(
                  streamable: controller.count,
                  builder: (value) {
                    return Text(value.toString());
                  },
                ),
              ),
              TextButton(
                onPressed: controller.subtract,
                style: ButtonStyle(
                  padding: MaterialStateProperty.resolveWith(
                    (states) => const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                  ),
                ),
                child: const Text("sub"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class CountController {
  /// the value to observe, can also be anything
  final count = Streamable(0);

  void add([int? i]) {
    final newVal = i ?? 1;
    count.value += newVal;
  }

  void subtract([int? i]) {
    final newVal = i ?? 1;
    count.value -= newVal;
  }
}
2
likes
150
points
9
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A simple state management solution made from two classes

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on streamable_state