Initial commit: Flutter 无书应用项目

This commit is contained in:
Developer
2026-03-30 02:35:31 +08:00
commit 9175ff9905
566 changed files with 103261 additions and 0 deletions

View File

@@ -0,0 +1,179 @@
/// 时间: 2026-03-29
/// 功能: 填写投票凭证弹窗组件
/// 介绍: 提供投票功能的用户凭证填写功能
import 'package:flutter/material.dart';
import '../../../constants/app_constants.dart';
import '../../../utils/http/vote_api.dart';
class LoginRegisterDialog extends StatefulWidget {
final VoidCallback? onLoginSuccess;
const LoginRegisterDialog({super.key, this.onLoginSuccess});
@override
State<LoginRegisterDialog> createState() => _LoginRegisterDialogState();
}
class _LoginRegisterDialogState extends State<LoginRegisterDialog> {
final _formKey = GlobalKey<FormState>();
final _usernameController = TextEditingController();
bool _isLoading = false;
@override
void dispose() {
_usernameController.dispose();
super.dispose();
}
String? _validateUsername(String? value) {
if (value == null || value.isEmpty) {
return '请输入投票凭证';
}
final phoneRegex = RegExp(r'^1[3456789]\d{9}$');
final emailRegex = RegExp(
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
);
final wechatRegex = RegExp(r'^[a-zA-Z0-9_-]{6,20}$');
if (!phoneRegex.hasMatch(value) &&
!emailRegex.hasMatch(value) &&
!wechatRegex.hasMatch(value)) {
return '请输入正确的手机号、邮箱或微信号';
}
return null;
}
Future<void> _submit() async {
if (!_formKey.currentState!.validate()) {
return;
}
setState(() {
_isLoading = true;
});
try {
final deviceType = await VoteApi.getDeviceType();
final username = _usernameController.text.trim();
const password = '123456';
try {
await VoteApi.login(username: username, password: password);
if (mounted) {
Navigator.pop(context);
widget.onLoginSuccess?.call();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('验证成功'),
backgroundColor: AppConstants.successColor,
),
);
}
} catch (loginError) {
await VoteApi.register(
username: username,
password: password,
userIdentifier: deviceType,
);
await VoteApi.login(username: username, password: password);
if (mounted) {
Navigator.pop(context);
widget.onLoginSuccess?.call();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('注册并验证成功'),
backgroundColor: AppConstants.successColor,
),
);
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.toString().replaceAll('Exception: ', '')),
backgroundColor: AppConstants.errorColor,
),
);
}
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
@override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Container(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'填写投票凭证',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () => Navigator.pop(context),
),
],
),
const SizedBox(height: 20),
Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: '投票凭证',
hintText: '手机号/邮箱/微信号',
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.person),
),
validator: _validateUsername,
),
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _isLoading ? null : _submit,
style: ElevatedButton.styleFrom(
backgroundColor: AppConstants.primaryColor,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: _isLoading
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
Colors.white,
),
),
)
: const Text('验证', style: TextStyle(fontSize: 16)),
),
),
],
),
),
],
),
),
);
}
}