TKK_E32231115MAULANA/lib/features/auth/presentation/pages/login_page.dart

147 lines
4.8 KiB
Dart

import 'package:flutter/material.dart';
import '../../data/user_repository.dart';
import 'register_page.dart';
import 'package:monitoring_jamur/core/theme/app_theme.dart';
import 'package:monitoring_jamur/features/home/presentation/pages/main_screen.dart';
import 'package:monitoring_jamur/core/session/user_session.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
final _userRepo = UserRepository();
bool _isLoading = false;
bool _obscurePassword = true;
void _login() async {
if (_usernameController.text.isEmpty || _passwordController.text.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please fill all fields')),
);
return;
}
setState(() => _isLoading = true);
final user = await _userRepo.loginUser(
_usernameController.text,
_passwordController.text,
);
setState(() => _isLoading = false);
if (user != null) {
await UserSession.saveSession(_usernameController.text);
if (mounted) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const MainScreen()),
(route) => false,
);
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Invalid username or password')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'lib/assets/mushroom.png',
height: 120,
fit: BoxFit.contain,
),
const SizedBox(height: 16),
const Text(
'Mushroom Monitor',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: AppTheme.primaryGreen,
),
),
const Text(
'Expert Quality Agricultural Care',
style: TextStyle(color: AppTheme.textLight),
),
const SizedBox(height: 48),
TextField(
controller: _usernameController,
decoration: const InputDecoration(
hintText: 'Username',
prefixIcon: Icon(Icons.person_outline),
),
),
const SizedBox(height: 16),
TextField(
controller: _passwordController,
obscureText: _obscurePassword,
decoration: InputDecoration(
hintText: 'Password',
prefixIcon: const Icon(Icons.lock_outline),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword ? Icons.visibility_off : Icons.visibility,
color: AppTheme.primaryGreen.withOpacity(0.7),
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
),
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _isLoading ? null : _login,
child: _isLoading
? const CircularProgressIndicator(color: Colors.white)
: const Text('LOGIN'),
),
),
const SizedBox(height: 16),
TextButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => const RegisterPage()),
),
child: RichText(
text: const TextSpan(
text: "Don't have an account? ",
style: TextStyle(color: AppTheme.textLight),
children: [
TextSpan(
text: 'Register',
style: TextStyle(
color: AppTheme.primaryGreen,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
],
),
),
),
);
}
}