Skip to content

Commit d6eef4f

Browse files
committed
Add new and const back in. Not ready yet in Dart 2, causing problems.
1 parent 8410417 commit d6eef4f

File tree

6 files changed

+148
-111
lines changed

6 files changed

+148
-111
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ addons:
99
- libstdc++6
1010
- fonts-droid
1111
before_script:
12-
- git clone https://github.com/flutter/flutter.git -b beta --depth 1
12+
- git clone https://github.com/flutter/flutter.git -b master --depth 1
1313
- ./flutter/bin/flutter doctor
1414
script:
1515
- ./flutter/bin/flutter test --preview-dart-2 --coverage --coverage-path=lcov.info

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
* Works with Dart 2 (no longer supports Dart 1)
66
* Stronger Type info Required
77
* Breaking Changes:
8-
* `StoreProvider` now requires generic type info: `StoreProvider<AppState>`
9-
* `new StoreProvider.of(context).store` is now `StoreProvider.of<AppState>(context)`
8+
* `StoreProvider` now requires generic type info: `new StoreProvider<AppState>`
9+
* `new StoreProvider.of(context).store` is now `StoreProvider.of<AppState>(context)`
10+
11+
## 0.3.6
12+
13+
* Add `onWillChange`. This function will be called before the builder and can be used for working with Imperative APIs, such as Navigator, TextEditingController, or TabController.
1014

1115
## 0.3.6
1216

README.md

