Skip to content

Commit 5b46cb0

Browse files
committed
增加注释
1 parent c853dc9 commit 5b46cb0

File tree

4 files changed

+209
-1
lines changed

4 files changed

+209
-1
lines changed

lib/common/redux/UserRedux.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ import 'package:redux/redux.dart';
77
* Created by guoshuyu
88
* Date: 2018-07-16
99
*/
10-
10+
/// redux 的 combineReducers, 通过 TypedReducer 将 UpdateUserAction 与 reducers 关联起来
1111
final UserReducer = combineReducers<User>([
1212
TypedReducer<User, UpdateUserAction>(_updateLoaded),
1313
]);
1414

15+
/// 如果有 UpdateUserAction 发起一个请求时
16+
/// 就会调用到 _updateLoaded
17+
/// _updateLoaded 这里接受一个新的userInfo,并返回
1518
User _updateLoaded(User user, action) {
1619
user = action.userInfo;
1720
return user;
1821
}
1922

23+
///定一个 UpdateUserAction ,用于发起 userInfo 的的改变
24+
///类名随你喜欢定义,只要通过上面TypedReducer绑定就好
2025
class UpdateUserAction {
2126
final User userInfo;
2227
UpdateUserAction(this.userInfo);

lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ void main() {
1616
}
1717

1818
class FlutterReduxApp extends StatelessWidget {
19+
20+
/// 创建Store,引用 GSYState 中的 appReducer 创建 Reducer
21+
/// initialState 初始化 State
1922
final store = new Store<GSYState>(appReducer, initialState: new GSYState(userInfo: User.empty(), eventList: new List()));
2023

2124
FlutterReduxApp({Key key}) : super(key: key);
2225

2326
@override
2427
Widget build(BuildContext context) {
28+
/// 通过 StoreProvider 应用 store
2529
return new StoreProvider(
2630
store: store,
2731
child: new MaterialApp(

lib/test/DemoDb.dart

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
4+
import 'package:gsy_github_app_flutter/common/model/User.dart';
5+
import 'package:meta/meta.dart';
6+
import 'package:sqflite/sqflite.dart';
7+
8+
/**
9+
* Created by guoshuyu
10+
* Date: 2018-08-06
11+
*/
12+
13+
14+
///数据库管理
15+
class DemoSqlManager {
16+
static final _VERSION = 1;
17+
18+
static final _NAME = "demo_github_app_flutter.db";
19+
20+
static Database _database;
21+
22+
///初始化
23+
static init() async {
24+
// open the database
25+
var databasesPath = await getDatabasesPath();
26+
String path = databasesPath + _NAME;
27+
_database = await openDatabase(path, version: _VERSION, onCreate: (Database db, int version) async {
28+
// When creating the db, create the table
29+
//await db.execute("CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)");
30+
});
31+
}
32+
33+
/**
34+
* 表是否存在
35+
*/
36+
static isTableExits(String tableName) async {
37+
await getCurrentDatabase();
38+
var res = await _database.rawQuery("select * from Sqlite_master where type = 'table' and name = '$tableName'");
39+
return res != null && res.length > 0;
40+
}
41+
42+
///获取当前数据库对象
43+
static Future<Database> getCurrentDatabase() async {
44+
if (_database == null) {
45+
await init();
46+
}
47+
return _database;
48+
}
49+
50+
///关闭
51+
static close() {
52+
if (_database != null) {
53+
_database.close();
54+
_database = null;
55+
}
56+
}
57+
}
58+
59+
///数据库数据提供的抽象基类
60+
abstract class DemoBaseDbProvider {
61+
bool isTableExits = false;
62+
63+
tableSqlString();
64+
65+
tableName();
66+
67+
tableBaseString(String name, String columnId) {
68+
return '''
69+
create table $name (
70+
$columnId integer primary key autoincrement,
71+
''';
72+
}
73+
74+
Future<Database> getDataBase() async {
75+
return await open();
76+
}
77+
78+
@mustCallSuper
79+
prepare(name, String createSql) async {
80+
isTableExits = await DemoSqlManager.isTableExits(name);
81+
if (!isTableExits) {
82+
Database db = await DemoSqlManager.getCurrentDatabase();
83+
return await db.execute(createSql);
84+
}
85+
}
86+
87+
@mustCallSuper
88+
open() async {
89+
if (!isTableExits) {
90+
await prepare(tableName(), tableSqlString());
91+
}
92+
return await DemoSqlManager.getCurrentDatabase();
93+
}
94+
}
95+
96+
97+
/**
98+
* 用户表
99+
*/
100+
class DemoUserInfoDbProvider extends DemoBaseDbProvider {
101+
final String name = 'UserInfo';
102+
103+
final String columnId = "_id";
104+
final String columnUserName = "userName";
105+
final String columnData = "data";
106+
107+
int id;
108+
String userName;
109+
String data;
110+
111+
DemoUserInfoDbProvider();
112+
113+
Map<String, dynamic> toMap(String userName, String data) {
114+
Map<String, dynamic> map = {columnUserName: userName, columnData: data};
115+
if (id != null) {
116+
map[columnId] = id;
117+
}
118+
return map;
119+
}
120+
121+
DemoUserInfoDbProvider.fromMap(Map map) {
122+
id = map[columnId];
123+
userName = map[columnUserName];
124+
data = map[columnData];
125+
}
126+
127+
@override
128+
tableSqlString() {
129+
return tableBaseString(name, columnId) +
130+
'''
131+
$columnUserName text not null,
132+
$columnData text not null)
133+
''';
134+
}
135+
136+
137+
@override
138+
tableName() {
139+
return name;
140+
}
141+
142+
Future _getUserProvider(Database db, String userName) async {
143+
List<Map<String, dynamic>> maps =
144+
await db.query(name, columns: [columnId, columnUserName, columnData], where: "$columnUserName = ?", whereArgs: [userName]);
145+
if (maps.length > 0) {
146+
DemoUserInfoDbProvider provider = DemoUserInfoDbProvider.fromMap(maps.first);
147+
return provider;
148+
}
149+
return null;
150+
}
151+
152+
///插入到数据库
153+
Future insert(String userName, String eventMapString) async {
154+
Database db = await getDataBase();
155+
var userProvider = await _getUserProvider(db, userName);
156+
if (userProvider != null) {
157+
var result = await db.delete(name, where: "$columnUserName = ?", whereArgs: [userName]);
158+
print(result);
159+
}
160+
return await db.insert(name, toMap(userName, eventMapString));
161+
}
162+
163+
///获取事件数据
164+
Future<User> getUserInfo(String userName) async {
165+
Database db = await getDataBase();
166+
var userProvider = await _getUserProvider(db, userName);
167+
if (userProvider != null) {
168+
return User.fromJson(json.decode(userProvider.data));
169+
}
170+
return null;
171+
}
172+
}

lib/test/DemoUserStore.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Created by guoshuyu
3+
* Date: 2018-08-06
4+
*/
5+
6+
import 'package:flutter/material.dart';
7+
import 'package:flutter_redux/flutter_redux.dart';
8+
import 'package:gsy_github_app_flutter/common/model/User.dart';
9+
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
10+
11+
class DemoUseStorePage extends StatelessWidget {
12+
@override
13+
Widget build(BuildContext context) {
14+
///通过 StoreConnector 关联 GSYState 中的 User
15+
return new StoreConnector<GSYState, User>(
16+
///通过 converter 将 GSYState 中的 userInfo返回
17+
converter: (store) => store.state.userInfo,
18+
///在 userInfo 中返回实际渲染的控件
19+
builder: (context, userInfo) {
20+
return new Text(
21+
userInfo.name,
22+
style: Theme.of(context).textTheme.display1,
23+
);
24+
},
25+
);
26+
}
27+
}

0 commit comments

Comments
 (0)