Skip to content

Commit a87ed05

Browse files
authored
Merge pull request #7 from davidmarne/0.2.0
0.2.0
2 parents 1b90961 + b526888 commit a87ed05

File tree

6 files changed

+53
-123
lines changed

6 files changed

+53
-123
lines changed

README.md

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ flutter_built_redux lets a widget to subscribe to the pieces of the redux state
2424
### Consuming
2525

2626
Wrap your top-level flutter widget with ReduxProvider
27-
```
27+
```dart
2828
class MyProviderWidget extends StatelessWidget {
2929
final store = new Store<MyReduxState, MyReduxStateBuilder, MyReduxStateActions>(
3030
new MyReduxState(),
@@ -41,45 +41,27 @@ class MyProviderWidget extends StatelessWidget {
4141
}
4242
```
4343

44+
Write a widget that extends StoreConnector.
4445
Declare the properties from your state you want this widget to subscribe to by
45-
creating a new built value.
46-
```
47-
abstract class MyWidgetProps implements Built<MyWidgetProps, MyWidgetPropsBuilder> {
48-
String get propIWantFromMyReduxState;
49-
50-
// Built value boilerplate
51-
MyWidgetProps._();
52-
factory MyWidgetProps([updates(MyWidgetPropsBuilder b)]) => _$MyWidgetProps;
53-
}
46+
creating a implementing connect & implement the build method.
47+
```dart
5448
5549
// first 3 generics are the redux store value, builder and actions, while the last
5650
// two are the subscribed values value and builder.
57-
class MyWidget extends StoreConnector<MyReduxState, MyReduxStateBuilder, MyReduxStateActions,
58-
MyWidgetProps, MyWidgetPropsBuilder> {
51+
class MyWidget extends StoreConnector<MyReduxState, MyReduxStateBuilder, MyReduxStateActions, String> {
5952
MyWidget({Key key}) : super(key: key);
6053
6154
6255
// connect is the function that returns an object containing the properties from
6356
// your store that this component cares about. It requires that you return a
64-
// built_value to ensure your props has a valid == override so setState
65-
// is only called if the properties this component cares about change.
57+
// comparable type to ensure your props setState is only called when necessary.
58+
// Primitive types, built values, and collections are recommended.
6659
@override
6760
MyWidgetProps connect(Store<MyReduxState, MyReduxStateBuilder, MyReduxStateActions> store) =>
68-
new MyWidgetProps((b) => b..propIWantFromMyReduxState = store.state.someProperty)
69-
70-
MyWidgetState createState() => new MyWidgetState();
71-
}
72-
73-
class MyWidgetState extends StoreConnectorState<MyReduxState, MyReduxStateBuilder,
74-
MyReduxStateActions, MyWidgetProps, MyWidgetPropsBuilder> {
75-
76-
// by extending StoreConnectorState you inherit a state property. In this case
77-
// it would be of type MyWidgetProps.
78-
//
79-
// you also inherit an actions property, which is of type MyReduxStateActions
61+
store.state.someProperty;
8062
8163
@override
82-
Widget build(BuildContext context) {
64+
Widget build(BuildContext context, MyWidgetProps props, MyReduxStateActions action) {
8365
return new Center(
8466
child: new Text(state.propIWantFromMyReduxState),
8567
);

analysis_options.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
analyzer:
2+
strong-mode: true
File renamed without changes.

example/chat_app/lib/main.dart

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,14 @@ class ChatProvider extends StatelessWidget {
3131
}
3232

3333
class ChatScreen extends StoreConnector<ChatMessages, ChatMessagesBuilder,
34-
ChatMessagesActions, ChatMessages, ChatMessagesBuilder> {
34+
ChatMessagesActions, ChatMessages> {
3535
ChatScreen({Key key}) : super(key: key);
3636

3737
@override
3838
ChatMessages connect(ChatMessages state) => state;
3939

40-
ChatScreenState createState() => new ChatScreenState();
41-
}
42-
43-
class ChatScreenState extends StoreConnectorState<
44-
ChatMessages,
45-
ChatMessagesBuilder,
46-
ChatMessagesActions,
47-
ChatMessages,
48-
ChatMessagesBuilder> {
49-
Widget _buildTextComposer(BuildContext context) {
40+
Widget _buildTextComposer(
41+
BuildContext context, ChatMessages state, ChatMessagesActions actions) {
5042
ThemeData themeData = Theme.of(context);
5143
return new Row(children: <Widget>[
5244
new Flexible(
@@ -73,70 +65,23 @@ class ChatScreenState extends StoreConnectorState<
7365
final ChatUser me = new ChatUser();
7466

7567
@override
76-
Widget build(BuildContext context) {
68+
Widget build(
69+
BuildContext context, ChatMessages state, ChatMessagesActions actions) {
7770
return new Scaffold(
7871
appBar: new AppBar(title: new Text('Chatting as ${me.name}')),
7972
body: new ListView(
8073
padding: new EdgeInsets.symmetric(vertical: 8.0),
8174
children: state.messages.map((ChatMessage message) {
82-
return new ChatMessageListItem(message);
75+
return new ListTile(
76+
dense: true,
77+
leading: new CircleAvatar(
78+
child: new Text(message.sender.name[0]),
79+
backgroundColor: message.sender.color),
80+
title: new Text(message.sender.name),
81+
subtitle: new Text(message.text));
8382
}).toList(),
8483
),
85-
bottomNavigationBar: _buildTextComposer(context),
86-
);
87-
}
88-
}
89-
90-
class ChatMessageListItem extends StatefulWidget {
91-
ChatMessageListItem(ChatMessage m)
92-
: message = m,
93-
super(key: new ObjectKey(m));
94-
95-
final ChatMessage message;
96-
97-
@override
98-
State createState() => new ChatMessageListItemState();
99-
}
100-
101-
class ChatMessageListItemState extends State<ChatMessageListItem>
102-
with SingleTickerProviderStateMixin {
103-
AnimationController _animationController;
104-
Animation<double> _animation;
105-
106-
@override
107-
void initState() {
108-
super.initState();
109-
_animationController = new AnimationController(
110-
vsync: this,
111-
duration: new Duration(milliseconds: 700),
112-
);
113-
114-
_animation = new CurvedAnimation(
115-
parent: _animationController,
116-
curve: Curves.easeOut,
84+
bottomNavigationBar: _buildTextComposer(context, state, actions),
11785
);
118-
119-
_animationController.forward();
120-
}
121-
122-
@override
123-
void dispose() {
124-
_animationController.dispose();
125-
super.dispose();
126-
}
127-
128-
@override
129-
Widget build(BuildContext context) {
130-
final ChatMessage message = widget.message;
131-
return new SizeTransition(
132-
sizeFactor: _animation,
133-
axisAlignment: 0.0,
134-
child: new ListTile(
135-
dense: true,
136-
leading: new CircleAvatar(
137-
child: new Text(message.sender.name[0]),
138-
backgroundColor: message.sender.color),
139-
title: new Text(message.sender.name),
140-
subtitle: new Text(message.text)));
14186
}
14287
}

lib/src/store_connector.dart

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,45 @@ import 'package:flutter/widgets.dart' hide Builder;
55
import 'package:built_redux/built_redux.dart';
66
import 'package:built_value/built_value.dart';
77

8+
/// [StoreConnector] is a widget that rebuilds when the redux store
9+
/// has triggered and the connect function yields a new result.
10+
/// [LocalState] should be comparable. It is recommended to only use primitive and built types.
811
abstract class StoreConnector<
9-
StoreState extends Built<StoreState, StoreStateBuilder>,
10-
StoreStateBuilder extends Builder<StoreState, StoreStateBuilder>,
11-
Actions extends ReduxActions,
12-
LocalState extends Built<LocalState, LocalStateBuilder>,
13-
LocalStateBuilder extends Builder<LocalState, LocalStateBuilder>>
14-
extends StatefulWidget {
12+
StoreState extends Built<StoreState, StoreStateBuilder>,
13+
StoreStateBuilder extends Builder<StoreState, StoreStateBuilder>,
14+
Actions extends ReduxActions,
15+
LocalState> extends StatefulWidget {
1516
StoreConnector({Key key}) : super(key: key);
1617

1718
/// [connect] takes the current state of the redux store and retuns an object that contains
1819
/// the subset of the redux state tree that this component cares about.
1920
@protected
2021
LocalState connect(StoreState state);
22+
23+
@override
24+
StoreConnectorState<StoreState, StoreStateBuilder, Actions, LocalState>
25+
createState() => new StoreConnectorState<StoreState, StoreStateBuilder,
26+
Actions, LocalState>();
27+
28+
@protected
29+
Widget build(BuildContext context, LocalState state, Actions actions);
2130
}
2231

23-
abstract class StoreConnectorState<
32+
class StoreConnectorState<
2433
StoreState extends Built<StoreState, StoreStateBuilder>,
2534
StoreStateBuilder extends Builder<StoreState, StoreStateBuilder>,
2635
Actions extends ReduxActions,
27-
LocalState extends Built<LocalState, LocalStateBuilder>,
28-
LocalStateBuilder extends Builder<LocalState, LocalStateBuilder>>
36+
LocalState>
2937
extends State<
30-
StoreConnector<StoreState, StoreStateBuilder, Actions, LocalState,
31-
LocalStateBuilder>> {
38+
StoreConnector<StoreState, StoreStateBuilder, Actions, LocalState>> {
3239
StreamSubscription<SubstateChange<LocalState>> _storeSub;
3340
ReduxProvider _reduxProvider;
3441

3542
/// [LocalState] is an object that contains the subset of the redux state tree that this component
3643
/// cares about.
37-
LocalState state;
44+
LocalState _state;
3845

39-
Store<StoreState, StoreStateBuilder, Actions> get store {
46+
Store<StoreState, StoreStateBuilder, Actions> get _store {
4047
// get the store from the ReduxProvider ancestor
4148
if (_reduxProvider == null) {
4249
_reduxProvider = context.inheritFromWidgetOfExactType(ReduxProvider);
@@ -47,21 +54,18 @@ abstract class StoreConnectorState<
4754
return _reduxProvider.store;
4855
}
4956

50-
/// [actions] returns the actions object that contains the [ActionDispatchers] associated with the redux store
51-
Actions get actions => store.actions;
52-
5357
/// setup a subscription to the store
5458
@override
5559
void didChangeDependencies() {
5660
super.didChangeDependencies();
5761

5862
// set the initial state
59-
state = widget.connect(store.state);
63+
_state = widget.connect(_store.state);
6064

6165
// listen to changes
62-
_storeSub = store.substateStream(widget.connect).listen((change) {
66+
_storeSub = _store.substateStream(widget.connect).listen((change) {
6367
setState(() {
64-
state = change.next;
68+
_state = change.next;
6569
});
6670
});
6771
}
@@ -73,6 +77,11 @@ abstract class StoreConnectorState<
7377
_storeSub.cancel();
7478
super.dispose();
7579
}
80+
81+
@override
82+
Widget build(BuildContext context) {
83+
return widget.build(context, _state, _store.actions);
84+
}
7685
}
7786

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

pubspec.yaml

Lines changed: 1 addition & 9 deletions
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.1.0
4+
version: 0.2.0
55
description: Built_redux provider for Flutter
66
homepage: https://github.com/davidmarne/flutter_built_redux
77
dependencies:
@@ -11,13 +11,5 @@ dependencies:
1111
built_redux: ^6.0.0
1212
built_value: ^4.0.0
1313

14-
dev_dependencies:
15-
quiver: ^0.24.0
16-
rate_limit: ^0.1.0
17-
test: ^0.12.13
18-
built_collection: ^1.3.0
19-
flutter_test:
20-
sdk: flutter
21-
2214
environment:
2315
sdk: ">=1.19.0 <2.0.0"

0 commit comments

Comments
 (0)