Files
packages.dart/experimental/pocketbase_auth/lib/src/ui/update_profile_screen.dart
T
2026-01-15 14:38:46 -08:00

120 lines
3.6 KiB
Dart

import 'package:flutter/material.dart';
import '../auth_service.dart';
import '../view_models/update_profile_view_model.dart';
class PocketBaseUpdateProfileScreen extends StatefulWidget {
final AuthService authService;
const PocketBaseUpdateProfileScreen({super.key, required this.authService});
@override
State<PocketBaseUpdateProfileScreen> createState() =>
_PocketBaseUpdateProfileScreenState();
}
class _PocketBaseUpdateProfileScreenState
extends State<PocketBaseUpdateProfileScreen> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
late final UpdateProfileViewModel _viewModel;
bool _isInit = true;
@override
void initState() {
super.initState();
_viewModel = UpdateProfileViewModel(widget.authService);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_isInit) {
final user = widget.authService.currentUser;
if (user != null) {
_nameController.text = user.getStringValue('name');
}
_isInit = false;
}
}
Future<void> _updateProfile() async {
if (!_formKey.currentState!.validate()) return;
try {
await _viewModel.updateProfile(name: _nameController.text.trim());
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Profile updated successfully')),
);
}
} catch (_) {
// Error handled by view model
}
}
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: _viewModel,
builder: (context, _) {
return Scaffold(
appBar: AppBar(title: const Text('Update Profile')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (_viewModel.errorMessage != null)
Padding(
padding: const EdgeInsets.only(bottom: 16.0),
child: Text(
_viewModel.errorMessage!,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
textAlign: TextAlign.center,
),
),
TextFormField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Name',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your name';
}
return null;
},
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: _viewModel.isLoading ? null : _updateProfile,
child: _viewModel.isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Update Profile'),
),
],
),
),
),
);
},
);
}
@override
void dispose() {
_nameController.dispose();
_viewModel.dispose();
super.dispose();
}
}