You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-29Lines changed: 50 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,6 @@
41
41
Define custom state classes, then use them in your Trent state manager:
42
42
43
43
```dart
44
-
45
44
//
46
45
// Classes A, B, and C defined here
47
46
//
@@ -87,9 +86,9 @@ class AuthTrent extends Trent<AuthTypes> {
87
86
88
87
// Map over the current state and do things based on the type
89
88
// (not all routes need to be defined)
90
-
currStateMapper
91
-
..all((state) {
92
-
// Do something
89
+
stateMap
90
+
..orElse((state) {
91
+
// Do something (doElse run if nothing else more specific hit)
93
92
})
94
93
..as<A>((state) {
95
94
// Do something
@@ -102,7 +101,7 @@ class AuthTrent extends Trent<AuthTypes> {
102
101
});
103
102
104
103
// Simply access the raw state for custom manipulation
105
-
print(currState);
104
+
print(state);
106
105
}
107
106
108
107
/// ... More business functions ...
@@ -113,26 +112,48 @@ class AuthTrent extends Trent<AuthTypes> {
113
112
114
113
### UI Layer: Built-in Widgets
115
114
116
-
-`Alerter` widget that listens to one-time state `alert(...)`s from your business logic layer. This is good if your business logic needs to "quickly send off a state without saving it". An example would be you having `Loading`, `Data`, and `WarningNotification` states. You may be in `Data` state, but want to send off a quick `WarningNotification` state without having to throw away your `Data` state. This is what an `alert(WarningNotification(...))` is good for.
115
+
-`Alerter` widget that listens to one-time state `alert(...)`s from your business logic layer in `listenAlerts`. This is good if your business logic needs to "quickly send off a state without saving it". An example would be you having `Loading`, `Data`, and `WarningNotification` states. You may be in `Data` state, but want to send off a quick `WarningNotification` state without having to throw away your `Data` state. This is what an `alert(WarningNotification(...))` is good for. `Alerter` can also can listen to regular state updates in `listenStates`. Both can have their listeners programmatically toggled on/off with `listenAlertsIf` and `listenStatesIf` respectively.
117
116
118
117
```dart
119
118
// AuthTrent is where your business logic is defined, AuthTrentTypes is
120
119
// the type all your business logic types extend from (in this example `A`, `B`, and `C` states)
121
120
Alerter<AuthTrent, AuthTrentTypes>(
122
121
// Not all handlers need to be defined
123
-
handlers: (mapper) => mapper
124
-
..all((state) {
125
-
// Always called if defined
126
-
})
127
-
..as<A>((state) {
128
-
// Called if `A` is alerted
129
-
})
130
-
..as<B>((state) {
131
-
// Called if `B` is alerted
132
-
})
133
-
..as<C>((_) {
134
-
// Called if `C` is alerted
135
-
}),
122
+
//
123
+
// This only listens to alerts
124
+
listenAlerts: (mapper) => mapper
125
+
..orElse((state) {
126
+
// Triggered if nothing more specific is defined
127
+
})
128
+
..as<A>((state) {
129
+
// Called if `A` is alerted
130
+
})
131
+
..as<B>((state) {
132
+
// Called if `B` is alerted
133
+
})
134
+
..as<C>((_) {
135
+
// Called if `C` is alerted
136
+
}),
137
+
// Not all handlers need to be defined
138
+
//
139
+
// This only listens to states emitted
140
+
listenStates: (mapper) => mapper
141
+
..orElse((state) {
142
+
// Triggered if nothing more specific is defined
143
+
})
144
+
..as<A>((state) {
145
+
// Called if `A` is alerted
146
+
})
147
+
..as<B>((state) {
148
+
// Called if `B` is alerted
149
+
})
150
+
..as<C>((_) {
151
+
// Called if `C` is alerted
152
+
}),
153
+
154
+
// Only trigger listens if...
155
+
listenAlertsIf: (oldAlert, newAlert) => true,
156
+
listenStatesIf: (oldState, newState) => true,
136
157
child: Container(),
137
158
);
138
159
```
@@ -144,9 +165,9 @@ class AuthTrent extends Trent<AuthTypes> {
144
165
// the type all your business logic types extend from (in this example `A`, `B`, and `C` states)
145
166
Digester<AuthTrent, AuthTrentTypes>(
146
167
// Not all handlers need to be defined
147
-
handlers: (mapper) {
168
+
child: (mapper) {
148
169
mapper
149
-
..all((state) => const Text("Rendered if no more specific type is defined"))
170
+
..orElse((state) => const Text("Rendered if no more specific type is defined"))
150
171
..as<A>((state) => Text("State is A"))
151
172
..as<B>((state) => const Text("State is B"))
152
173
..as<C>((state) => const Text("State is C"));
@@ -182,7 +203,7 @@ class AuthTrent extends Trent<AuthTypes> {
182
203
}
183
204
```
184
205
185
-
-`alert(state)`: Alert a temporary state WITHOUT setting it, but being able to listen to it from the `Alerter` widget (for things like notifications).
206
+
-`alert(state)`: Alert a temporary state WITHOUT setting/saving it, but being able to listen to it from the `Alerter` widget (for things like notifications).
186
207
187
208
```dart
188
209
class CalculatorTrent extends Trent<CalculatorStates> {
@@ -195,7 +216,7 @@ class AuthTrent extends Trent<AuthTypes> {
195
216
}
196
217
```
197
218
198
-
-`getExStateAs<T>()`: This will return the last state of type `T`. Useful for accessing a state you transitioned away from.
219
+
-`getExStateAs<T>()`: This will return the last state of type `T`. Useful for accessing a state you transitioned away from. For example, if you transitioned from `Division` to `Multiplication`, you can still access the last value of the `Division` state after transitioning away from it.
199
220
200
221
```dart
201
222
class CalculatorTrent extends Trent<CalculatorStates> {
@@ -235,17 +256,17 @@ class AuthTrent extends Trent<AuthTypes> {
235
256
}
236
257
```
237
258
238
-
-`currStateMapper`: Maps over the current state and performs actions based on its type.
259
+
-`stateMap`: Maps over the current state and performs actions based on its type.
239
260
240
261
```dart
241
262
class CalculatorTrent extends Trent<CalculatorStates> {
242
263
CalculatorTrent() : super(BlankScreen());
243
264
244
265
// Perform different actions depending on the current state type
245
266
void handleState() {
246
-
currStateMapper
247
-
..all((state) {
248
-
print("Generic state handler.");
267
+
stateMap
268
+
..orElse((state) {
269
+
print("Generic state handler. Called if nothing more specific defined.");
249
270
})
250
271
..as<BlankScreen>((_) {
251
272
print("Calculator is blank.");
@@ -260,15 +281,15 @@ class AuthTrent extends Trent<AuthTypes> {
260
281
}
261
282
```
262
283
263
-
-`currState`: Access the raw state for custom manipulation.
284
+
-`state`: Access the raw state for custom manipulation.
264
285
265
286
```dart
266
287
class CalculatorTrent extends Trent<CalculatorStates> {
267
288
CalculatorTrent() : super(BlankScreen());
268
289
269
290
// Print the raw state for debugging or custom handling
Copy file name to clipboardExpand all lines: pubspec.yaml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
name: trent
2
2
description: A Flutter package for simple, scalable, and reactive state management with built-in dependency injection and efficient stream-based state handling.
0 commit comments