|
1 | | -<!-- |
2 | | -This README describes the package. If you publish this package to pub.dev, |
3 | | -this README's contents appear on the landing page for your package. |
4 | 1 |
|
5 | | -For information about how to write a good package README, see the guide for |
6 | | -[writing package pages](https://dart.dev/tools/pub/writing-package-pages). |
| 2 | +# Pike |
7 | 3 |
|
8 | | -For general information about developing packages, see the Dart guide for |
9 | | -[creating packages](https://dart.dev/guides/libraries/create-packages) |
10 | | -and the Flutter guide for |
11 | | -[developing packages and plugins](https://flutter.dev/to/develop-packages). |
12 | | ---> |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
13 | 9 |
|
14 | | -TODO: Put a short description of the package here that helps potential users |
15 | | -know whether this package might be useful for them. |
| 10 | +`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`. |
16 | 11 |
|
17 | 12 | ## Features |
18 | 13 |
|
19 | | -TODO: List what your package can do. Maybe include images, gifs, or videos. |
| 14 | +- **State management using events**: The state is updated in response to events. |
| 15 | +- **Components for listening to and displaying state**: `PikeBuilder`, `PikeConsumer`, and `PikeProvider` are used to manage and display state changes. |
| 16 | +- **Customizable event handling**: Allows for custom event handlers and selective state rebuilds. |
20 | 17 |
|
21 | | -## Getting started |
| 18 | +## Installation |
22 | 19 |
|
23 | | -TODO: List prerequisites and provide or point to information on how to |
24 | | -start using the package. |
| 20 | +Add `pike` to your `pubspec.yaml` file: |
| 21 | + |
| 22 | +```yaml |
| 23 | +dependencies: |
| 24 | + pike: ^latest_version |
| 25 | +``` |
25 | 26 |
|
26 | 27 | ## Usage |
27 | 28 |
|
28 | | -TODO: Include short and useful examples for package users. Add longer examples |
29 | | -to `/example` folder. |
| 29 | +### Creating a Pike instance |
| 30 | +
|
| 31 | +Create a `Pike` instance by passing an initial state: |
| 32 | + |
| 33 | +```dart |
| 34 | +final class MockPike extends Pike<Event, State> { |
| 35 | + MockPike() : super(InitialState()) { |
| 36 | + on<Event>((event, emit) => switch(event) { |
| 37 | + FetchEvent() => _callback(event, emit), |
| 38 | + FetchWithExceptionEvent() => _callbackA(event, emit), |
| 39 | + }); |
| 40 | +
|
| 41 | + } |
| 42 | +
|
| 43 | + Future<void> _callback(FetchEvent event, Emit<State> emit) async { |
| 44 | + emit(LoadedState()); |
| 45 | + } |
| 46 | +
|
| 47 | + void _callbackA(FetchWithExceptionEvent event, Emit<State> emit) { |
| 48 | + emit(ExceptionState()); |
| 49 | + } |
| 50 | +} |
| 51 | +
|
| 52 | +// Events |
| 53 | +
|
| 54 | +sealed class Event {} |
| 55 | +
|
| 56 | +class FetchEvent extends Event {} |
| 57 | +
|
| 58 | +class FetchWithExceptionEvent extends Event {} |
| 59 | +
|
| 60 | +// States |
| 61 | +
|
| 62 | +sealed class State {} |
| 63 | +
|
| 64 | +class InitialState extends State {} |
| 65 | +
|
| 66 | +class LoadedState extends State {} |
| 67 | +
|
| 68 | +class ExceptionState extends State {} |
| 69 | +``` |
| 70 | + |
| 71 | +### Adding events |
| 72 | + |
| 73 | +Add events to the `Pike` instance to trigger state changes: |
| 74 | + |
| 75 | +```dart |
| 76 | +pike.add(MyEvent()); |
| 77 | +``` |
| 78 | + |
| 79 | +### Listening to state changes |
| 80 | + |
| 81 | +Use `PikeBuilder` or `PikeConsumer` to rebuild your widgets when the state changes: |
| 82 | + |
| 83 | +```dart |
| 84 | +PikeBuilder<MyPike, MyState>( |
| 85 | + builder: (context, state) { |
| 86 | + return Text('Current State: $state'); |
| 87 | + }, |
| 88 | +); |
| 89 | +``` |
| 90 | + |
| 91 | +### Providing the Pike instance |
| 92 | + |
| 93 | +Wrap your widget tree with `PikeProvider` to provide the `Pike` instance to the widgets: |
| 94 | + |
| 95 | +```dart |
| 96 | +PikeProvider<MyPike>( |
| 97 | + pike: pike, |
| 98 | + child: MyWidget(), |
| 99 | +); |
| 100 | +``` |
| 101 | + |
| 102 | +### Custom event handling with `PikeObserver` |
| 103 | + |
| 104 | +You can implement `PikeObserver` to track the lifecycle and events of your `Pike` instances: |
| 105 | + |
| 106 | +```dart |
| 107 | +class MyPikeObserver extends PikeObserver { |
| 108 | + @override |
| 109 | + void onCreate<P extends Pike<Object?, Object?>>(P pike) { |
| 110 | + print("Pike created"); |
| 111 | + } |
| 112 | +
|
| 113 | + @override |
| 114 | + void onEvent<P extends Pike<Object?, Object?>, Event>( |
| 115 | + P pike, |
| 116 | + Event event, |
| 117 | + ) { |
| 118 | + print("Event occurred: $event"); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Components |
| 124 | + |
| 125 | +### `PikeBuilder` |
| 126 | + |
| 127 | +A widget that listens to state changes and rebuilds when the state updates. |
| 128 | + |
| 129 | +```dart |
| 130 | +PikeBuilder<MyPike, MyState>( |
| 131 | + builder: (context, state) { |
| 132 | + return Text('Current State: $state'); |
| 133 | + }, |
| 134 | +); |
| 135 | +``` |
| 136 | + |
| 137 | +### `PikeConsumer` |
| 138 | + |
| 139 | +A widget that listens to state changes and provides a custom listener to trigger side effects. |
| 140 | + |
| 141 | +```dart |
| 142 | +PikeConsumer<MyPike, MyState>( |
| 143 | + builder: (context, state) { |
| 144 | + return Text('Current State: $state'); |
| 145 | + }, |
| 146 | + listener: (context, state) { |
| 147 | + // Handle side effects here |
| 148 | + }, |
| 149 | +); |
| 150 | +``` |
| 151 | + |
| 152 | +### `PikeProvider` |
| 153 | + |
| 154 | +A widget that provides a `Pike` instance to its descendants. |
30 | 155 |
|
31 | 156 | ```dart |
32 | | -const like = 'sample'; |
| 157 | +PikeProvider<MyPike>( |
| 158 | + pike: pike, |
| 159 | + child: MyWidget(), |
| 160 | +); |
33 | 161 | ``` |
34 | 162 |
|
35 | | -## Additional information |
| 163 | +## Codecov |
36 | 164 |
|
37 | | -TODO: Tell users more about the package: where to find more information, how to |
38 | | -contribute to the package, how to file issues, what response they can expect |
39 | | -from the package authors, and more. |
|
0 commit comments