292 lines
10 KiB
Dart
292 lines
10 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
final _phoneController = TextEditingController();
|
|
final _smsCodeController = TextEditingController();
|
|
bool _isLoading = false;
|
|
bool _isSmsLogin = false;
|
|
bool _isCountdownRunning = false;
|
|
int _countdownSeconds = 0;
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
_phoneController.dispose();
|
|
_smsCodeController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _handleLogin() async {
|
|
if (!_formKey.currentState!.validate()) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
|
|
if (mounted) {
|
|
Get.offAllNamed('/home');
|
|
}
|
|
}
|
|
|
|
Future<void> _sendSmsCode() async {
|
|
if (_phoneController.text.isEmpty) {
|
|
showCupertinoDialog(
|
|
context: context,
|
|
builder: (context) => CupertinoAlertDialog(
|
|
title: const Text('提示'),
|
|
content: const Text('请先输入手机号'),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
child: const Text('确定'),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isCountdownRunning = true;
|
|
_countdownSeconds = 60;
|
|
});
|
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
while (_countdownSeconds > 0 && mounted) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
setState(() {
|
|
_countdownSeconds--;
|
|
});
|
|
}
|
|
|
|
if (mounted) {
|
|
setState(() {
|
|
_isCountdownRunning = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return CupertinoPageScaffold(
|
|
navigationBar: const CupertinoNavigationBar(
|
|
middle: Text('登录'),
|
|
),
|
|
child: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const Icon(
|
|
CupertinoIcons.person_3,
|
|
size: 80,
|
|
color: CupertinoColors.activeBlue,
|
|
),
|
|
const SizedBox(height: 32),
|
|
const Text(
|
|
'欢迎回来',
|
|
style: TextStyle(
|
|
fontSize: 28,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
_isSmsLogin ? '使用手机号验证码登录' : '请登录您的账户',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: CupertinoColors.inactiveGray,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 48),
|
|
CupertinoSlidingSegmentedControl<bool>(
|
|
groupValue: _isSmsLogin,
|
|
onValueChanged: (value) {
|
|
if (value != null) {
|
|
setState(() {
|
|
_isSmsLogin = value;
|
|
});
|
|
}
|
|
},
|
|
children: const {
|
|
false: Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
child: Text('密码登录'),
|
|
),
|
|
true: Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
child: Text('短信登录'),
|
|
),
|
|
},
|
|
),
|
|
const SizedBox(height: 24),
|
|
if (!_isSmsLogin) ...[
|
|
CupertinoFormSection.insetGrouped(
|
|
children: [
|
|
CupertinoTextFormFieldRow(
|
|
controller: _usernameController,
|
|
prefix: const Padding(
|
|
padding: EdgeInsets.only(left: 8),
|
|
child: Icon(CupertinoIcons.person, size: 20),
|
|
),
|
|
placeholder: '请输入用户名',
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return '请输入用户名';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
CupertinoFormSection.insetGrouped(
|
|
children: [
|
|
CupertinoTextFormFieldRow(
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
prefix: const Padding(
|
|
padding: EdgeInsets.only(left: 8),
|
|
child: Icon(CupertinoIcons.lock, size: 20),
|
|
),
|
|
placeholder: '请输入密码',
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return '请输入密码';
|
|
}
|
|
if (value.length < 6) {
|
|
return '密码至少6位';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
],
|
|
),
|
|
] else ...[
|
|
CupertinoFormSection.insetGrouped(
|
|
children: [
|
|
CupertinoTextFormFieldRow(
|
|
controller: _phoneController,
|
|
keyboardType: TextInputType.phone,
|
|
prefix: const Padding(
|
|
padding: EdgeInsets.only(left: 8),
|
|
child: Icon(CupertinoIcons.phone, size: 20),
|
|
),
|
|
placeholder: '请输入手机号',
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return '请输入手机号';
|
|
}
|
|
if (value.length != 11) {
|
|
return '请输入有效的手机号';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: CupertinoFormSection.insetGrouped(
|
|
children: [
|
|
CupertinoTextFormFieldRow(
|
|
controller: _smsCodeController,
|
|
keyboardType: TextInputType.number,
|
|
prefix: const Padding(
|
|
padding: EdgeInsets.only(left: 8),
|
|
child: Icon(CupertinoIcons.shield, size: 20),
|
|
),
|
|
placeholder: '请输入验证码',
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return '请输入验证码';
|
|
}
|
|
if (value.length != 6) {
|
|
return '验证码为6位';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
SizedBox(
|
|
width: 120,
|
|
height: 50,
|
|
child: CupertinoButton.filled(
|
|
onPressed: _isCountdownRunning ? null : _sendSmsCode,
|
|
borderRadius: BorderRadius.circular(12),
|
|
padding: EdgeInsets.zero,
|
|
child: Text(
|
|
_isCountdownRunning ? '${_countdownSeconds}s' : '获取验证码',
|
|
style: const TextStyle(fontSize: 14),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
const SizedBox(height: 32),
|
|
CupertinoButton.filled(
|
|
onPressed: _isLoading ? null : _handleLogin,
|
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
|
borderRadius: BorderRadius.circular(12),
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CupertinoActivityIndicator(
|
|
radius: 10,
|
|
),
|
|
)
|
|
: Text(
|
|
_isSmsLogin ? '验证码登录' : '登录',
|
|
style: const TextStyle(fontSize: 17),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
CupertinoButton(
|
|
onPressed: () {
|
|
Get.offAndToNamed('/register');
|
|
},
|
|
child: const Text('还没有账户?立即注册'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|