forked from davidmarne/flutter_built_redux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.dart
More file actions
136 lines (113 loc) · 3.63 KB
/
Copy pathexample.dart
File metadata and controls
136 lines (113 loc) · 3.63 KB
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
library example;
import 'package:built_redux/built_redux.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
import 'package:flutter/material.dart' hide Builder;
import 'package:built_value/built_value.dart';
part 'example.g.dart';
void main() {
// create the store
final store = new Store(
reducerBuilder.build(),
new Counter(),
new CounterActions(),
);
runApp(new ConnectionExample(store));
// or comment the line above and uncomment the line below
// runApp(new ConnectorExample(store));
}
/// an example using `StoreConnection`
class ConnectionExample extends StatelessWidget {
final Store<Counter, CounterBuilder, CounterActions> store;
ConnectionExample(this.store);
@override
Widget build(BuildContext context) => new MaterialApp(
title: 'flutter_built_redux_test',
home: new ReduxProvider(
store: store,
child: new StoreConnection<Counter, CounterActions, int>(
connect: (state) => state.count,
onInit: (state, actions) => print('onInit'),
onDidChange: (state, action) => print('onDidChange'),
onAfterFirstBuild: (state, action) => print('onAfterFirstBuild'),
onDispose: (action) => print('onDispose'),
builder: (BuildContext context, int count, CounterActions actions) {
return new Scaffold(
body: new Row(
children: <Widget>[
new RaisedButton(
onPressed: actions.increment,
child: new Text('Increment'),
),
new Text('Count: $count'),
],
),
);
},
),
),
);
}
/// an example using a widget that implements `StoreConnector`
class ConnectorExample extends StatelessWidget {
final Store<Counter, CounterBuilder, CounterActions> store;
ConnectorExample(this.store);
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'flutter_built_redux_test',
home: new ReduxProvider(
store: store,
child: new CounterWidget(),
),
);
}
}
/// [CounterWidget] impelments [StoreConnector] manually
class CounterWidget extends StoreConnector<Counter, CounterActions, int> {
CounterWidget();
@override
void onInit(int state, CounterActions actions) {
print('onInit');
}
@override
void onDidChange(int state, CounterActions actions) {
print('onDidChange');
}
@override
void onFirstBuild(int state, CounterActions actions) {
print('onFirstBuild');
}
@override
void onDispose(CounterActions actions) {
print('onDispose');
}
@override
int connect(Counter state) => state.count;
@override
Widget build(BuildContext context, int count, CounterActions actions) =>
new Scaffold(
body: new Row(
children: <Widget>[
new RaisedButton(
onPressed: actions.increment,
child: new Text('Increment'),
),
new Text('Count: $count'),
],
),
);
}
// Built redux counter state, actions, and reducer
ReducerBuilder<Counter, CounterBuilder> reducerBuilder =
new ReducerBuilder<Counter, CounterBuilder>()
..add(CounterActionsNames.increment, (s, a, b) => b.count++);
abstract class CounterActions extends ReduxActions {
factory CounterActions() => new _$CounterActions();
CounterActions._();
ActionDispatcher<Null> get increment;
}
abstract class Counter implements Built<Counter, CounterBuilder> {
factory Counter() => new _$Counter._(count: 0);
Counter._();
int get count;
}