Skip to content

Commit

Permalink
mod: 登录页面自动填充
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhigq committed Oct 2, 2024
1 parent 96b1c82 commit 3ff3d07
Showing 1 changed file with 167 additions and 162 deletions.
329 changes: 167 additions & 162 deletions lib/pages/page_login.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:flutter_v2ex/utils/login.dart';
Expand Down Expand Up @@ -78,186 +79,190 @@ class _LoginPageState extends State<LoginPage> {
Form(
key: _formKey, //设置globalKey,用于后面获取FormState
autovalidateMode: AutovalidateMode.disabled,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
// mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(height: 20),
Text(
'登录',
style: Theme.of(context).textTheme.headlineLarge,
),
const SizedBox(height: 10),
Text('使用您的v2ex账号',
style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 50),
Container(
// height: 70,
padding: const EdgeInsets.fromLTRB(20, 10, 20, 5),
child: TextFormField(
controller: _userNameController,
focusNode: userNameTextFieldNode,
decoration: InputDecoration(
labelText: '用户名',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
child: AutofillGroup(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
const SizedBox(height: 20),
Text(
'登录',
style: Theme.of(context).textTheme.headlineLarge,
),
const SizedBox(height: 10),
Text('使用您的v2ex账号',
style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 50),
Container(
// height: 70,
padding: const EdgeInsets.fromLTRB(20, 10, 20, 5),
child: TextFormField(
controller: _userNameController,
focusNode: userNameTextFieldNode,
autofillHints: const [AutofillHints.username],
decoration: InputDecoration(
labelText: '用户名',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
),
),
// 校验用户名
validator: (v) {
return v!.trim().isNotEmpty ? null : "用户名不能为空";
},
onSaved: (val) {
_userName = val;
},
),
// 校验用户名
validator: (v) {
return v!.trim().isNotEmpty ? null : "用户名不能为空";
},
onSaved: (val) {
_userName = val;
},
),
),
Container(
padding: const EdgeInsets.fromLTRB(20, 10, 20, 5),
child: TextFormField(
controller: _passwordController,
obscureText: passwordVisible,
focusNode: passwordTextFieldNode,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
),
labelText: '密码',
suffixIcon: IconButton(
icon: Icon(
passwordVisible
? Icons.visibility
: Icons.visibility_off,
color: Theme.of(context).colorScheme.primary,
Container(
padding: const EdgeInsets.fromLTRB(20, 10, 20, 5),
child: TextFormField(
controller: _passwordController,
obscureText: passwordVisible,
focusNode: passwordTextFieldNode,
autofillHints: const [AutofillHints.password],
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
),
labelText: '密码',
suffixIcon: IconButton(
icon: Icon(
passwordVisible
? Icons.visibility
: Icons.visibility_off,
color: Theme.of(context).colorScheme.primary,
),
onPressed: () {
setState(() {
passwordVisible = !passwordVisible;
});
},
),
onPressed: () {
setState(() {
passwordVisible = !passwordVisible;
});
},
),
//校验密码
validator: (v) {
return v!.trim().length > 5 ? null : "密码不能少于6位";
},
onSaved: (val) {
_password = val;
},
),
//校验密码
validator: (v) {
return v!.trim().length > 5 ? null : "密码不能少于6位";
},
onSaved: (val) {
_password = val;
},
),
),
Container(
padding: const EdgeInsets.fromLTRB(20, 10, 20, 5),
child: Stack(
children: [
TextFormField(
controller: _codeController,
keyboardType: TextInputType.text,
focusNode: captchaTextFieldNode,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
Container(
padding: const EdgeInsets.fromLTRB(20, 10, 20, 5),
child: Stack(
children: [
TextFormField(
controller: _codeController,
keyboardType: TextInputType.text,
focusNode: captchaTextFieldNode,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
),
labelText: '验证码',
),
labelText: '验证码',
validator: (v) {
return v!.trim().isNotEmpty ? null : "验证码不能为空";
},
onSaved: (val) {
_code = val;
},
),
validator: (v) {
return v!.trim().isNotEmpty ? null : "验证码不能为空";
},
onSaved: (val) {
_code = val;
},
),
if (loginKey.captchaImg != '') ...[
Positioned(
right: 6,
top: 6,
height: 52,
child: Container(
clipBehavior: Clip.hardEdge,
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(6),
if (loginKey.captchaImg != '') ...[
Positioned(
right: 6,
top: 6,
height: 52,
child: Container(
clipBehavior: Clip.hardEdge,
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(6),
),
),
),
child: GestureDetector(
onTap: () {
setState(() {
getSignKey();
});
},
child: Image.memory(
loginKey.captchaImgBytes!,
height: 52.0,
fit: BoxFit.fitHeight,
child: GestureDetector(
onTap: () {
setState(() {
getSignKey();
});
},
child: Image.memory(
loginKey.captchaImgBytes!,
height: 52.0,
fit: BoxFit.fitHeight,
),
),
),
),
),
]
],
),
),
Container(
height: 94,
padding: const EdgeInsets.all(20),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size.fromHeight(50)),
child: Text(
'登录',
style: Theme.of(context).textTheme.titleMedium,
]
],
),
onPressed: () async {
if ((_formKey.currentState as FormState).validate()) {
//验证通过提交数据
(_formKey.currentState as FormState).save();
loginKey.userNameValue = _userName!;
loginKey.passwordValue = _password!;
loginKey.codeValue = _code!;
// 键盘收起
captchaTextFieldNode.unfocus();
var result = await DioRequestWeb.onLogin(loginKey);
if (result == 'true') {
// 登录成功
GStorage().setLoginStatus(true);
Get.back(result: {'loginStatus': 'success'});
} else if (result == 'false') {
// 登录失败
setState(() {
// _passwordController.value =
// const TextEditingValue(text: '');
_codeController.value =
const TextEditingValue(text: '');
});
Timer(const Duration(milliseconds: 500), () {
getSignKey();
});
} else if (result == '2fa') {
Login.twoFADialog();
}
}
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// TextButton(onPressed: () => Utils.launchURL('https://www.v2ex.com/signin'), child: Text(
// '网页登录',
// style: TextStyle(color: Colors.grey[600]),
// ),),
// const SizedBox(width: 10),
TextButton(
onPressed: () =>
Utils.openURL('https://www.v2ex.com/forgot'),
Container(
height: 94,
padding: const EdgeInsets.all(20),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size.fromHeight(50)),
child: Text(
'忘记密码?',
style: TextStyle(color: Colors.grey[600]),
'登录',
style: Theme.of(context).textTheme.titleMedium,
),
onPressed: () async {
if ((_formKey.currentState as FormState).validate()) {
TextInput.finishAutofillContext();
//验证通过提交数据
(_formKey.currentState as FormState).save();
loginKey.userNameValue = _userName!;
loginKey.passwordValue = _password!;
loginKey.codeValue = _code!;
// 键盘收起
captchaTextFieldNode.unfocus();
var result = await DioRequestWeb.onLogin(loginKey);
if (result == 'true') {
// 登录成功
GStorage().setLoginStatus(true);
Get.back(result: {'loginStatus': 'success'});
} else if (result == 'false') {
// 登录失败
setState(() {
// _passwordController.value =
// const TextEditingValue(text: '');
_codeController.value =
const TextEditingValue(text: '');
});
Timer(const Duration(milliseconds: 500), () {
getSignKey();
});
} else if (result == '2fa') {
Login.twoFADialog();
}
}
},
),
],
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// TextButton(onPressed: () => Utils.launchURL('https://www.v2ex.com/signin'), child: Text(
// '网页登录',
// style: TextStyle(color: Colors.grey[600]),
// ),),
// const SizedBox(width: 10),
TextButton(
onPressed: () =>
Utils.openURL('https://www.v2ex.com/forgot'),
child: Text(
'忘记密码?',
style: TextStyle(color: Colors.grey[600]),
),
),
],
),
],
),
),
),
Positioned(
Expand Down

0 comments on commit 3ff3d07

Please sign in to comment.