feat: add profile screen and update routing; enhance login validation and logout functionality
This commit is contained in:
@@ -22,6 +22,9 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
}
|
||||
|
||||
Future<void> _submit() async {
|
||||
if (_usernameCtrl.text.trim().isEmpty || _passwordCtrl.text.isEmpty) {
|
||||
return;
|
||||
}
|
||||
await ref.read(authStateProvider.notifier).login(
|
||||
_usernameCtrl.text.trim(),
|
||||
_passwordCtrl.text,
|
||||
@@ -47,12 +50,15 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
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)
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../auth/data/auth_providers.dart';
|
||||
|
||||
class ProfileScreen extends ConsumerWidget {
|
||||
const ProfileScreen({super.key});
|
||||
|
||||
Future<void> _logout(BuildContext context, WidgetRef ref) async {
|
||||
await ref.read(authStateProvider.notifier).logout();
|
||||
if (context.mounted) context.go('/login');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Profil'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: 'Recept',
|
||||
icon: const Icon(Icons.restaurant_menu),
|
||||
onPressed: () => context.go('/recipes'),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Logga ut',
|
||||
icon: const Icon(Icons.logout),
|
||||
onPressed: () => _logout(context, ref),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: const Center(
|
||||
child: Text('Profilsida (grundversion)'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,36 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../auth/data/auth_providers.dart';
|
||||
import '../data/recipe_providers.dart';
|
||||
|
||||
class RecipesScreen extends ConsumerWidget {
|
||||
const RecipesScreen({super.key});
|
||||
|
||||
Future<void> _logout(BuildContext context, WidgetRef ref) async {
|
||||
await ref.read(authStateProvider.notifier).logout();
|
||||
if (context.mounted) context.go('/login');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final recipesAsync = ref.watch(recipesProvider);
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Recept')),
|
||||
appBar: AppBar(
|
||||
title: const Text('Recept'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: 'Profil',
|
||||
icon: const Icon(Icons.person),
|
||||
onPressed: () => context.go('/profile'),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Logga ut',
|
||||
icon: const Icon(Icons.logout),
|
||||
onPressed: () => _logout(context, ref),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: recipesAsync.when(
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (e, _) => Center(child: Text('Fel: $e')),
|
||||
|
||||
Reference in New Issue
Block a user