cb_prestige_qr/lib/features/auth/presentation/views/login_view.dart

314 lines
13 KiB
Dart
Raw Normal View History

2026-04-27 09:08:26 +00:00
import 'package:cb_prestige_qr/app/navigation/main_shell.dart';
import 'package:cb_prestige_qr/features/auth/presentation/viewmodels/login_view_model.dart';
2026-04-09 10:06:31 +00:00
import 'package:flutter/material.dart';
2026-04-27 09:08:26 +00:00
import 'package:hooks_riverpod/hooks_riverpod.dart';
2026-04-09 10:06:31 +00:00
2026-04-27 09:08:26 +00:00
class LoginView extends ConsumerStatefulWidget {
const LoginView({super.key});
2026-04-09 10:06:31 +00:00
@override
2026-04-27 09:08:26 +00:00
ConsumerState<LoginView> createState() => _LoginViewState();
2026-04-09 10:06:31 +00:00
}
2026-04-27 09:08:26 +00:00
class _LoginViewState extends ConsumerState<LoginView> {
2026-04-09 10:06:31 +00:00
final _formKey = GlobalKey<FormState>();
2026-04-22 07:59:06 +00:00
Future<void> _submit() async {
2026-04-09 10:06:31 +00:00
final form = _formKey.currentState;
if (form == null || !form.validate()) return;
2026-04-27 09:08:26 +00:00
final didLogin = await ref.read(loginViewModelProvider.notifier).submit();
if (!didLogin || !mounted) return;
2026-04-23 04:52:41 +00:00
Navigator.of(
context,
).pushReplacement(MaterialPageRoute(builder: (_) => const MainShell()));
2026-04-09 10:06:31 +00:00
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colorScheme = theme.colorScheme;
2026-04-27 09:08:26 +00:00
final state = ref.watch(loginViewModelProvider);
final viewModel = ref.read(loginViewModelProvider.notifier);
2026-04-09 10:06:31 +00:00
return Scaffold(
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
2026-04-23 04:52:41 +00:00
colors: [Color(0xff17181c), Color(0xff25262b), Color(0xff2c2d33)],
2026-04-09 10:06:31 +00:00
),
),
child: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 440),
child: Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
2026-04-23 04:52:41 +00:00
color: const Color(0xff1d1e23).withValues(alpha: 0.92),
2026-04-09 10:06:31 +00:00
borderRadius: BorderRadius.circular(28),
2026-04-23 04:52:41 +00:00
border: Border.all(
color: Colors.white.withValues(alpha: 0.08),
),
2026-04-09 10:06:31 +00:00
boxShadow: [
BoxShadow(
2026-04-23 04:52:41 +00:00
color: Colors.black.withValues(alpha: 0.28),
2026-04-09 10:06:31 +00:00
blurRadius: 28,
offset: const Offset(0, 18),
),
],
),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Container(
height: 56,
width: 56,
decoration: BoxDecoration(
2026-04-23 04:52:41 +00:00
color: colorScheme.primary.withValues(
alpha: 0.16,
),
2026-04-09 10:06:31 +00:00
borderRadius: BorderRadius.circular(18),
),
child: const Icon(
Icons.lock_person_rounded,
color: Colors.white,
size: 30,
),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Secure Login',
style: theme.textTheme.headlineSmall
?.copyWith(
fontWeight: FontWeight.w700,
color: Colors.white,
),
),
const SizedBox(height: 4),
Text(
'Use your username and password to continue.',
style: theme.textTheme.bodyMedium?.copyWith(
color: Colors.white70,
height: 1.35,
),
),
],
),
),
],
),
const SizedBox(height: 28),
Container(
width: double.infinity,
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
2026-04-23 04:52:41 +00:00
color: Colors.white.withValues(alpha: 0.04),
2026-04-09 10:06:31 +00:00
borderRadius: BorderRadius.circular(20),
border: Border.all(
2026-04-23 04:52:41 +00:00
color: Colors.white.withValues(alpha: 0.06),
2026-04-09 10:06:31 +00:00
),
),
child: Row(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.asset(
'assets/images/logo_white.png',
height: 48,
width: 48,
fit: BoxFit.cover,
),
),
const SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'CB Prestige Banking',
style: theme.textTheme.titleMedium
?.copyWith(
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 2),
Text(
'Sign in to access your QR dashboard.',
style: theme.textTheme.bodySmall
?.copyWith(color: Colors.white60),
),
],
),
),
],
),
),
const SizedBox(height: 28),
_AuthTextField(
2026-04-27 09:08:26 +00:00
initialValue: state.username,
2026-04-09 10:06:31 +00:00
label: 'Username',
hintText: 'Enter your username',
keyboardType: TextInputType.text,
prefixIcon: Icons.person_rounded,
2026-04-27 09:08:26 +00:00
onChanged: viewModel.updateUsername,
2026-04-09 10:06:31 +00:00
validator: (value) {
final username = value?.trim() ?? '';
if (username.isEmpty) {
return 'Username is required';
}
if (username.length < 3) {
return 'Username must be at least 3 characters';
}
return null;
},
),
const SizedBox(height: 16),
_AuthTextField(
2026-04-27 09:08:26 +00:00
initialValue: state.password,
2026-04-09 10:06:31 +00:00
label: 'Password',
hintText: 'Enter your password',
prefixIcon: Icons.lock_rounded,
2026-04-27 09:08:26 +00:00
obscureText: state.obscurePassword,
onChanged: viewModel.updatePassword,
2026-04-09 10:06:31 +00:00
validator: (value) {
final password = value ?? '';
if (password.isEmpty) {
return 'Password is required';
}
if (password.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
suffixIcon: IconButton(
2026-04-27 09:08:26 +00:00
onPressed: viewModel.togglePasswordVisibility,
2026-04-09 10:06:31 +00:00
icon: Icon(
2026-04-27 09:08:26 +00:00
state.obscurePassword
2026-04-09 10:06:31 +00:00
? Icons.visibility_off_rounded
: Icons.visibility_rounded,
color: Colors.white70,
),
),
),
const SizedBox(height: 12),
Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: () {},
child: const Text('Forgot password?'),
),
),
2026-04-27 09:08:26 +00:00
if (state.errorMessage != null) ...[
Text(
state.errorMessage!,
style: theme.textTheme.bodySmall?.copyWith(
color: colorScheme.error,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 14),
] else
const SizedBox(height: 6),
2026-04-09 10:06:31 +00:00
SizedBox(
width: double.infinity,
child: FilledButton(
2026-04-27 09:08:26 +00:00
onPressed: state.isSubmitting ? null : _submit,
2026-04-09 10:06:31 +00:00
style: FilledButton.styleFrom(
backgroundColor: colorScheme.primary,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18),
),
),
2026-04-27 09:08:26 +00:00
child: state.isSubmitting
2026-04-22 07:59:06 +00:00
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2.4,
valueColor: AlwaysStoppedAnimation<Color>(
Colors.white,
),
),
)
: const Text(
'Sign In',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
2026-04-09 10:06:31 +00:00
),
),
],
),
),
),
),
),
),
),
),
);
}
}
class _AuthTextField extends StatelessWidget {
const _AuthTextField({
2026-04-27 09:08:26 +00:00
required this.initialValue,
2026-04-09 10:06:31 +00:00
required this.label,
required this.hintText,
required this.prefixIcon,
required this.validator,
2026-04-27 09:08:26 +00:00
required this.onChanged,
2026-04-09 10:06:31 +00:00
this.keyboardType,
this.obscureText = false,
this.suffixIcon,
});
2026-04-27 09:08:26 +00:00
final String initialValue;
2026-04-09 10:06:31 +00:00
final String label;
final String hintText;
final IconData prefixIcon;
final String? Function(String?) validator;
2026-04-27 09:08:26 +00:00
final ValueChanged<String> onChanged;
2026-04-09 10:06:31 +00:00
final TextInputType? keyboardType;
final bool obscureText;
final Widget? suffixIcon;
@override
Widget build(BuildContext context) {
return TextFormField(
2026-04-27 09:08:26 +00:00
initialValue: initialValue,
2026-04-09 10:06:31 +00:00
keyboardType: keyboardType,
obscureText: obscureText,
validator: validator,
2026-04-27 09:08:26 +00:00
onChanged: onChanged,
2026-04-09 10:06:31 +00:00
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
labelText: label,
hintText: hintText,
prefixIcon: Icon(prefixIcon),
suffixIcon: suffixIcon,
),
);
}
}