Skip to content

Commit fc7d18c

Browse files
committed
add example and store_connection widget
1 parent 9f38e54 commit fc7d18c

File tree

8 files changed

+442
-453
lines changed

8 files changed

+442
-453
lines changed

analysis_options.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
analyzer:
2-
strong-mode: true
2+
strong-mode:
3+
implicit-casts: false
4+
implicit-dynamic: false

example/example.dart

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
library example;
2+
3+
import 'package:built_redux/built_redux.dart';
4+
import 'package:flutter_built_redux/flutter_built_redux.dart';
5+
import 'package:flutter/material.dart' hide Builder;
6+
import 'package:built_value/built_value.dart';
7+
8+
part 'example.g.dart';
9+
10+
/// an example using `StoreConnection`
11+
class ConnectionExample extends StatelessWidget {
12+
final Store<Counter, CounterBuilder, CounterActions> store;
13+
14+
ConnectionExample(this.store);
15+
16+
@override
17+
Widget build(BuildContext context) => new MaterialApp(
18+
title: 'flutter_built_redux_test',
19+
home: new ReduxProvider(
20+
store: store,
21+
child: new StoreConnection<Counter, CounterActions, int>(
22+
connect: (state) => state.count,
23+
builder: (BuildContext context, int count, CounterActions actions) {
24+
return new Scaffold(
25+
body: new Row(
26+
children: <Widget>[
27+
new RaisedButton(
28+
onPressed: actions.increment,
29+
child: new Text('Increment'),
30+
),
31+
new Text('Count: $count'),
32+
],
33+
),
34+
);
35+
},
36+
),
37+
),
38+
);
39+
}
40+
41+
/// an example using a widget that implements `StoreConnector`
42+
class ConnectorExample extends StatelessWidget {
43+
final Store<Counter, CounterBuilder, CounterActions> store;
44+
45+
ConnectorExample(this.store);
46+
47+
@override
48+
Widget build(BuildContext context) {
49+
return new MaterialApp(
50+
title: 'flutter_built_redux_test',
51+
home: new ReduxProvider(
52+
store: store,
53+
child: new CounterWidget(),
54+
),
55+
);
56+
}
57+
}
58+
59+
/// [CounterWidget] impelments [StoreConnector] manually
60+
class CounterWidget extends StoreConnector<Counter, CounterActions, int> {
61+
CounterWidget();
62+
63+
@override
64+
int connect(Counter state) {
65+
return state.count;
66+
}
67+
68+
@override
69+
Widget build(BuildContext context, int count, CounterActions actions) =>
70+
new Scaffold(
71+
body: new Row(
72+
children: <Widget>[
73+
new RaisedButton(
74+
onPressed: actions.increment,
75+
child: new Text('Increment'),
76+
),
77+
new Text('Count: $count'),
78+
],
79+
),
80+
);
81+
}
82+
83+
// Built redux counter store, actions, and reducer
84+
85+
Store<Counter, CounterBuilder, CounterActions> createStore() => new Store(
86+
reducerBuilder.build(),
87+
new Counter(),
88+
new CounterActions(),
89+
);
90+
91+
ReducerBuilder<Counter, CounterBuilder> reducerBuilder =
92+
new ReducerBuilder<Counter, CounterBuilder>()
93+
..add(CounterActionsNames.increment, (s, a, b) => b.count++);
94+
95+
abstract class CounterActions extends ReduxActions {
96+
factory CounterActions() => new _$CounterActions();
97+
CounterActions._();
98+
99+
ActionDispatcher<Null> get increment;
100+
}
101+
102+
abstract class Counter implements Built<Counter, CounterBuilder> {
103+
factory Counter() => new _$Counter._(count: 0);
104+
Counter._();
105+
106+
int get count;
107+
}

example/example.g.dart

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/flutter_built_redux.dart

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,50 @@ import 'package:meta/meta.dart';
44
import 'package:flutter/widgets.dart' hide Builder;
55
import 'package:built_redux/built_redux.dart';
66

7+
/// [StoreConnection] is a widget that rebuilds when the redux store
8+
/// has triggered and the connect function yields a new result. It is an implementation
9+
/// of `StoreConnector` that takes a connect function and builder function as parameters
10+
/// so `StoreConnector` doesn't have to be implemented manually.
11+
///
12+
/// [connect] takes the current state of the redux store and retuns an object that contains
13+
/// the subset of the redux state tree that this component cares about.
14+
/// It requires that you return a comparable type to ensure your props setState is only called when necessary.
15+
/// Primitive types, built values, and collections are recommended.
16+
/// The result of [connect] is what gets passed to the build function's second param
17+
///
18+
/// [builder] is a function that takes a `BuildContext`, the `LocalState` returned from
19+
/// connect, and your `ReduxActions` class and returns a widget.
20+
///
21+
/// [StoreState] is the generic type of your built_redux store's state object
22+
/// [Actions] is the generic tyoe of your built_redux store's actions contiainer
23+
/// [LocalState] is the state from your store that this widget needs to render.
24+
/// [LocalState] should be comparable. It is recommended to only use primitive or built types.
25+
class StoreConnection<StoreState, Actions extends ReduxActions, LocalState>
26+
extends StoreConnector<StoreState, Actions, LocalState> {
27+
final LocalState Function(StoreState state) _connect;
28+
final Widget Function(BuildContext context, LocalState state, Actions actions)
29+
_builder;
30+
31+
StoreConnection({
32+
@required LocalState connect(StoreState state),
33+
@required
34+
Widget builder(BuildContext context, LocalState state, Actions actions),
35+
Key key,
36+
})
37+
: assert(connect != null, 'StoreConnection: connect must not be null'),
38+
assert(builder != null, 'StoreConnection: builder must not be null'),
39+
_connect = connect,
40+
_builder = builder,
41+
super(key: key);
42+
43+
@protected
44+
LocalState connect(StoreState state) => _connect(state);
45+
46+
@protected
47+
Widget build(BuildContext context, LocalState state, Actions actions) =>
48+
_builder(context, state, actions);
49+
}
50+
751
/// [StoreConnector] is a widget that rebuilds when the redux store
852
/// has triggered and the connect function yields a new result.
953
/// [StoreState] is the generic type of your built_redux store's state object
@@ -93,7 +137,7 @@ class _StoreConnectorState<StoreState, Actions extends ReduxActions, LocalState>
93137

94138
@override
95139
Widget build(BuildContext context) =>
96-
widget.build(context, _state, _store.actions);
140+
widget.build(context, _state, _store.actions as Actions);
97141
}
98142

99143
/// [ReduxProvider] provides access to the redux store to descendant widgets.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Important: Use "flutter packages get", not "pub get".
22
name: flutter_built_redux
33
author: David Marne <[email protected]>
4-
version: 0.4.3
4+
version: 0.4.4
55
description: Built_redux provider for Flutter
66
homepage: https://github.com/davidmarne/flutter_built_redux
77
dependencies:

0 commit comments

Comments
 (0)