Skip to content

Commit

Permalink
Add new and const back in. Not ready yet in Dart 2, causing problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianegan committed Mar 19, 2018
1 parent 8410417 commit d6eef4f
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ addons:
- libstdc++6
- fonts-droid
before_script:
- git clone https://github.com/flutter/flutter.git -b beta --depth 1
- git clone https://github.com/flutter/flutter.git -b master --depth 1
- ./flutter/bin/flutter doctor
script:
- ./flutter/bin/flutter test --preview-dart-2 --coverage --coverage-path=lcov.info
Expand Down
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
* Works with Dart 2 (no longer supports Dart 1)
* Stronger Type info Required
* Breaking Changes:
* `StoreProvider` now requires generic type info: `StoreProvider<AppState>`
* `new StoreProvider.of(context).store` is now `StoreProvider.of<AppState>(context)`
* `StoreProvider` now requires generic type info: `new StoreProvider<AppState>`
* `new StoreProvider.of(context).store` is now `StoreProvider.of<AppState>(context)`

## 0.3.6

* 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.

## 0.3.6

Expand Down
56 changes: 30 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ This package is built to work with [Redux.dart](https://pub.dartlang.org/package

* Dart 1: 0.3.x
* Dart 2: 0.4.0+. See the migration guide below!

## Dart 2 Migration Guide

Dart 2 requires more strict typing (yay!), and gives us the option to make getting the Store from the StoreProvider more convenient!

1. Change `new StoreProvider(...)` to `new StoreProvider<StateClass>(...)` in your Widget tree.
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 :)

## Examples

Expand Down Expand Up @@ -71,21 +78,21 @@ class FlutterReduxApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
theme: new ThemeData.dark(),
title: title,
home: StoreProvider<int>(
home: new StoreProvider<int>(
// Pass the store to the StoreProvider. Any ancestor `StoreConnector`
// Widgets will find and use this value as the `Store`.
store: store,
child: Scaffold(
appBar: AppBar(
title: Text(title),
child: new Scaffold(
appBar: new AppBar(
title: new Text(title),
),
body: Center(
child: Column(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
new Text(
'You have pushed the button this many times:',
),
// Connect the Store to a Text Widget that renders the current
Expand All @@ -103,10 +110,12 @@ class FlutterReduxApp extends StatelessWidget {
// count. No need to manually manage subscriptions or Streams!
new StoreConnector<int, String>(
converter: (store) => store.state.toString(),
builder: (context, count) => Text(
count,
style: Theme.of(context).textTheme.display1,
),
builder: (context, count) {
return new Text(
count,
style: Theme.of(context).textTheme.display1,
);
},
)
],
),
Expand All @@ -122,26 +131,21 @@ class FlutterReduxApp extends StatelessWidget {
// with no parameters. It only dispatches an Increment action.
return () => store.dispatch(Actions.Increment);
},
builder: (context, callback) => FloatingActionButton(
// Attach the `callback` to the `onPressed` attribute
onPressed: callback,
tooltip: 'Increment',
child: Icon(Icons.add),
),
builder: (context, callback) {
return new FloatingActionButton(
// Attach the `callback` to the `onPressed` attribute
onPressed: callback,
tooltip: 'Increment',
child: new Icon(Icons.add),
);
},
),
),
),
);
}
}
```

## Dart 2 Migration

Dart 2 requires more strict typing (yay!), and gives us the option to make getting the Store from the StoreProvider more convenient!

1. Change `new StoreProvider(...)` to `StoreProvider<StateClass>(...)` in your Widget tree.
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 :)
```

## Purpose

