payfrit-app/lib/screens/profile_settings_screen.dart
John Mizerek 2491c961e0 Add address management and user account features
- Add delivery address list, add, edit, delete, set default functionality
- Add order history screen
- Add profile settings screen
- Add account screen with avatar upload
- Update restaurant select gradient direction
- Add states API endpoint for address forms
- Fix table names (tt_States)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-08 20:22:37 -08:00

288 lines
7.9 KiB
Dart

import 'package:flutter/material.dart';
import '../models/user_profile.dart';
import '../services/api.dart';
class ProfileSettingsScreen extends StatefulWidget {
const ProfileSettingsScreen({super.key});
@override
State<ProfileSettingsScreen> createState() => _ProfileSettingsScreenState();
}
class _ProfileSettingsScreenState extends State<ProfileSettingsScreen> {
final _formKey = GlobalKey<FormState>();
final _firstNameController = TextEditingController();
final _lastNameController = TextEditingController();
bool _isLoading = true;
bool _isSaving = false;
String? _error;
UserProfile? _profile;
@override
void initState() {
super.initState();
_loadProfile();
}
@override
void dispose() {
_firstNameController.dispose();
_lastNameController.dispose();
super.dispose();
}
Future<void> _loadProfile() async {
setState(() {
_isLoading = true;
_error = null;
});
try {
final profile = await Api.getProfile();
if (mounted) {
setState(() {
_profile = profile;
_firstNameController.text = profile.firstName;
_lastNameController.text = profile.lastName;
_isLoading = false;
});
}
} catch (e) {
debugPrint('Error loading profile: $e');
if (mounted) {
setState(() {
_error = e.toString();
_isLoading = false;
});
}
}
}
Future<void> _saveProfile() async {
if (!_formKey.currentState!.validate()) return;
setState(() => _isSaving = true);
try {
final updatedProfile = await Api.updateProfile(
firstName: _firstNameController.text.trim(),
lastName: _lastNameController.text.trim(),
);
if (mounted) {
setState(() {
_profile = updatedProfile;
_isSaving = false;
});
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Profile updated'),
backgroundColor: Colors.green,
),
);
Navigator.of(context).pop();
}
} catch (e) {
debugPrint('Error saving profile: $e');
if (mounted) {
setState(() => _isSaving = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Failed to save: $e'),
backgroundColor: Colors.red,
),
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Profile Settings'),
actions: [
if (!_isLoading && _error == null)
TextButton(
onPressed: _isSaving ? null : _saveProfile,
child: _isSaving
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Save'),
),
],
),
body: _buildBody(),
);
}
Widget _buildBody() {
if (_isLoading) {
return const Center(child: CircularProgressIndicator());
}
if (_error != null) {
return Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.error_outline,
size: 48,
color: Theme.of(context).colorScheme.error,
),
const SizedBox(height: 16),
Text(
'Failed to load profile',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(
_error!,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 24),
FilledButton.icon(
onPressed: _loadProfile,
icon: const Icon(Icons.refresh),
label: const Text('Retry'),
),
],
),
),
);
}
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Editable fields
TextFormField(
controller: _firstNameController,
decoration: const InputDecoration(
labelText: 'First Name',
border: OutlineInputBorder(),
),
textCapitalization: TextCapitalization.words,
validator: (value) {
if (value == null || value.trim().isEmpty) {
return 'Please enter your first name';
}
return null;
},
),
const SizedBox(height: 16),
TextFormField(
controller: _lastNameController,
decoration: const InputDecoration(
labelText: 'Last Name',
border: OutlineInputBorder(),
),
textCapitalization: TextCapitalization.words,
validator: (value) {
if (value == null || value.trim().isEmpty) {
return 'Please enter your last name';
}
return null;
},
),
const SizedBox(height: 24),
// Read-only fields
if (_profile != null) ...[
Text(
'Contact Information',
style: Theme.of(context).textTheme.titleSmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 8),
_ReadOnlyField(
label: 'Email',
value: _profile!.email.isNotEmpty ? _profile!.email : 'Not set',
icon: Icons.email_outlined,
),
const SizedBox(height: 8),
_ReadOnlyField(
label: 'Phone',
value: _profile!.phone.isNotEmpty ? _profile!.phone : 'Not set',
icon: Icons.phone_outlined,
),
const SizedBox(height: 16),
Text(
'Contact info can only be changed through customer support.',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
],
),
),
);
}
}
class _ReadOnlyField extends StatelessWidget {
final String label;
final String value;
final IconData icon;
const _ReadOnlyField({
required this.label,
required this.value,
required this.icon,
});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Icon(
icon,
size: 20,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
Text(
value,
style: Theme.of(context).textTheme.bodyMedium,
),
],
),
),
],
),
);
}
}