85 lines
2.6 KiB
Dart
85 lines
2.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../data/auth_providers.dart';
|
|
|
|
class LoginScreen extends ConsumerStatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends ConsumerState<LoginScreen> {
|
|
final _usernameCtrl = TextEditingController();
|
|
final _passwordCtrl = TextEditingController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameCtrl.dispose();
|
|
_passwordCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (_usernameCtrl.text.trim().isEmpty || _passwordCtrl.text.isEmpty) {
|
|
return;
|
|
}
|
|
await ref.read(authStateProvider.notifier).login(
|
|
_usernameCtrl.text.trim(),
|
|
_passwordCtrl.text,
|
|
);
|
|
if (mounted) {
|
|
final state = ref.read(authStateProvider);
|
|
if (state is AsyncData && state.value != null) {
|
|
if (context.mounted) context.go('/recipes');
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final authState = ref.watch(authStateProvider);
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Logga in')),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
TextField(
|
|
controller: _usernameCtrl,
|
|
decoration: const InputDecoration(labelText: 'Anvandarnamn'),
|
|
textInputAction: TextInputAction.next,
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _passwordCtrl,
|
|
decoration: const InputDecoration(labelText: 'Lösenord'),
|
|
obscureText: true,
|
|
textInputAction: TextInputAction.done,
|
|
onSubmitted: (_) => _submit(),
|
|
),
|
|
const SizedBox(height: 24),
|
|
if (authState is AsyncLoading)
|
|
const CircularProgressIndicator()
|
|
else
|
|
ElevatedButton(
|
|
onPressed: _submit,
|
|
child: const Text('Logga in'),
|
|
),
|
|
if (authState is AsyncError)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 12),
|
|
child: Text(
|
|
'Inloggning misslyckades',
|
|
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|