Expand Down
40 changes: 22 additions & 18 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ class FlutterReduxApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
theme: new ThemeData.dark(),
title: title,
home: StoreProvider<int>(
home: new StoreProvider<int>(
// Pass the store to the StoreProvider. Any ancestor `StoreConnector`
// Widgets will find and use this value as the `Store`.
store: store,
child: Scaffold(
appBar: AppBar(
title: Text(title),
child: new Scaffold(
appBar: new AppBar(
title: new Text(title),
),
body: Center(
child: Column(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
new Text(
'You have pushed the button this many times:',
),
// Connect the Store to a Text Widget that renders the current
Expand All @@ -67,10 +67,12 @@ class FlutterReduxApp extends StatelessWidget {
// count. No need to manually manage subscriptions or Streams!
new StoreConnector<int, String>(
converter: (store) => store.state.toString(),
builder: (context, count) => Text(
count,
style: Theme.of(context).textTheme.display1,
),
builder: (context, count) {
return new Text(
count,
style: Theme.of(context).textTheme.display1,
);
},
)
],
),
Expand All @@ -86,12 +88,14 @@ class FlutterReduxApp extends StatelessWidget {
// with no parameters. It only dispatches an Increment action.
return () => store.dispatch(Actions.Increment);
},
builder: (context, callback) => FloatingActionButton(
// Attach the `callback` to the `onPressed` attribute
onPressed: callback,
tooltip: 'Increment',
child: Icon(Icons.add),
),
builder: (context, callback) {
return new FloatingActionButton(
// Attach the `callback` to the `onPressed` attribute
onPressed: callback,
tooltip: 'Increment',
child: new Icon(Icons.add),
);
},
),
),
),
Expand Down
32 changes: 17 additions & 15 deletions lib/flutter_redux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class StoreProvider<S> extends InheritedWidget {
Key key,
@required Store<S> store,
@required Widget child,
})
: assert(store != null),
}) : assert(store != null),
assert(child != null),
_store = store,
super(key: key, child: child);
Expand Down Expand Up @@ -122,7 +121,7 @@ class StoreConnector<S, ViewModel> extends StatelessWidget {
///
/// This can be useful for dispatching actions that fetch data for your Widget
/// when it is first displayed.
final OnInitCallback onInit;
final OnInitCallback<S> onInit;

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

/// Determines whether the Widget should be rebuilt when the Store emits an
/// onChange event.
Expand Down Expand Up @@ -172,8 +171,7 @@ class StoreConnector<S, ViewModel> extends StatelessWidget {
this.rebuildOnChange = true,
this.ignoreChange,
this.onWillChange,
})
: assert(builder != null),
}) : assert(builder != null),
assert(converter != null),
super(key: key);

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

/// A function that will be run when the StoreBuilder is removed from the
/// Widget Tree.
Expand All @@ -223,7 +221,13 @@ class StoreBuilder<S> extends StatelessWidget {
///
/// This can be useful for dispatching actions that remove stale data from
/// your State tree.
final OnDisposeCallback onDispose;
final OnDisposeCallback<S> onDispose;

/// A function that will be run on State change.
///
/// This can be useful for imperative calls to things like Navigator,
/// TabController, etc
final OnWillChangeCallback<Store<S>> onWillChange;

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

@override
Expand All @@ -250,7 +253,7 @@ class StoreBuilder<S> extends StatelessWidget {
rebuildOnChange: rebuildOnChange,
onInit: onInit,
onDispose: onDispose,
onWillChange: this.onWillChange,
onWillChange: onWillChange,
);
}
}
Expand All @@ -262,8 +265,8 @@ class _StoreStreamListener<S, ViewModel> extends StatefulWidget {
final Store<S> store;
final bool rebuildOnChange;
final bool distinct;
final OnInitCallback onInit;
final OnDisposeCallback onDispose;
final OnInitCallback<S> onInit;
final OnDisposeCallback<S> onDispose;
final IgnoreChangeTest<S> ignoreChange;
final OnWillChangeCallback<ViewModel> onWillChange;

Expand All @@ -278,8 +281,7 @@ class _StoreStreamListener<S, ViewModel> extends StatefulWidget {
this.rebuildOnChange = true,
this.ignoreChange,
this.onWillChange,
})
: super(key: key);
}) : super(key: key);

@override
State<StatefulWidget> createState() {
Expand Down
Loading

0 comments on commit d6eef4f

Please sign in to comment.