+30-26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ This package is built to work with [Redux.dart](https://pub.dartlang.org/package
1616

1717
* Dart 1: 0.3.x
1818
* Dart 2: 0.4.0+. See the migration guide below!
19+
20+
## Dart 2 Migration Guide
21+
22+
Dart 2 requires more strict typing (yay!), and gives us the option to make getting the Store from the StoreProvider more convenient!
23+
24+
1. Change `new StoreProvider(...)` to `new StoreProvider<StateClass>(...)` in your Widget tree.
25+
2. Change `new StoreProvider.of(context).store` to `StoreProvider.of<StateClass>(context)` if you're directly fetching the `Store<AppState>` yourself from the `StoreProvider<AppState>`. No need to access the `store` field directly any more since Dart 2 can now infer the proper type with a static function :)
1926

2027
## Examples
2128

@@ -71,21 +78,21 @@ class FlutterReduxApp extends StatelessWidget {
7178
@override
7279
Widget build(BuildContext context) {
7380
return MaterialApp(
74-
theme: ThemeData.dark(),
81+
theme: new ThemeData.dark(),
7582
title: title,
76-
home: StoreProvider<int>(
83+
home: new StoreProvider<int>(
7784
// Pass the store to the StoreProvider. Any ancestor `StoreConnector`
7885
// Widgets will find and use this value as the `Store`.
7986
store: store,
80-
child: Scaffold(
81-
appBar: AppBar(
82-
title: Text(title),
87+
child: new Scaffold(
88+
appBar: new AppBar(
89+
title: new Text(title),
8390
),
84-
body: Center(
85-
child: Column(
91+
body: new Center(
92+
child: new Column(
8693
mainAxisAlignment: MainAxisAlignment.center,
8794
children: [
88-
Text(
95+
new Text(
8996
'You have pushed the button this many times:',
9097
),
9198
// Connect the Store to a Text Widget that renders the current
@@ -103,10 +110,12 @@ class FlutterReduxApp extends StatelessWidget {
103110
// count. No need to manually manage subscriptions or Streams!
104111
new StoreConnector<int, String>(
105112
converter: (store) => store.state.toString(),
106-
builder: (context, count) => Text(
107-
count,
108-
style: Theme.of(context).textTheme.display1,
109-
),
113+
builder: (context, count) {
114+
return new Text(
115+
count,
116+
style: Theme.of(context).textTheme.display1,
117+
);
118+
},
110119
)
111120
],
112121
),
@@ -122,26 +131,21 @@ class FlutterReduxApp extends StatelessWidget {
122131
// with no parameters. It only dispatches an Increment action.
123132
return () => store.dispatch(Actions.Increment);
124133
},
125-
builder: (context, callback) => FloatingActionButton(
126-
// Attach the `callback` to the `onPressed` attribute
127-
onPressed: callback,
128-
tooltip: 'Increment',
129-
child: Icon(Icons.add),
130-
),
134+
builder: (context, callback) {
135+
return new FloatingActionButton(
136+
// Attach the `callback` to the `onPressed` attribute
137+
onPressed: callback,
138+
tooltip: 'Increment',
139+
child: new Icon(Icons.add),
140+
);
141+
},
131142
),
132143
),
133144
),
134145
);
135146
}
136147
}
137-
```
138-
139-
## Dart 2 Migration
140-
141-
Dart 2 requires more strict typing (yay!), and gives us the option to make getting the Store from the StoreProvider more convenient!
142-
143-
1. Change `new StoreProvider(...)` to `StoreProvider<StateClass>(...)` in your Widget tree.
144-
2. Change `new StoreProvider.of(context).store` to `StoreProvider.of<StateClass>(context)` if you're directly fetching the `Store<AppState>` yourself from the `StoreProvider<AppState>`. No need to access the `store` field directly any more since Dart 2 can now infer the proper type with a static function :)
148+
```
145149

146150
## Purpose
147151

example/lib/main.dart

+22-18
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ class FlutterReduxApp extends StatelessWidget {
3535
@override
3636
Widget build(BuildContext context) {
3737
return MaterialApp(
38-
theme: ThemeData.dark(),
38+
theme: new ThemeData.dark(),
3939
title: title,
40-
home: StoreProvider<int>(
40+
home: new StoreProvider<int>(
4141
// Pass the store to the StoreProvider. Any ancestor `StoreConnector`
4242
// Widgets will find and use this value as the `Store`.
4343
store: store,
44-
child: Scaffold(
45-
appBar: AppBar(
46-
title: Text(title),
44+
child: new Scaffold(
45+
appBar: new AppBar(
46+
title: new Text(title),
4747
),
48-
body: Center(
49-
child: Column(
48+
body: new Center(
49+
child: new Column(
5050
mainAxisAlignment: MainAxisAlignment.center,
5151
children: [
52-
Text(
52+
new Text(
5353
'You have pushed the button this many times:',
5454
),
5555
// Connect the Store to a Text Widget that renders the current
@@ -67,10 +67,12 @@ class FlutterReduxApp extends StatelessWidget {
6767
// count. No need to manually manage subscriptions or Streams!
6868
new StoreConnector<int, String>(
6969
converter: (store) => store.state.toString(),
70-
builder: (context, count) => Text(
71-
count,
72-
style: Theme.of(context).textTheme.display1,
73-
),
70+
builder: (context, count) {
71+
return new Text(
72+
count,
73+
style: Theme.of(context).textTheme.display1,
74+
);
75+
},
7476
)
7577
],
7678
),
@@ -86,12 +88,14 @@ class FlutterReduxApp extends StatelessWidget {
8688
// with no parameters. It only dispatches an Increment action.
8789
return () => store.dispatch(Actions.Increment);
8890
},
89-
builder: (context, callback) => FloatingActionButton(
90-
// Attach the `callback` to the `onPressed` attribute
91-
onPressed: callback,
92-
tooltip: 'Increment',
93-
child: Icon(Icons.add),
94-
),
91+
builder: (context, callback) {
92+
return new FloatingActionButton(
93+
// Attach the `callback` to the `onPressed` attribute
94+
onPressed: callback,
95+
tooltip: 'Increment',
96+
child: new Icon(Icons.add),
97+
);
98+
},
9599
),
96100
),
97101
),

lib/flutter_redux.dart

+17-15
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class StoreProvider<S> extends InheritedWidget {
1616
Key key,
1717
@required Store<S> store,
1818
@required Widget child,
19-
})
20-
: assert(store != null),
19+
}) : assert(store != null),
2120
assert(child != null),
2221
_store = store,
2322
super(key: key, child: child);
@@ -122,7 +121,7 @@ class StoreConnector<S, ViewModel> extends StatelessWidget {
122121
///
123122
/// This can be useful for dispatching actions that fetch data for your Widget
124123
/// when it is first displayed.
125-
final OnInitCallback onInit;
124+
final OnInitCallback<S> onInit;
126125

127126
/// A function that will be run when the StoreConnector is removed from the
128127
/// Widget Tree.
@@ -131,7 +130,7 @@ class StoreConnector<S, ViewModel> extends StatelessWidget {
131130
///
132131
/// This can be useful for dispatching actions that remove stale data from
133132
/// your State tree.
134-
final OnDisposeCallback onDispose;
133+
final OnDisposeCallback<S> onDispose;
135134

136135
/// Determines whether the Widget should be rebuilt when the Store emits an
137136
/// onChange event.
@@ -172,8 +171,7 @@ class StoreConnector<S, ViewModel> extends StatelessWidget {
172171
this.rebuildOnChange = true,
173172
this.ignoreChange,
174173
this.onWillChange,
175-
})
176-
: assert(builder != null),
174+
}) : assert(builder != null),
177175
assert(converter != null),
178176
super(key: key);
179177

@@ -214,7 +212,7 @@ class StoreBuilder<S> extends StatelessWidget {
214212
///
215213
/// This can be useful for dispatching actions that fetch data for your Widget
216214
/// when it is first displayed.
217-
final OnInitCallback onInit;
215+
final OnInitCallback<S> onInit;
218216

219217
/// A function that will be run when the StoreBuilder is removed from the
220218
/// Widget Tree.
@@ -223,7 +221,13 @@ class StoreBuilder<S> extends StatelessWidget {
223221
///
224222
/// This can be useful for dispatching actions that remove stale data from
225223
/// your State tree.
226-
final OnDisposeCallback onDispose;
224+
final OnDisposeCallback<S> onDispose;
225+
226+
/// A function that will be run on State change.
227+
///
228+
/// This can be useful for imperative calls to things like Navigator,
229+
/// TabController, etc
230+
final OnWillChangeCallback<Store<S>> onWillChange;
227231

228232
/// A function that will be run on State change.
229233
///
@@ -238,8 +242,7 @@ class StoreBuilder<S> extends StatelessWidget {
238242
this.onDispose,
239243
this.rebuildOnChange = true,
240244
this.onWillChange,
241-
})
242-
: assert(builder != null),
245+
}) : assert(builder != null),
243246
super(key: key);
244247

245248
@override
@@ -250,7 +253,7 @@ class StoreBuilder<S> extends StatelessWidget {
250253
rebuildOnChange: rebuildOnChange,
251254
onInit: onInit,
252255
onDispose: onDispose,
253-
onWillChange: this.onWillChange,
256+
onWillChange: onWillChange,
254257
);
255258
}
256259
}
@@ -262,8 +265,8 @@ class _StoreStreamListener<S, ViewModel> extends StatefulWidget {
262265
final Store<S> store;
263266
final bool rebuildOnChange;
264267
final bool distinct;
265-
final OnInitCallback onInit;
266-
final OnDisposeCallback onDispose;
268+
final OnInitCallback<S> onInit;
269+
final OnDisposeCallback<S> onDispose;
267270
final IgnoreChangeTest<S> ignoreChange;
268271
final OnWillChangeCallback<ViewModel> onWillChange;
269272

@@ -278,8 +281,7 @@ class _StoreStreamListener<S, ViewModel> extends StatefulWidget {
278281
this.rebuildOnChange = true,
279282
this.ignoreChange,
280283
this.onWillChange,
281-
})
282-
: super(key: key);
284+
}) : super(key: key);
283285

284286
@override
285287
State<StatefulWidget> createState() {

0 commit comments

Comments
 (0)