flutter_tutorial/lib/loginPage.dart
2025-06-17 09:58:04 +08:00

119 lines
3.4 KiB
Dart

import 'package:flutter/material.dart';
import 'homePage.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<StatefulWidget> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage>{
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
void dispose() {
_usernameController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
// appBar: AppBar(
// title: const Text('Login Page'),
// ),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// login Logo
const SizedBox(height: 16.0),
// username Text field
TextFormField(
controller: _usernameController,
decoration: InputDecoration(
labelText: 'Username', // Assign the controller here
border: const OutlineInputBorder(),
prefixIcon: const Icon(Icons.person),
),
),
const SizedBox(height: 16.0),
// password Text field
TextFormField(
obscureText: true,
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Username', // Assign the controller here
border: const OutlineInputBorder(),
prefixIcon: const Icon(Icons.lock),
),
),
const SizedBox(height: 16.0),
// login button
ElevatedButton(
onPressed: () {
_login();
print('Login button pressed');
},
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0),
textStyle: const TextStyle(fontSize: 18),
),
child: const Text('Login'),
),
const SizedBox(height: 16.0),
// forget Password?
TextButton(
onPressed: () {
// TODO: Implement forgot password logic
print("forgot password");
},
child: const Text('Forgot Password?'),
),
],
),
),
),
);
}
void _login() {
print('Username: ${_usernameController.text}');
print('Password: ${_passwordController.text}');
if(_usernameController.text == 'admin' && _passwordController.text == 'admin'){
// Navigator.pushNamed(context, HomePage());
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const HomePage()),
);
}else{
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Login Failed'),
content: const Text('Invalid username or password.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
}
}