2026-03-22 00:21:21 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import 'package:provider/provider.dart';
|
2026-03-22 04:50:19 +08:00
|
|
|
|
|
2026-04-05 22:24:04 +08:00
|
|
|
|
import '../../../core/theme/app_color_scheme.dart';
|
2026-03-23 15:37:59 +08:00
|
|
|
|
import '../../../core/theme/app_spacing.dart';
|
2026-04-05 23:37:27 +08:00
|
|
|
|
import '../../../core/theme/app_theme.dart';
|
2026-04-06 01:58:08 +08:00
|
|
|
|
import '../../../core/theme/app_theme_extension.dart';
|
2026-03-22 00:21:21 +08:00
|
|
|
|
import '../../../providers/auth_provider.dart';
|
2026-04-08 11:11:43 +08:00
|
|
|
|
import '../../components/material_input.dart';
|
2026-03-23 00:08:19 +08:00
|
|
|
|
import '../main/main_page.dart';
|
2026-03-22 15:38:25 +08:00
|
|
|
|
import 'register_page.dart';
|
2026-03-22 00:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
|
|
|
|
const LoginPage({super.key});
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
2026-04-08 11:11:43 +08:00
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
2026-04-05 22:24:04 +08:00
|
|
|
|
final _usernameController = TextEditingController();
|
|
|
|
|
|
final _passwordController = TextEditingController();
|
|
|
|
|
|
bool _obscurePassword = true;
|
2026-03-22 00:21:21 +08:00
|
|
|
|
|
2026-03-23 00:08:19 +08:00
|
|
|
|
static const _loadingIndicatorSize = 16.0;
|
2026-04-05 22:24:04 +08:00
|
|
|
|
static const _logoCircleSize = 80.0;
|
|
|
|
|
|
static const _buttonHeight = 52.0;
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
|
_usernameController.dispose();
|
|
|
|
|
|
_passwordController.dispose();
|
|
|
|
|
|
super.dispose();
|
|
|
|
|
|
}
|
2026-03-23 00:08:19 +08:00
|
|
|
|
|
2026-03-22 00:21:21 +08:00
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
|
return Scaffold(
|
2026-04-06 01:58:08 +08:00
|
|
|
|
backgroundColor: context.colors.surface,
|
2026-04-05 22:24:04 +08:00
|
|
|
|
body: SafeArea(
|
|
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
|
horizontal: AppSpacing.xl,
|
|
|
|
|
|
vertical: AppSpacing.xxl,
|
|
|
|
|
|
),
|
2026-04-08 11:11:43 +08:00
|
|
|
|
child: Form(
|
|
|
|
|
|
key: _formKey,
|
2026-04-05 22:24:04 +08:00
|
|
|
|
child: Column(
|
|
|
|
|
|
children: [
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// 頂部品牌區域
|
2026-04-06 01:58:08 +08:00
|
|
|
|
_buildBrandSection(),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
const SizedBox(height: AppSpacing.xxl),
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// 表單區域
|
2026-04-06 01:58:08 +08:00
|
|
|
|
_buildFormSection(),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
const SizedBox(height: AppSpacing.xl),
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// 底部註冊鏈接
|
2026-04-06 01:58:08 +08:00
|
|
|
|
_buildRegisterRow(),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
],
|
2026-03-22 00:21:21 +08:00
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-03-23 00:08:19 +08:00
|
|
|
|
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// 品牌區域 - Logo + 品牌名 + 標語
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
|
|
|
|
|
|
2026-04-06 01:58:08 +08:00
|
|
|
|
Widget _buildBrandSection() {
|
2026-03-23 00:08:19 +08:00
|
|
|
|
return Column(
|
|
|
|
|
|
children: [
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// Logo 圓形:漸變 #1F2937 → #374151,內含 "M"
|
2026-04-05 22:24:04 +08:00
|
|
|
|
Container(
|
|
|
|
|
|
width: _logoCircleSize,
|
|
|
|
|
|
height: _logoCircleSize,
|
2026-04-06 01:58:08 +08:00
|
|
|
|
decoration: const BoxDecoration(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
shape: BoxShape.circle,
|
2026-04-06 01:58:08 +08:00
|
|
|
|
gradient: LinearGradient(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
begin: Alignment.topCenter,
|
|
|
|
|
|
end: Alignment.bottomCenter,
|
2026-04-05 23:37:27 +08:00
|
|
|
|
colors: [AppColorScheme.darkSurfaceContainerHigh, AppColorScheme.darkOutline],
|
2026-04-05 22:24:04 +08:00
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
alignment: Alignment.center,
|
|
|
|
|
|
child: Text(
|
|
|
|
|
|
'M',
|
2026-04-05 23:37:27 +08:00
|
|
|
|
style: AppTextStyles.displayLarge(context).copyWith(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
fontSize: 32,
|
|
|
|
|
|
fontWeight: FontWeight.w800,
|
2026-04-06 01:58:08 +08:00
|
|
|
|
color: context.colors.onPrimary,
|
2026-04-05 22:24:04 +08:00
|
|
|
|
),
|
|
|
|
|
|
),
|
2026-03-23 00:08:19 +08:00
|
|
|
|
),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
const SizedBox(height: AppSpacing.md),
|
|
|
|
|
|
// 品牌名 "MONISUO"
|
2026-03-23 00:08:19 +08:00
|
|
|
|
Text(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
'MONISUO',
|
2026-04-05 23:37:27 +08:00
|
|
|
|
style: AppTextStyles.displayLarge(context).copyWith(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
letterSpacing: 3,
|
2026-04-06 01:58:08 +08:00
|
|
|
|
color: context.colors.onSurface,
|
2026-04-05 22:24:04 +08:00
|
|
|
|
),
|
2026-03-23 00:08:19 +08:00
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
|
),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
const SizedBox(height: AppSpacing.md),
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// 標語
|
2026-03-23 00:08:19 +08:00
|
|
|
|
Text(
|
2026-04-07 01:05:05 +08:00
|
|
|
|
'虛擬貨幣模擬交易平臺',
|
2026-04-05 23:37:27 +08:00
|
|
|
|
style: AppTextStyles.bodyLarge(context).copyWith(
|
2026-04-06 01:58:08 +08:00
|
|
|
|
color: context.colors.onSurfaceVariant,
|
2026-04-05 22:24:04 +08:00
|
|
|
|
),
|
2026-03-23 00:08:19 +08:00
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// 表單區域 - 用戶名 + 密碼 + 登錄按鈕
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
|
|
|
|
|
|
2026-04-06 01:58:08 +08:00
|
|
|
|
Widget _buildFormSection() {
|
2026-04-05 22:24:04 +08:00
|
|
|
|
return Column(
|
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
|
|
children: [
|
2026-04-06 01:58:08 +08:00
|
|
|
|
_buildUsernameField(),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
const SizedBox(height: AppSpacing.md),
|
2026-04-06 01:58:08 +08:00
|
|
|
|
_buildPasswordField(),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
const SizedBox(height: AppSpacing.sm),
|
2026-04-06 01:58:08 +08:00
|
|
|
|
_buildLoginButton(),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
],
|
2026-03-23 00:08:19 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-06 01:58:08 +08:00
|
|
|
|
Widget _buildUsernameField() {
|
2026-04-08 11:11:43 +08:00
|
|
|
|
return MaterialInput(
|
|
|
|
|
|
controller: _usernameController,
|
|
|
|
|
|
labelText: '用戶名',
|
|
|
|
|
|
hintText: '請輸入用戶名',
|
|
|
|
|
|
prefixIcon: Icons.person_outline,
|
|
|
|
|
|
validator: _validateUsername,
|
2026-04-05 22:24:04 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-06 01:58:08 +08:00
|
|
|
|
Widget _buildPasswordField() {
|
2026-04-08 11:11:43 +08:00
|
|
|
|
return MaterialPasswordInput(
|
|
|
|
|
|
controller: _passwordController,
|
|
|
|
|
|
labelText: '密碼',
|
|
|
|
|
|
hintText: '請輸入密碼',
|
|
|
|
|
|
prefixIcon: Icons.lock_outline,
|
|
|
|
|
|
validator: _validatePassword,
|
2026-03-23 00:08:19 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-06 01:58:08 +08:00
|
|
|
|
Widget _buildLoginButton() {
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// 設計稿: accent-primary = light:#1F2937 / dark:#D4AF37
|
2026-04-06 01:58:08 +08:00
|
|
|
|
final buttonColor = context.appColors.accentPrimary;
|
|
|
|
|
|
final textColor = context.colors.onPrimary;
|
2026-04-05 22:24:04 +08:00
|
|
|
|
|
2026-03-23 00:08:19 +08:00
|
|
|
|
return Consumer<AuthProvider>(
|
|
|
|
|
|
builder: (context, auth, _) {
|
2026-04-05 22:24:04 +08:00
|
|
|
|
return SizedBox(
|
|
|
|
|
|
height: _buttonHeight,
|
2026-04-08 11:11:43 +08:00
|
|
|
|
child: ElevatedButton(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
onPressed: auth.isLoading ? null : () => _handleLogin(auth),
|
2026-04-08 11:11:43 +08:00
|
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
|
|
backgroundColor: buttonColor,
|
|
|
|
|
|
foregroundColor: textColor,
|
|
|
|
|
|
disabledBackgroundColor: buttonColor.withValues(alpha: 0.5),
|
|
|
|
|
|
disabledForegroundColor: textColor.withValues(alpha: 0.5),
|
|
|
|
|
|
elevation: 0,
|
|
|
|
|
|
shadowColor: Colors.transparent,
|
|
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
|
|
borderRadius: BorderRadius.circular(AppRadius.lg),
|
|
|
|
|
|
),
|
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
|
horizontal: AppSpacing.xl,
|
|
|
|
|
|
vertical: AppSpacing.md,
|
2026-04-05 22:24:04 +08:00
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
child: auth.isLoading
|
|
|
|
|
|
? SizedBox.square(
|
|
|
|
|
|
dimension: _loadingIndicatorSize,
|
|
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
|
|
strokeWidth: 2,
|
|
|
|
|
|
color: textColor,
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
: Text(
|
2026-04-07 01:05:05 +08:00
|
|
|
|
'登錄',
|
2026-04-05 23:37:27 +08:00
|
|
|
|
style: AppTextStyles.headlineLarge(context).copyWith(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
fontWeight: FontWeight.w700,
|
|
|
|
|
|
color: textColor,
|
|
|
|
|
|
),
|
2026-03-23 00:08:19 +08:00
|
|
|
|
),
|
2026-04-05 22:24:04 +08:00
|
|
|
|
),
|
2026-03-23 00:08:19 +08:00
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
2026-04-07 01:05:05 +08:00
|
|
|
|
// 底部註冊鏈接
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
|
|
|
|
|
|
2026-04-06 01:58:08 +08:00
|
|
|
|
Widget _buildRegisterRow() {
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// gold-accent: light=#F59E0B / dark=#D4AF37
|
2026-04-06 01:58:08 +08:00
|
|
|
|
final goldColor = context.appColors.accentPrimary;
|
|
|
|
|
|
final secondaryTextColor = context.colors.onSurfaceVariant;
|
2026-04-05 22:24:04 +08:00
|
|
|
|
|
|
|
|
|
|
return Padding(
|
|
|
|
|
|
padding: const EdgeInsets.only(bottom: AppSpacing.xl),
|
|
|
|
|
|
child: Row(
|
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Text(
|
2026-04-07 01:05:05 +08:00
|
|
|
|
'還沒有賬戶?',
|
2026-04-05 23:37:27 +08:00
|
|
|
|
style: AppTextStyles.bodyLarge(context).copyWith(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
color: secondaryTextColor,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
const SizedBox(width: AppSpacing.xs),
|
|
|
|
|
|
GestureDetector(
|
|
|
|
|
|
onTap: _navigateToRegister,
|
|
|
|
|
|
child: Text(
|
2026-04-07 01:05:05 +08:00
|
|
|
|
'立即註冊',
|
2026-04-05 23:37:27 +08:00
|
|
|
|
style: AppTextStyles.bodyLarge(context).copyWith(
|
2026-04-05 22:24:04 +08:00
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
|
color: goldColor,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
2026-03-23 00:08:19 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
2026-03-23 00:08:19 +08:00
|
|
|
|
// Validators
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
|
|
|
|
|
|
2026-03-23 00:08:19 +08:00
|
|
|
|
String? _validateUsername(String? value) {
|
|
|
|
|
|
if (value == null || value.isEmpty) {
|
2026-04-07 01:05:05 +08:00
|
|
|
|
return '請輸入用戶名';
|
2026-03-23 00:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (value.length < 3) {
|
2026-04-07 01:05:05 +08:00
|
|
|
|
return '用戶名至少 3 個字符';
|
2026-03-23 00:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String? _validatePassword(String? value) {
|
|
|
|
|
|
if (value == null || value.isEmpty) {
|
2026-04-07 01:05:05 +08:00
|
|
|
|
return '請輸入密碼';
|
2026-03-23 00:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (value.length < 6) {
|
2026-04-07 01:05:05 +08:00
|
|
|
|
return '密碼至少 6 個字符';
|
2026-03-23 00:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
2026-03-23 00:08:19 +08:00
|
|
|
|
// Actions
|
2026-04-05 22:24:04 +08:00
|
|
|
|
// ============================================
|
|
|
|
|
|
|
2026-03-23 00:08:19 +08:00
|
|
|
|
Future<void> _handleLogin(AuthProvider auth) async {
|
2026-04-08 11:11:43 +08:00
|
|
|
|
if (!_formKey.currentState!.validate()) return;
|
2026-03-23 00:08:19 +08:00
|
|
|
|
|
|
|
|
|
|
final response = await auth.login(
|
2026-04-08 11:11:43 +08:00
|
|
|
|
_usernameController.text.trim(),
|
|
|
|
|
|
_passwordController.text,
|
2026-03-23 00:08:19 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
|
_navigateToMainPage();
|
|
|
|
|
|
} else {
|
2026-04-07 01:05:05 +08:00
|
|
|
|
_showErrorDialog(response.message ?? '用戶名或密碼錯誤');
|
2026-03-23 00:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void _navigateToMainPage() {
|
2026-03-26 01:35:51 +08:00
|
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
|
|
MaterialPageRoute(builder: (context) => const MainPage()),
|
|
|
|
|
|
(route) => false,
|
|
|
|
|
|
);
|
2026-03-23 00:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void _navigateToRegister() {
|
|
|
|
|
|
Navigator.push(
|
|
|
|
|
|
context,
|
|
|
|
|
|
MaterialPageRoute(builder: (context) => const RegisterPage()),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void _showErrorDialog(String message) {
|
2026-04-08 11:11:43 +08:00
|
|
|
|
showDialog(
|
2026-03-23 00:08:19 +08:00
|
|
|
|
context: context,
|
2026-04-08 11:11:43 +08:00
|
|
|
|
builder: (context) => AlertDialog(
|
2026-04-07 01:05:05 +08:00
|
|
|
|
title: const Text('登錄失敗'),
|
2026-04-08 11:11:43 +08:00
|
|
|
|
content: Text(message),
|
2026-03-23 00:08:19 +08:00
|
|
|
|
actions: [
|
2026-04-08 11:11:43 +08:00
|
|
|
|
TextButton(
|
2026-03-23 00:08:19 +08:00
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
2026-04-08 11:11:43 +08:00
|
|
|
|
child: const Text('確定'),
|
2026-03-23 00:08:19 +08:00
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-03-22 00:21:21 +08:00
|
|
|
|
}
|