forked from CarGuo/gsy_github_app_flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_bloc_page.dart
More file actions
157 lines (139 loc) · 5.33 KB
/
Copy pathdemo_bloc_page.dart
File metadata and controls
157 lines (139 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:gsy_github_app_flutter/redux/gsy_state.dart';
import 'package:gsy_github_app_flutter/redux/login_redux.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LoginPage extends StatefulWidget {
@override
State createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> with LoginBLoC {
@override
Widget build(BuildContext context) {
///共享 store
return new GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
/// 触摸收起键盘
FocusScope.of(context).requestFocus(new FocusNode());
},
child: Scaffold(
///使用主题颜色做背景
body: new Container(
color: Theme.of(context).primaryColor,
child: new Center(
///同时弹出键盘不遮挡
child: SingleChildScrollView(
///显示卡片
child: new Card(
elevation: 5.0,
shape: new RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
color: Colors.white,
margin: const EdgeInsets.only(left: 30.0, right: 30.0),
child: new Padding(
padding: new EdgeInsets.only(
left: 30.0, top: 40.0, right: 30.0, bottom: 0.0),
///内容
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Padding(padding: new EdgeInsets.all(10.0)),
///用户名输入框
new TextField(
controller: userController,
decoration: new InputDecoration(
hintText: "请输入用户名",
icon: Icon(Icons.person),
),
),
new Padding(padding: new EdgeInsets.all(30.0)),
///密码输入框
new TextField(
controller: pwController,
obscureText: true,
decoration: new InputDecoration(
hintText: "请输入密码", icon: Icon(Icons.person)),
),
new Padding(padding: new EdgeInsets.all(15.0)),
///登陆按键
LayoutBuilder(
builder: (context, constraints) {
return SizedBox(
height: 40,
width: constraints.maxWidth,
child: new TextButton(
style: TextButton.styleFrom(
textStyle: TextStyle(
color: Colors.white,
),
backgroundColor:
Theme.of(context).primaryColor),
child: new Text("登陆",
style: new TextStyle(fontSize: 14),
textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.ellipsis),
onPressed: loginIn),
);
},
),
new Padding(padding: new EdgeInsets.all(15.0)),
],
),
),
),
),
),
),
),
);
}
}
mixin LoginBLoC on State<LoginPage> {
final TextEditingController userController = new TextEditingController();
final TextEditingController pwController = new TextEditingController();
String? _userName = "";
String? _password = "";
@override
void initState() {
super.initState();
_initState();
userController.addListener(_usernameChange);
pwController.addListener(_passwordChange);
}
@override
void dispose() {
super.dispose();
userController.removeListener(_usernameChange);
pwController.removeListener(_passwordChange);
}
_usernameChange() {
_userName = userController.text;
}
_passwordChange() {
_password = pwController.text;
}
_initState() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
_userName = await (prefs.get("username") as Future<String?>);
_password = await (prefs.get("password") as Future<String?>);
userController.value = new TextEditingValue(text: _userName ?? "");
pwController.value = new TextEditingValue(text: _password ?? "");
}
loginIn() async {
if (_userName == null || _userName!.isEmpty) {
return;
}
if (_password == null || _password!.isEmpty) {
return;
}
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("username", _userName!);
await prefs.setString("password", _password!);
///通过 redux 去执行登陆流程
StoreProvider.of<GSYState>(context)
.dispatch(LoginAction(context, _userName, _password));
}
}