前言
目前我們已經(jīng)實(shí)現(xiàn)了幾個(gè)界面,今天這篇文章開(kāi)始著手進(jìn)行登錄頁(yè)的制作,主要流程就是獲取輸入框中的內(nèi)容,發(fā)送給后臺(tái)進(jìn)行驗(yàn)證岖妄,如果成功將返回信息保存在本地并跳轉(zhuǎn)至首頁(yè),如果失敗就提示用戶(hù)重新輸入寂祥。
在這里面需要掌握3塊知識(shí)荐虐,第一是flutter中的表單組件的使用,然后則是flutter中的數(shù)據(jù)存儲(chǔ)丸凭,最后是網(wǎng)絡(luò)請(qǐng)求福扬。
效果
代碼
上述三個(gè)部分我已經(jīng)在其他文章中分別介紹了,詳情點(diǎn)擊相關(guān)文章的鏈接
- 《flutter表單組件》
- 《Flutter數(shù)據(jù)存儲(chǔ)之shared_preferences》
- 《利用Flutter寫(xiě)一個(gè)跨平臺(tái)的果核APP(3)——網(wǎng)絡(luò)請(qǐng)求》
剩下的我就直接放出首頁(yè)代碼了:
import 'package:flutter/material.dart';
import 'package:flutter_guohe/constant//UrlConstant.dart';
import 'package:dio/dio.dart';
import 'package:flutter_guohe/views/customview.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_guohe/constant/SpConstant.dart';
import 'package:flutter_guohe/pages/app.dart';
class LoginPage extends StatefulWidget {
static String tag = 'login-page';
@override
_LoginPageState createState() => new _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
String _account;
String _password;
//表單驗(yàn)證方法
void _forSubmitted(BuildContext context) {
var _form = _formKey.currentState;
if (_form.validate()) {
_form.save();
login(context, _account.trim(), _password.trim());
}
}
//登錄
void login(BuildContext context, String account, String password) {
showDialog(
context: context,
builder: (context) {
return new LoadingDialog(content: "登錄中惜犀,請(qǐng)稍后......");
});
FormData formData =
new FormData.from({"username": account, "password": password});
Dio().post(Constant.STU_INFO, data: formData).then((res) {
print(account + " " + password);
if (res.statusCode == 200) {
Navigator.pop(context);
if (res.data['code'] == 200) {
print(res.data);
String name = res.data['info']['name'];
String academy = res.data['info']['academy'];
String major = res.data['info']['major'];
String stu_id = res.data['info']['name'];
String stu_pass = res.data['info']['password'];
List<String> list = new List();
list.add(name);
list.add(academy);
list.add(major);
list.add(stu_id);
list.add(stu_pass);
store(list);
Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new Guohe()),
);
}
}
});
}
//將學(xué)生的相關(guān)信息保存至本地
void store(List<String> list) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setBool(SpConstant.IS_LOGIN, true);
sharedPreferences.setStringList(SpConstant.STU_INFO, list);
}
@override
Widget build(BuildContext context) {
final account = new TextFormField(
autofocus: true,
initialValue: '',
decoration: new InputDecoration(
labelText: '學(xué)號(hào)',
),
onSaved: (val) {
_account = val;
},
);
final password = new TextFormField(
initialValue: '',
obscureText: true,
decoration: new InputDecoration(
labelText: '密碼',
),
onSaved: (val) {
_password = val;
},
);
final loginButton = new FloatingActionButton(
backgroundColor: Colors.white,
foregroundColor: Colors.black26,
child: const Icon(Icons.arrow_forward),
elevation: 18.0,
onPressed: () => _forSubmitted(context),
);
final loginBox = new Container(
width: 320.0,
height: 250.0,
margin: new EdgeInsets.only(top: 300.0, right: 30.0),
child: new Stack(
children: <Widget>[
new Container(
color: Colors.white,
width: 280.0,
height: 220.0,
child: new Form(
key: _formKey,
child: new ListView(
shrinkWrap: true,
padding: new EdgeInsets.only(left: 24.0, right: 24.0),
children: <Widget>[
SizedBox(height: 48.0),
account,
new SizedBox(
height: 15.0,
),
password,
],
),
)),
new Positioned(
//方法二
right: 15.0,
top: 180.0,
child: loginButton),
],
),
);
return new Scaffold(
backgroundColor: Colors.white,
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage('assets/imgs/bg_login.webp'),
fit: BoxFit.cover)),
child: new Center(child: loginBox),
),
);
}
}