240 lines
8.2 KiB
Dart
240 lines
8.2 KiB
Dart
import 'package:fengshui_compass/components/custom_textfield_widget.dart';
|
|
import 'package:fengshui_compass/models/login_bean.dart';
|
|
import 'package:fengshui_compass/net/dio_utils.dart';
|
|
import 'package:fengshui_compass/pages/register_page.dart';
|
|
import 'package:fengshui_compass/states/token.dart';
|
|
import 'package:fengshui_compass/utils/token_helper.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
final String username;
|
|
|
|
const LoginPage({Key key, this.username}) : super(key: key);
|
|
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final FocusNode _userNameFocusNode = FocusNode();
|
|
final FocusNode _passwordFocusNode = FocusNode();
|
|
|
|
final TextEditingController _userNameEditController = TextEditingController();
|
|
final TextEditingController _passwordEditController = TextEditingController();
|
|
|
|
bool _passwordShow = true;
|
|
|
|
@override
|
|
void initState() {
|
|
if (widget.username != null) {
|
|
setState(() {
|
|
_userNameEditController.text = widget.username;
|
|
});
|
|
}
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
// appBar: AppBar(
|
|
// elevation: 0,
|
|
// centerTitle: true,
|
|
// title: const Text(W
|
|
// '登陆',
|
|
// style: TextStyle(color: Colors.white),
|
|
// ),
|
|
// ),
|
|
body: SizedBox(
|
|
width: double.infinity,
|
|
height: double.infinity,
|
|
child: GestureDetector(
|
|
onTap: () {
|
|
_userNameFocusNode.unfocus();
|
|
_passwordFocusNode.unfocus();
|
|
},
|
|
child: Stack(
|
|
children: [
|
|
const Hero(
|
|
tag: "logo",
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
child: Image(
|
|
width: double.infinity,
|
|
image: AssetImage("assets/images/head.png"),
|
|
fit: BoxFit.fitWidth,
|
|
),
|
|
),
|
|
),
|
|
Positioned(
|
|
top: 250,
|
|
left: 0,
|
|
right: 0,
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: const [
|
|
Text(
|
|
"登录",
|
|
style: TextStyle(
|
|
fontSize: 32, fontWeight: FontWeight.w600),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
Positioned(
|
|
left: 25,
|
|
top: 25,
|
|
child: CloseButton(
|
|
color: Colors.white,
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
)),
|
|
Positioned(
|
|
bottom: 80,
|
|
right: 180,
|
|
left: 180,
|
|
child: Column(
|
|
children: [
|
|
// 输入表单
|
|
TextFieldWidget(
|
|
hintText: "手机号",
|
|
submit: (value) {
|
|
if (value.length != 11) {
|
|
Fluttertoast.showToast(msg: "请输入11位手机号");
|
|
FocusScope.of(context)
|
|
.requestFocus(_userNameFocusNode);
|
|
return;
|
|
}
|
|
_userNameFocusNode.unfocus();
|
|
FocusScope.of(context)
|
|
.requestFocus(_passwordFocusNode);
|
|
},
|
|
focusNode: _userNameFocusNode,
|
|
prefixIconData: Icons.phone_android_outlined,
|
|
controller: _userNameEditController,
|
|
obscureText: false,
|
|
),
|
|
const SizedBox(height: 20),
|
|
TextFieldWidget(
|
|
hintText: "密码",
|
|
submit: (value) {
|
|
if (value.length < 5) {
|
|
Fluttertoast.showToast(msg: "请输入5位以上密码");
|
|
FocusScope.of(context)
|
|
.requestFocus(_passwordFocusNode);
|
|
return;
|
|
}
|
|
_userNameFocusNode.unfocus();
|
|
_passwordFocusNode.unfocus();
|
|
submitFunction();
|
|
},
|
|
focusNode: _passwordFocusNode,
|
|
prefixIconData: Icons.lock_open_outlined,
|
|
suffixIconData: _passwordShow
|
|
? Icons.visibility
|
|
: Icons.visibility_off,
|
|
obscureText: _passwordShow,
|
|
controller: _passwordEditController,
|
|
onTap: () {
|
|
setState(() {
|
|
_passwordShow = !_passwordShow;
|
|
});
|
|
},
|
|
),
|
|
const SizedBox(height: 50),
|
|
// 登录
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 56,
|
|
child: ElevatedButton(
|
|
onPressed: () {
|
|
submitFunction();
|
|
},
|
|
child: const Text("登录"),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Text("没有账号, "),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => RegisterPage()));
|
|
},
|
|
child: const Text("点击注册"))
|
|
],
|
|
)
|
|
],
|
|
),
|
|
)
|
|
],
|
|
),
|
|
)),
|
|
);
|
|
}
|
|
|
|
void submitFunction() async {
|
|
// 获取输入用户名密码
|
|
String userName = _userNameEditController.text;
|
|
String password = _passwordEditController.text;
|
|
|
|
if (userName.trim().length != 11) {
|
|
Fluttertoast.showToast(
|
|
msg: "请输入11位手机号",
|
|
backgroundColor: Colors.orange,
|
|
textColor: Colors.white,
|
|
fontSize: 20.0);
|
|
return;
|
|
}
|
|
|
|
if (password.trim().length < 5) {
|
|
Fluttertoast.showToast(
|
|
msg: "请输入5位以上密码",
|
|
backgroundColor: Colors.orange,
|
|
textColor: Colors.white,
|
|
fontSize: 20.0);
|
|
return;
|
|
}
|
|
|
|
Map<String, dynamic> map = {
|
|
"username": userName,
|
|
"password": password,
|
|
"uuid": "",
|
|
"code": ""
|
|
};
|
|
|
|
LoginInfo responseInfo = await DioUtils.instance.loginRequest(
|
|
url: "http://120.26.107.74:1619/login",
|
|
jsonMap: map,
|
|
);
|
|
|
|
if (responseInfo.success) {
|
|
Map<String, dynamic> res = {"token": responseInfo.token.toString()};
|
|
LoginBean loginBean = LoginBean.fromMap(res);
|
|
// TokenHelper.getInstance.loginBean = loginBean;
|
|
// 将token添加到状态管理器中
|
|
Provider.of<TokenProvider>(context, listen: false).setToken(loginBean);
|
|
Fluttertoast.showToast(msg: "登录成功");
|
|
|
|
//关闭当前页面
|
|
Navigator.of(context).pop(true);
|
|
//发送消息更新我的页面显示内容
|
|
// loginStreamController.add(0);
|
|
} else {
|
|
//登录失败页面小提示
|
|
Fluttertoast.showToast(
|
|
msg: responseInfo.msg,
|
|
backgroundColor: Colors.red,
|
|
textColor: Colors.white,
|
|
fontSize: 20.0);
|
|
}
|
|
}
|
|
}
|