-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmain_increment_async.dart
177 lines (150 loc) · 5.24 KB
/
main_increment_async.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
import 'dart:async';
import 'package:async_redux/async_redux.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
// Developed by Marcelo Glasberg (2019) https://glasberg.dev and https://github.com/marcglasberg
// For more info: https://asyncredux.com AND https://pub.dev/packages/async_redux
late Store<AppState> store;
/// This example shows a counter, a text description, and a button.
/// When the button is tapped, the counter will increment synchronously,
/// while an async process downloads some text description that relates
/// to the counter number (using the NumberAPI: http://numbersapi.com).
///
/// Note: This example uses http. It was configured to work in Android, debug mode only.
/// If you use iOS, please see:
/// https://flutter.dev/docs/release/breaking-changes/network-policy-ios-android
///
void main() {
var state = AppState.initialState();
store = Store<AppState>(initialState: state);
runApp(MyApp());
}
/// The app state, which in this case is a counter and a description.
@immutable
class AppState {
final int counter;
final String description;
AppState({
required this.counter,
required this.description,
});
AppState copy({int? counter, String? description}) => AppState(
counter: counter ?? this.counter,
description: description ?? this.description,
);
static AppState initialState() => AppState(counter: 0, description: "");
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is AppState &&
runtimeType == other.runtimeType &&
counter == other.counter &&
description == other.description;
@override
int get hashCode => counter.hashCode ^ description.hashCode;
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => StoreProvider<AppState>(
store: store,
child: MaterialApp(
home: MyHomePageConnector(),
));
}
/// This action increments the counter by 1,
/// and then gets some description text relating to the new counter number.
class IncrementAndGetDescriptionAction extends ReduxAction<AppState> {
//
// Async reducer.
// To make it async we simply return Future<AppState> instead of AppState.
@override
Future<AppState> reduce() async {
// First, we increment the counter, synchronously.
dispatch(IncrementAction(amount: 1));
// Then, we start and wait for some asynchronous process.
String description = await read(Uri.http("numbersapi.com", "${state.counter}"));
// After we get the response, we can modify the state with it,
// without having to dispatch another action.
return state.copy(description: description);
}
}
/// This action increments the counter by [amount]].
class IncrementAction extends ReduxAction<AppState> {
final int amount;
IncrementAction({required this.amount});
// Synchronous reducer.
@override
AppState reduce() => state.copy(counter: state.counter + amount);
}
/// This widget is a connector. It connects the store to "dumb-widget".
class MyHomePageConnector extends StatelessWidget {
MyHomePageConnector({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return StoreConnector<AppState, ViewModel>(
vm: () => Factory(),
builder: (BuildContext context, ViewModel vm) => MyHomePage(
counter: vm.counter,
description: vm.description,
onIncrement: vm.onIncrement,
),
);
}
}
/// Factory that creates a view-model for the StoreConnector.
class Factory extends VmFactory<AppState, MyHomePageConnector, ViewModel> {
@override
ViewModel fromStore() => ViewModel(
counter: state.counter,
description: state.description,
onIncrement: _onIncrement,
);
void _onIncrement() {
dispatch(IncrementAndGetDescriptionAction());
print('Counter in the the view-model = ${vm.counter}');
print('Counter in the state when the view-model was created = ${state.counter}');
print('Counter in the current state = ${currentState().counter}');
}
}
/// The view-model holds the part of the Store state the dumb-widget needs.
class ViewModel extends Vm {
final int counter;
final String description;
final VoidCallback onIncrement;
ViewModel({
required this.counter,
required this.description,
required this.onIncrement,
}) : super(equals: [counter, description]);
}
class MyHomePage extends StatelessWidget {
final int? counter;
final String? description;
final VoidCallback? onIncrement;
MyHomePage({
Key? key,
this.counter,
this.description,
this.onIncrement,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Increment Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('You have pushed the button this many times:'),
Text('$counter', style: const TextStyle(fontSize: 30)),
Text(description!, style: const TextStyle(fontSize: 15), textAlign: TextAlign.center),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: onIncrement,
child: const Icon(Icons.add),
),
);
}
}