Pike
is a state management library for Flutter based on the Event-Driven pattern. It uses event streams to manage state and provides convenient components for interacting with the state, such as PikeBuilder
, PikeConsumer
, and PikeProvider
.
- State management using events: The state is updated in response to events.
- Components for listening to and displaying state:
PikeBuilder
,PikeConsumer
, andPikeProvider
are used to manage and display state changes. - Customizable event handling: Allows for custom event handlers and selective state rebuilds.
Add pike
to your pubspec.yaml
file:
dependencies:
pike: ^latest_version
Create a Pike
instance by passing an initial state:
final class MockPike extends Pike<Event, State> {
MockPike() : super(InitialState()) {
on<Event>((event, emit) => switch(event) {
FetchEvent() => _callback(event, emit),
FetchWithExceptionEvent() => _callbackA(event, emit),
});
}
Future<void> _callback(FetchEvent event, Emit<State> emit) async {
emit(LoadedState());
}
void _callbackA(FetchWithExceptionEvent event, Emit<State> emit) {
emit(ExceptionState());
}
}
// Events
sealed class Event {}
class FetchEvent extends Event {}
class FetchWithExceptionEvent extends Event {}
// States
sealed class State {}
class InitialState extends State {}
class LoadedState extends State {}
class ExceptionState extends State {}
Add events to the Pike
instance to trigger state changes:
pike.add(MyEvent());
Use PikeBuilder
or PikeConsumer
to rebuild your widgets when the state changes:
PikeBuilder<MyPike, MyState>(
builder: (context, state) {
return Text('Current State: $state');
},
);
Wrap your widget tree with PikeProvider
to provide the Pike
instance to the widgets:
PikeProvider<MyPike>(
pike: pike,
child: MyWidget(),
);
You can implement PikeObserver
to track the lifecycle and events of your Pike
instances:
class MyPikeObserver extends PikeObserver {
@override
void onCreate<P extends Pike<Object?, Object?>>(P pike) {
print("Pike created");
}
@override
void onEvent<P extends Pike<Object?, Object?>, Event>(
P pike,
Event event,
) {
print("Event occurred: $event");
}
}
A widget that listens to state changes and rebuilds when the state updates.
PikeBuilder<MyPike, MyState>(
builder: (context, state) {
return Text('Current State: $state');
},
);
A widget that listens to state changes and provides a custom listener to trigger side effects.
PikeConsumer<MyPike, MyState>(
builder: (context, state) {
return Text('Current State: $state');
},
listener: (context, state) {
// Handle side effects here
},
);
A widget that provides a Pike
instance to its descendants.
PikeProvider<MyPike>(
pike: pike,
child: MyWidget(),
);