streamable_state 0.0.1
streamable_state: ^0.0.1 copied to clipboard
A simple state management solution made from two classes
Streamable #
A simple, barebones StreamBuilder-based state management solution. Refer to the code below to understand how to use this package
// ignore_for_file: public_member_api_docs
import 'package:flutter/material.dart';
import 'package:mojsterapp_flutter/streamable/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;
}
}