File-Drive-App-Frontend/lib/screens/login_screen.dart

156 lines
5.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../controllers/session_controller.dart';
import '../core/app_theme.dart';
import '../services/api_client.dart';
import 'register_screen.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _form = GlobalKey<FormState>();
final _username = TextEditingController();
final _password = TextEditingController();
bool _busy = false;
bool _hidden = true;
@override
void dispose() {
_username.dispose();
_password.dispose();
super.dispose();
}
Future<void> _submit() async {
if (!_form.currentState!.validate()) return;
setState(() => _busy = true);
try {
await context.read<SessionController>().login(
_username.text.trim(),
_password.text,
);
} catch (error) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.read<ApiClient>().messageFrom(error))),
);
}
} finally {
if (mounted) setState(() => _busy = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 440),
child: Form(
key: _form,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
width: 68,
height: 68,
decoration: BoxDecoration(
color: AppTheme.blue,
borderRadius: BorderRadius.circular(22),
),
child: const Icon(
Icons.cloud_rounded,
color: Colors.white,
size: 38,
),
),
const SizedBox(height: 28),
const Text(
'Your private drive',
style: TextStyle(
fontSize: 31,
fontWeight: FontWeight.w800,
color: AppTheme.navy,
),
),
const SizedBox(height: 8),
const Text(
'Sign in to upload and organize your full-quality media.',
style: TextStyle(fontSize: 16, color: Colors.black54),
),
const SizedBox(height: 32),
TextFormField(
controller: _username,
textInputAction: TextInputAction.next,
autocorrect: false,
decoration: const InputDecoration(
labelText: 'Username',
prefixIcon: Icon(Icons.person_outline),
),
validator: (value) => (value?.trim().length ?? 0) < 3
? 'Enter your username'
: null,
),
const SizedBox(height: 14),
TextFormField(
controller: _password,
obscureText: _hidden,
onFieldSubmitted: (_) => _submit(),
decoration: InputDecoration(
labelText: 'Password',
prefixIcon: const Icon(Icons.lock_outline),
suffixIcon: IconButton(
onPressed: () => setState(() => _hidden = !_hidden),
icon: Icon(
_hidden
? Icons.visibility_outlined
: Icons.visibility_off_outlined,
),
),
),
validator: (value) => (value?.isEmpty ?? true)
? 'Enter your password'
: null,
),
const SizedBox(height: 22),
FilledButton(
onPressed: _busy ? null : _submit,
child: _busy
? const SizedBox.square(
dimension: 22,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Text('Sign in'),
),
const SizedBox(height: 14),
TextButton(
onPressed: _busy
? null
: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => const RegisterScreen(),
),
),
child: const Text('New to the family? Create an account'),
),
],
),
),
),
),
),
),
);
}
}