CommunityAPP/lib/login_page.dart
2025-05-13 15:23:21 +08:00

178 lines
6.0 KiB
Dart

import 'package:flutter/material.dart';
import 'home_page.dart'; // 要跳轉的頁面
import 'new_resident_step.dart';
import 'forgot_password.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
void _login() {
String account = _emailController.text.trim();
String password = _passwordController.text;
if (account == 'admin' && password == '1234') {
// 登入成功,跳轉到主畫面
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const HomePage()),
);
} else {
// 登入失敗,跳出錯誤訊息
showDialog(
context: context,
builder:
(_) => AlertDialog(
title: const Text('登入失敗'),
content: const Text('帳號或密碼錯誤,請重新輸入。'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('確定'),
),
],
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[100],
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 10,
offset: const Offset(0, 5),
),
],
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'社區通',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: Colors.indigo,
),
),
const SizedBox(height: 16),
CircleAvatar(
radius: 64,
backgroundImage: AssetImage('assets/images/login.png'),
),
const SizedBox(height: 24),
const Text(
'歡迎回來',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 24),
TextField(
controller: _emailController,
decoration: InputDecoration(
labelText: '帳號',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
const SizedBox(height: 16),
TextField(
controller: _passwordController,
decoration: InputDecoration(
labelText: '密碼',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
obscureText: true,
),
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _login,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.indigo,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: const Text(
'登入',
style: TextStyle(color: Colors.white),
),
),
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
OutlinedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const NewResidentStepPage(),
),
);
},
style: OutlinedButton.styleFrom(
foregroundColor: Colors.indigo,
side: const BorderSide(color: Colors.indigo),
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 12,
),
),
child: const Text('新住戶'),
),
OutlinedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ForgotPasswordPage(),
),
);
},
style: OutlinedButton.styleFrom(
foregroundColor: Colors.indigo,
side: const BorderSide(color: Colors.indigo),
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 12,
),
),
child: const Text('忘記密碼'),
),
],
),
],
),
),
),
),
);
}
}