Skip to content

Commit 0d60e9c

Browse files
committed
add redux test
1 parent dfecf11 commit 0d60e9c

8 files changed

Lines changed: 105 additions & 56 deletions

File tree

‎lib/common/dao/UserDao.dart‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart';
88
import 'package:gsy_github_app_flutter/common/model/User.dart';
99
import 'package:gsy_github_app_flutter/common/net/Address.dart';
1010
import 'package:gsy_github_app_flutter/common/net/Api.dart';
11+
import 'package:redux/redux.dart';
12+
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
1113

1214
class UserDao {
1315
static login(userName, password, callback) async {
@@ -32,7 +34,6 @@ class UserDao {
3234
if (res != null && res.result) {
3335
await LocalStorage.save(Config.PW_KEY, password);
3436
var resultData = await getUserInfo(null);
35-
//todo 登录成功后
3637
if(Config.DEBUG) {
3738
print("user result " + resultData.result.toString());
3839
print(resultData.data);
@@ -45,11 +46,11 @@ class UserDao {
4546
}
4647

4748
///初始化用户信息
48-
static initUserInfo() async {
49+
static initUserInfo(Store store) async {
4950
var token = await LocalStorage.get(Config.TOKEN_KEY);
5051
var res = await getUserInfoLocal();
5152
if (res != null && res.result && token != null) {
52-
//todo dispatch store
53+
store.dispatch(UserActions(res.data));
5354
}
5455
return new DataResult(res.data, (res.result && (token != null)));
5556
}

‎lib/common/model/User.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,8 @@ class User extends Object with _$UserSerializerMixin {
8787
/// from a map. We pass the map to the generated _$UserFromJson constructor.
8888
/// The constructor is named after the source class, in this case User.
8989
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
90+
91+
// 命名构造函数
92+
User.empty();
93+
9094
}

‎lib/common/redux/GSYState.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:gsy_github_app_flutter/common/model/User.dart';
2+
3+
class GSYState {
4+
User userInfo;
5+
GSYState({this.userInfo});
6+
}
7+
8+
// One simple action: Increment
9+
class UserActions {
10+
final User userInfo;
11+
UserActions(this.userInfo);
12+
}
13+
14+
GSYState counterReducer(GSYState state, dynamic action) {
15+
if (action is UserActions) {
16+
state.userInfo = action.userInfo;
17+
}
18+
return state;
19+
}
20+

‎lib/main.dart‎

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
import 'package:flutter/material.dart';
2+
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
3+
import 'package:gsy_github_app_flutter/common/model/User.dart';
24
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
5+
import 'package:gsy_github_app_flutter/page/HomePage.dart';
6+
import 'package:gsy_github_app_flutter/page/LoginPage.dart';
37
import 'package:gsy_github_app_flutter/page/WelcomePage.dart';
48
import 'package:flutter_redux/flutter_redux.dart';
59
import 'package:redux/redux.dart';
610

7-
// One simple action: Increment
8-
enum Actions { Increment }
9-
10-
// The reducer, which takes the previous count and increments it in response
11-
// to an Increment action.
12-
int counterReducer(int state, dynamic action) {
13-
if (action == Actions.Increment) {
14-
return state + 1;
15-
}
16-
17-
return state;
18-
}
19-
2011

2112
void main() {
22-
final store = new Store<int>(counterReducer, initialState: 0);
23-
24-
runApp(new FlutterReduxApp(
25-
title: 'Flutter Redux Demo',
26-
store: store,
27-
));
13+
runApp(new FlutterReduxApp());
2814
}
2915

3016
class FlutterReduxApp extends StatelessWidget {
31-
final Store<int> store;
32-
final String title;
3317

34-
FlutterReduxApp({Key key, this.store, this.title}) : super(key: key);
18+
final store = new Store<GSYState>(counterReducer, initialState: new GSYState(userInfo: User.empty()));
19+
20+
FlutterReduxApp({Key key}) : super(key: key);
3521

3622
@override
3723
Widget build(BuildContext context) {
38-
return new StoreProvider<int>(
24+
return new StoreProvider(
3925
store: store,
40-
child:new MaterialApp(
41-
title: 'Flutter Demo',
42-
theme: new ThemeData(
43-
primarySwatch: GSYColors.primarySwatch,
44-
),
45-
home: new WelcomePage()),
26+
child: new MaterialApp(
27+
title: 'Flutter Demo',
28+
theme: new ThemeData(
29+
primarySwatch: GSYColors.primarySwatch,
30+
),
31+
routes: {
32+
"/": (context) {
33+
return WelcomePage();
34+
},
35+
"home": (context) {
36+
return HomePage();
37+
},
38+
"login": (context) {
39+
return LoginPage();
40+
},
41+
}),
4642
);
4743
}
48-
}
44+
}

‎lib/page/HomePage.dart‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
4+
import 'package:flutter_redux/flutter_redux.dart';
5+
import 'package:gsy_github_app_flutter/common/model/User.dart';
6+
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
27
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
38
import 'package:gsy_github_app_flutter/widget/GSYTabBarWidget.dart';
49

@@ -14,8 +19,29 @@ class HomePage extends StatelessWidget {
1419
new Tab(icon: new Icon(Icons.directions_bike)),
1520
],
1621
tabViews: [
17-
new Icon(Icons.directions_car),
18-
new Icon(Icons.directions_transit),
22+
new StoreBuilder<GSYState>(
23+
builder: (context, store) {
24+
new Future.delayed(const Duration(seconds: 2), () {
25+
User user = store.state.userInfo;
26+
user.login = "ffff";
27+
user.name = "ttttt";
28+
store.dispatch(new UserActions(user));
29+
});
30+
return new Text(
31+
store.state.userInfo.login,
32+
style: Theme.of(context).textTheme.display1,
33+
);;
34+
},
35+
),
36+
new StoreConnector<GSYState, String>(
37+
converter: (store) => store.state.userInfo.name,
38+
builder: (context, count) {
39+
return new Text(
40+
count,
41+
style: Theme.of(context).textTheme.display1,
42+
);
43+
},
44+
),
1945
new Icon(Icons.directions_bike),
2046
],
2147
backgroundColor: GSYColors.primarySwatch,

‎lib/page/WelcomePage.dart‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
import 'dart:async';
22

33
import 'package:flutter/material.dart';
4+
import 'package:flutter_redux/flutter_redux.dart';
45
import 'package:gsy_github_app_flutter/common/dao/UserDao.dart';
6+
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
7+
import 'package:gsy_github_app_flutter/main.dart';
58
import 'package:gsy_github_app_flutter/page/HomePage.dart';
69
import 'package:gsy_github_app_flutter/page/LoginPage.dart';
710

811
class WelcomePage extends StatelessWidget {
912
@override
1013
Widget build(BuildContext context) {
1114
toNext(res) {
12-
Widget widget;
15+
String widget;
1316
if (res != null && res.result) {
14-
widget = new HomePage();
17+
widget = "home";
1518
} else {
16-
widget = new LoginPage();
19+
widget = "login";
1720
}
18-
Navigator.of(context).pushReplacement(new MaterialPageRoute(builder: (context) {
19-
//判断是去登录还是主页
20-
return widget;
21-
}));
21+
Navigator.pushReplacementNamed(context, widget);
2222
}
23-
24-
new Future.delayed(const Duration(seconds: 2), () {
25-
UserDao.initUserInfo().then((res) {
26-
print("Ffffffffff");
27-
print(res);
28-
toNext(res);
29-
});
30-
});
31-
return new Container(
32-
color: Colors.white,
33-
child: new Center(
34-
child: new Text("Welcome", style: new TextStyle(color: Colors.black, fontSize: 22.0)),
35-
),
23+
return StoreBuilder<GSYState>(
24+
builder: (context, store) {
25+
new Future.delayed(const Duration(seconds: 2), () {
26+
UserDao.initUserInfo(store).then((res) {
27+
toNext(res);
28+
});
29+
});
30+
return new Container(
31+
color: Colors.white,
32+
child: new Center(
33+
child: new Text("Welcome", style: new TextStyle(color: Colors.black, fontSize: 22.0)),
34+
),
35+
);
36+
},
3637
);
3738
}
3839
}

‎lib/widget/GSYTabBarWidget.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class GSYTabBarWidget extends StatefulWidget {
2626
_GSYTabBarState createState() => new _GSYTabBarState(type, tabItems, tabViews, backgroundColor, indicatorColor, title);
2727
}
2828

29+
// ignore: mixin_inherits_from_not_object
2930
class _GSYTabBarState extends State<GSYTabBarWidget> with SingleTickerProviderStateMixin {
3031
final int _type;
3132

‎test/widget_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:gsy_github_app_flutter/main.dart';
1212
void main() {
1313
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
1414
// Build our app and trigger a frame.
15-
await tester.pumpWidget(new MyApp());
15+
await tester.pumpWidget(new FlutterReduxApp());
1616

1717
// Verify that our counter starts at 0.
1818
expect(find.text('0'), findsOneWidget);

0 commit comments

Comments
 (0)