mu_state 0.4.0+4 mu_state: ^0.4.0+4 copied to clipboard
A set of helpers for pragmatic state handling as mentioned in my Medium article.
mu_state #
A minimal state solution based on my Pragmatic state handling in Flutter Medium article .
Features #
A set of classes built on ValueNotifier
and ValueListenableBuilder
for
handling state in Flutter.
MuLogic
- an alias forValueNotifier
where you can optionally use aMuState<T>
typeMuBuilder
- an alias forValueListenableBuilder
MuEvent<T>
- the base class for 3 state object which can be used in aMuState<T>
:MuEventData<T>
- the data state of typeT
MuEventError
- the error stateMuEventLoading
- the loading state
MuMultiBuilder
- listen to changes of a list ofListenable
objects
How To #
Contain state by inheriting from MuLogic
or using it directly. This can have any type.
You can use an optional MuEvent
type MuLoading
, MuError
or MuData
to contain the state.
Listen to MuLogic
changes using MuBuilder
.
Listen to multiple MuLogic
objects using MuMultiBuilder
.
Example #
In test_state.dart
:
import 'package:mu_state/mu_state.dart';
class CounterLogic extends MuState<int> {
CounterLogic(super.initValue);
void increment() {
value = value + 1;
}
}
final counterState = CounterState(0);
In main.dart
:
Scaffold(
body: Center(
child: MuBuilder(
state: counterState,
builder: (context, event, child) {
return Text('$value');
},
),
),
),
See more examples in Example.