Updated Settings Page
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pweb/models/edit_state.dart';
|
||||
import 'package:pshared/provider/account.dart';
|
||||
|
||||
class AccountName extends StatefulWidget {
|
||||
final String name;
|
||||
@@ -26,8 +30,9 @@ class _AccountNameState extends State<AccountName> {
|
||||
static const double _borderWidth = 2;
|
||||
|
||||
late final TextEditingController _controller;
|
||||
bool _isEditing = false;
|
||||
EditState _editState = EditState.view;
|
||||
late String _originalName;
|
||||
String _errorText = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -42,86 +47,131 @@ class _AccountNameState extends State<AccountName> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _startEditing() => setState(() => _isEditing = true);
|
||||
void _startEditing() => setState(() => _editState = EditState.edit);
|
||||
|
||||
void _cancelEditing() {
|
||||
setState(() {
|
||||
_controller.text = _originalName;
|
||||
_isEditing = false;
|
||||
_editState = EditState.view;
|
||||
_errorText = '';
|
||||
});
|
||||
}
|
||||
|
||||
void _saveEditing() {
|
||||
Future<void> _saveEditing(AccountProvider provider) async {
|
||||
final newName = _controller.text.trim();
|
||||
if (newName.isEmpty || newName == _originalName) {
|
||||
_cancelEditing();
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_originalName = _controller.text;
|
||||
_isEditing = false;
|
||||
_editState = EditState.saving;
|
||||
_errorText = '';
|
||||
});
|
||||
|
||||
try {
|
||||
await provider.resetUsername(newName);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_originalName = newName;
|
||||
_editState = EditState.view;
|
||||
});
|
||||
} catch (_) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_errorText = widget.errorText;
|
||||
_editState = EditState.edit;
|
||||
});
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(widget.errorText)),
|
||||
);
|
||||
return;
|
||||
} finally {
|
||||
if (!mounted) return;
|
||||
if (_editState == EditState.saving) {
|
||||
setState(() => _editState = EditState.edit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
return Consumer<AccountProvider>(
|
||||
builder: (context, provider, _) {
|
||||
final isEditing = _editState != EditState.view;
|
||||
final currentName = provider.account?.name ?? _originalName;
|
||||
final isBusy = provider.isLoading || _editState == EditState.saving;
|
||||
|
||||
if (!isEditing && currentName != _originalName) {
|
||||
_originalName = currentName;
|
||||
_controller.text = currentName;
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (_isEditing)
|
||||
SizedBox(
|
||||
width: _inputWidth,
|
||||
child: TextFormField(
|
||||
controller: _controller,
|
||||
style: theme.textTheme.headlineMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
autofocus: true,
|
||||
decoration: InputDecoration(
|
||||
hintText: widget.hintText,
|
||||
isDense: true,
|
||||
border: UnderlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: theme.colorScheme.primary,
|
||||
width: _borderWidth,
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
if (isEditing)
|
||||
SizedBox(
|
||||
width: _inputWidth,
|
||||
child: TextFormField(
|
||||
controller: _controller,
|
||||
style: theme.textTheme.headlineMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
autofocus: true,
|
||||
enabled: !isBusy,
|
||||
decoration: InputDecoration(
|
||||
hintText: widget.hintText,
|
||||
isDense: true,
|
||||
border: UnderlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: theme.colorScheme.primary,
|
||||
width: _borderWidth,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
Text(
|
||||
_originalName,
|
||||
style: theme.textTheme.headlineMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
const SizedBox(width: _spacing),
|
||||
if (isEditing) ...[
|
||||
IconButton(
|
||||
icon: Icon(Icons.check, color: theme.colorScheme.primary),
|
||||
onPressed: isBusy ? null : () => _saveEditing(provider),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(Icons.close, color: theme.colorScheme.error),
|
||||
onPressed: isBusy ? null : _cancelEditing,
|
||||
),
|
||||
] else
|
||||
IconButton(
|
||||
icon: Icon(Icons.edit, color: theme.colorScheme.primary),
|
||||
onPressed: isBusy ? null : _startEditing,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: _errorSpacing),
|
||||
if (_errorText.isNotEmpty)
|
||||
Text(
|
||||
_originalName,
|
||||
style: theme.textTheme.headlineMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
_errorText,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.error,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: _spacing),
|
||||
if (_isEditing) ...[
|
||||
IconButton(
|
||||
icon: Icon(Icons.check, color: theme.colorScheme.primary),
|
||||
onPressed: _saveEditing,
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(Icons.close, color: theme.colorScheme.error),
|
||||
onPressed: _cancelEditing,
|
||||
),
|
||||
] else
|
||||
IconButton(
|
||||
icon: Icon(Icons.edit, color: theme.colorScheme.primary),
|
||||
onPressed: _startEditing,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: _errorSpacing),
|
||||
if (widget.errorText.isEmpty)
|
||||
Text(
|
||||
widget.errorText,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.error,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user