mu_state 0.3.0+3 mu_state: ^0.3.0+3 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.
MuState
- aValueNotifier
of typeMuEvent<T>
MuBuilder
- aValueListenableBuilder
of typeMuEvent<T>
MuEvent<T>
- the base class for our 3 state objectMuEventData<T>
- the data state of typeT
MuEventError
- the error stateMuEventLoading
- the loading state
MuMultiBuilder
- listen to changes in a list ofMuState
objects
How To #
Declare state as a MuState<T>
variable and pass it an initial MuEvent
type,
e.g. a MuEventLoading
or a MuEventData<T>
.
For more complex state objects, extend MuState
and implement the necessary methods.
Listen to MuState
changes using MuBuilder
.
Listen to multiple MuState
objects using MuMultiBuilder
.
Example #
In test_state.dart
:
import 'package:mu_state/mu_state.dart';
class CounterState extends MuState<int> {
CounterState(super.initValue);
void increment() {
value = MuEventData((value as MuEventData<int>).data + 1);
}
}
final counterState = CounterState(const MuEventData(0));
In main.dart
:
Scaffold(
body: Center(
child: MuBuilder(
state: counterState,
builder: (context, event, child) {
return switch (event) {
MuEventLoading() => const CircularProgressIndicator(),
MuEventError(error: Object error) => Text('Error: $error'),
MuEventData(data: int value) => Text('$value')
};
},
),
),
),
Also see Example.