218 lines
7.4 KiB
Dart
218 lines
7.4 KiB
Dart
import 'dart:convert';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:will_play/utils/auth.dart';
|
||
|
||
import '../../utils/http_util.dart';
|
||
import '../../utils/auth.dart';
|
||
import '../../utils/showToast.dart';
|
||
|
||
class LoginPage extends StatefulWidget {
|
||
LoginPage({Key? key}) : super(key: key);
|
||
|
||
@override
|
||
State<LoginPage> createState() => _LoginPageState();
|
||
}
|
||
|
||
class _LoginPageState extends State<LoginPage> {
|
||
final _formKey = GlobalKey<FormState>();
|
||
final _userName = TextEditingController();
|
||
final _password = TextEditingController();
|
||
bool checked = true;
|
||
|
||
_login() async {
|
||
var removeToken = await Storage.remove('token');
|
||
var removeID = await Storage.remove('id');
|
||
var removeUserName = await Storage.remove('userName');
|
||
|
||
var res = await MyHttpUtil().post(
|
||
"http://101.35.117.69:9093/chat/api/auth/login",
|
||
data: {
|
||
"username": _userName.text,
|
||
"password": _password.text,
|
||
},
|
||
);
|
||
|
||
var setToken = await Storage.set('token', '${res.data["token"]}');
|
||
var setId = await Storage.set('id', '${res.data["id"]}');
|
||
var setUserName = await Storage.set('userName', '${res.data["username"]}');
|
||
|
||
Navigator.of(context).pushReplacementNamed('/');
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
resizeToAvoidBottomInset: false,
|
||
backgroundColor: Colors.white,
|
||
appBar: AppBar(
|
||
elevation: 0, // z轴阴影
|
||
titleSpacing: 0, // 标题与其他控件的间隔
|
||
backgroundColor: Colors.white,
|
||
leading: IconButton(
|
||
icon: Icon(Icons.arrow_back_ios, color: Colors.black),
|
||
onPressed: () => Navigator.of(context).pop(),
|
||
),
|
||
title: Text(
|
||
'登录',
|
||
style: TextStyle(
|
||
color: Color.fromRGBO(51, 51, 51, 1),
|
||
fontSize: 16,
|
||
fontWeight: FontWeight.bold),
|
||
),
|
||
),
|
||
body: SingleChildScrollView(
|
||
padding: EdgeInsets.symmetric(horizontal: 30),
|
||
child: Form(
|
||
key: _formKey,
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
SizedBox(
|
||
height: 80,
|
||
),
|
||
Text(
|
||
'您好,欢迎登录',
|
||
style: TextStyle(fontSize: 28, fontWeight: FontWeight.w800),
|
||
),
|
||
SizedBox(
|
||
height: 20,
|
||
),
|
||
TextFormField(
|
||
controller: _userName,
|
||
validator: (value) {
|
||
return value!.trim().isEmpty ? '请输入账号' : null;
|
||
},
|
||
decoration: InputDecoration(
|
||
labelText: "账号",
|
||
labelStyle: TextStyle(color: Color.fromRGBO(21, 185, 208, 1)),
|
||
enabledBorder: UnderlineInputBorder(
|
||
borderSide:
|
||
BorderSide(color: Color.fromRGBO(187, 187, 187, 1)),
|
||
),
|
||
focusedBorder: UnderlineInputBorder(
|
||
borderSide: BorderSide(
|
||
color: Color.fromRGBO(21, 185, 208, 1),
|
||
),
|
||
),
|
||
),
|
||
style: TextStyle(fontSize: 16),
|
||
keyboardType: TextInputType.phone,
|
||
),
|
||
SizedBox(
|
||
height: 20,
|
||
),
|
||
TextFormField(
|
||
controller: _password,
|
||
validator: (value) {
|
||
return value!.trim().isEmpty ? '请输入密码' : null;
|
||
},
|
||
decoration: InputDecoration(
|
||
labelText: "密码",
|
||
labelStyle: TextStyle(color: Color.fromRGBO(21, 185, 208, 1)),
|
||
enabledBorder: UnderlineInputBorder(
|
||
borderSide:
|
||
BorderSide(color: Color.fromRGBO(187, 187, 187, 1)),
|
||
),
|
||
focusedBorder: UnderlineInputBorder(
|
||
borderSide: BorderSide(
|
||
color: Color.fromRGBO(21, 185, 208, 1),
|
||
),
|
||
),
|
||
),
|
||
style: TextStyle(fontSize: 16),
|
||
obscureText: true,
|
||
),
|
||
Row(
|
||
mainAxisAlignment: MainAxisAlignment.start,
|
||
children: [
|
||
Checkbox(
|
||
value: checked,
|
||
activeColor: Color.fromRGBO(21, 185, 208, 1),
|
||
onChanged: (value) {
|
||
setState(() {
|
||
checked = !checked;
|
||
});
|
||
}),
|
||
Text(
|
||
'同意',
|
||
style: TextStyle(fontSize: 14),
|
||
),
|
||
Text(
|
||
'《服务协议》',
|
||
style: TextStyle(fontSize: 14, color: Colors.blue),
|
||
),
|
||
Text(
|
||
'和',
|
||
style: TextStyle(fontSize: 14),
|
||
),
|
||
Text('《隐私政策》',
|
||
style: TextStyle(fontSize: 14, color: Colors.blue)),
|
||
],
|
||
),
|
||
SizedBox(
|
||
height: 50,
|
||
),
|
||
ElevatedButton(
|
||
child: Text(
|
||
'登录',
|
||
style: TextStyle(fontSize: 16.0, color: Colors.white),
|
||
),
|
||
style: ElevatedButton.styleFrom(
|
||
primary: Color.fromRGBO(21, 185, 208, 1),
|
||
minimumSize: Size(MediaQuery.of(context).size.width, 50),
|
||
shape: RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.circular(30)),
|
||
),
|
||
onPressed: () async {
|
||
if (_formKey.currentState!.validate()) {
|
||
print('表单验证通过!');
|
||
if (!checked) {
|
||
print('请勾选服务协议和隐私政策');
|
||
} else {
|
||
_login();
|
||
|
||
// var token;
|
||
// getToken().then((value) {
|
||
// print('~~~~~~~~~~~~~~$value');
|
||
// token = value;
|
||
// });
|
||
|
||
// print('~~~~~~~~~~~~~~$getToken()');
|
||
// print('~~~~~~~~~~~~~~$token');
|
||
|
||
// var res = await dioHttp()
|
||
// .dio
|
||
// .post('chat/api/auth/login', data: {
|
||
// "username": _userName.text,
|
||
// "password": _password.text,
|
||
// });
|
||
|
||
// var resMap = jsonDecode(res.toString());
|
||
// print(resMap['data']);
|
||
|
||
// print('请求登录接口业务逻辑');
|
||
|
||
// showToast("请求登录接口业务逻辑");
|
||
|
||
// showLoading(context, '加载中...');
|
||
// Future.delayed(Duration(seconds: 3), () {
|
||
// Navigator.of(context).pop();
|
||
// });
|
||
|
||
// showConfirmDialog(context, '确认xxxxxxx吗?', () {
|
||
// print('xxxxxxx');
|
||
// });
|
||
|
||
}
|
||
}
|
||
},
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|