/
main.dart
256 lines (232 loc) · 7.88 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:washington/washington.dart';
// User Events
// These are the actions we expect to recieve from the UI.
class CounterIncrementPressed {}
class CounterDecrementPressed {}
class CounterResetPressed {}
class CounterRandomPressed {}
class CounterDividePressed {
final int divideBy;
const CounterDividePressed(this.divideBy);
}
// CounterState Event
// These will be dispatched by our state object to notify other States or
// EventListeners.
class CounterResetted {}
class LimitReached {}
// Extend UnitedState to create a CounterState
class CounterState extends UnitedState<int> {
// You can use fields but they should always be final.
// If it's part of the (changing) state, it should be part of the `value`.
final int lowerLimit;
final int upperLimit;
bool get canIncrement => !isLoading && value < upperLimit;
bool get canDecrement => !isLoading && value > lowerLimit;
bool get canReset => !isLoading && value != 0;
bool get canRandom => !isLoading;
CounterState({
required this.upperLimit,
required this.lowerLimit,
}) : super(0) {
// Add handlers to handle incomming events.
// Pro tip: use tear-offs to get a nice clean list of handlers.
addHandler<CounterIncrementPressed>(_increment);
addHandler<CounterDecrementPressed>(_decrement);
addHandler<CounterResetPressed>(_reset);
addHandler<CounterRandomPressed>(_random);
addHandler<CounterDividePressed>(_divide);
}
void _increment(CounterIncrementPressed event) {
// Here we do some logic checks and set the new State when needed.
if (value < upperLimit) {
setState(value + 1);
}
// When the value reached the upperLimit, we dispatch the LimitReached event.
if (value == upperLimit) {
dispatch(LimitReached());
}
}
void _decrement(CounterDecrementPressed event) {
if (value > lowerLimit) {
setState(value - 1);
}
if (value == lowerLimit) {
dispatch(LimitReached());
}
}
void _reset(CounterResetPressed event) {
setState(0);
dispatch(CounterResetted());
}
void _random(CounterRandomPressed event) {
setState(value, isLoading: true);
Future.delayed(const Duration(seconds: 1), () {
final newValue = Random().nextInt(upperLimit);
setState(newValue);
});
}
void _divide(CounterDividePressed event) {
if (event.divideBy == 0) {
setState(value, error: 'Cannot divide by 0');
} else {
setState((value / event.divideBy).round());
}
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiStateProvider(
stateProviders: [
StateProvider<CounterState>(
create: (_) => CounterState(lowerLimit: 0, upperLimit: 10))
],
child: MaterialApp(
title: 'Washington Demo',
theme: ThemeData(primarySwatch: Colors.purple),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Column(
children: [
Expanded(
child: EventListener(
listener: (context, event) {
if (event is CounterResetted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Counter has been reset!')));
}
if (event is LimitReached) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('A limit has been reached!')));
}
},
child: StateListener<CounterState, int>.success(
successListener: (context, state) {
// We can trigger events based on state changes
if (state.value >= 7) {
Washington.instance.dispatch(CounterResetPressed());
}
},
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Current value:',
),
StateBuilder<CounterState, int>(
builder: (context, state) {
final String text;
if (state.hasError) {
text = state.error.toString();
} else if (state.isLoading) {
text = 'Loading...';
} else {
text = state.value.toString();
}
return Text(
text,
style: Theme.of(context)
.textTheme
.headline4
?.copyWith(
color: state.hasError ? Colors.red : null),
);
},
),
const SizedBox(
height: 32,
),
],
),
),
),
),
),
const Expanded(child: Controls()),
],
),
);
}
}
class Controls extends StatelessWidget {
const Controls({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final counterState = context.watch<CounterState>();
return SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton.icon(
onPressed: counterState.canIncrement
? () =>
Washington.instance.dispatch(CounterIncrementPressed())
: null,
icon: const Icon(Icons.add),
label: const Text('Increment'),
),
ElevatedButton.icon(
onPressed: counterState.canDecrement
? () =>
Washington.instance.dispatch(CounterDecrementPressed())
: null,
icon: const Icon(Icons.remove),
label: const Text('Decrement'),
),
ElevatedButton.icon(
onPressed: counterState.canReset
? () => Washington.instance.dispatch(CounterResetPressed())
: null,
icon: const Icon(Icons.restore),
label: const Text('Reset'),
),
ElevatedButton.icon(
onPressed: counterState.canRandom
? () => Washington.instance.dispatch(CounterRandomPressed())
: null,
icon: const Icon(Icons.refresh),
label: const Text('Random (fake network call)'),
),
ElevatedButton(
onPressed: counterState.canRandom
? () => Washington.instance
.dispatch(const CounterDividePressed(2))
: null,
child: const Text('Divide by 2'),
),
ElevatedButton(
onPressed: counterState.canRandom
? () => Washington.instance
.dispatch(const CounterDividePressed(0))
: null,
child: const Text('Divide by 0 (error)'),
),
],
),
),
);
}
}