import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../../core/api/api_error_mapper.dart'; import '../../data/profile_repository.dart'; class ProfileScreen extends ConsumerStatefulWidget { const ProfileScreen({super.key}); @override ConsumerState createState() => _ProfileScreenState(); } class _ProfileScreenState extends ConsumerState { final _formKey = GlobalKey(); String _username = ''; String _email = ''; bool _isLoading = true; @override void initState() { super.initState(); _loadProfile(); } Future _loadProfile() async { try { final profile = await ref.read(profileRepositoryProvider).getProfile(); setState(() { _username = profile['username'] ?? ''; _email = profile['email'] ?? ''; _isLoading = false; }); } catch (e) { _showErrorMessage(e); setState(() { _isLoading = false; }); } } Future _updateProfile() async { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); setState(() { _isLoading = true; }); try { await ref.read(profileRepositoryProvider).updateProfile({ 'username': _username, 'email': _email, }); ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Profil uppdaterad!')), ); } catch (e) { _showErrorMessage(e); } finally { setState(() { _isLoading = false; }); } } } void _showErrorMessage(dynamic error) { final message = mapErrorToUserMessage(error, context); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(message)), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Användarprofil'), ), body: _isLoading ? const Center(child: CircularProgressIndicator()) : Padding( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: Column( children: [ TextFormField( decoration: const InputDecoration(labelText: 'Användarnamn'), initialValue: _username, validator: (value) { if (value == null || value.isEmpty) { return 'Ange ett användarnamn'; } return null; }, onSaved: (value) => _username = value!, ), TextFormField( decoration: const InputDecoration(labelText: 'E-post'), initialValue: _email, validator: (value) { if (value == null || value.isEmpty) { return 'Ange en e-postadress'; } if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) { return 'Ange en giltig e-postadress'; } return null; }, onSaved: (value) => _email = value!, ), const SizedBox(height: 20), ElevatedButton( onPressed: _updateProfile, child: const Text('Spara'), ), ], ), ), ), ); } }