Skip to content

Commit

Permalink
feat: improve textfield clear behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasn committed Jun 16, 2024
1 parent 642b34a commit 9a2862d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
42 changes: 41 additions & 1 deletion lib/features/tasks/ui/checklist_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,50 @@ class ChecklistWidget extends StatefulWidget {
}

class _ChecklistWidgetState extends State<ChecklistWidget> {
bool _isEditing = false;

@override
Widget build(BuildContext context) {
return ExpansionTile(
title: const Text('Checklist'),
title: AnimatedCrossFade(
duration: const Duration(milliseconds: 200),
firstChild: Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: TitleTextField(
initialValue: 'Checklist',
onSave: (title) {
debugPrint('Saved: $title');
setState(() {
_isEditing = false;
});
},
resetToInitialValue: true,
onClear: () {
setState(() {
_isEditing = false;
});
},
),
),
secondChild: Row(
children: [
const Text('Checklist'),
IconButton(
icon: const Icon(
Icons.edit,
size: 20,
),
onPressed: () {
setState(() {
_isEditing = !_isEditing;
});
},
),
],
),
crossFadeState:
_isEditing ? CrossFadeState.showFirst : CrossFadeState.showSecond,
),
subtitle: const LinearProgressIndicator(
value: 0.87,
semanticsLabel: 'Checklist progress',
Expand Down
25 changes: 19 additions & 6 deletions lib/features/tasks/ui/title_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TitleTextField extends StatefulWidget {
required this.onSave,
this.onClear,
this.clearOnSave = false,
this.resetToInitialValue = false,
this.initialValue,
this.semanticsLabel,
super.key,
Expand All @@ -19,6 +20,7 @@ class TitleTextField extends StatefulWidget {
final VoidCallback? onClear;
final String? semanticsLabel;
final bool clearOnSave;
final bool resetToInitialValue;

@override
State<TitleTextField> createState() => _TitleTextFieldState();
Expand All @@ -40,13 +42,28 @@ class _TitleTextFieldState extends State<TitleTextField> {

@override
Widget build(BuildContext context) {
final initialValue = widget.initialValue;

void onSave(String? value) {
widget.onSave(value ?? _controller.text);
if (widget.clearOnSave) {
_controller.clear();
}
setState(() {
_showClearButton = false;
_showClearButton = widget.resetToInitialValue;
_dirty = false;
});
}

void onClear() {
if (widget.resetToInitialValue && initialValue != null) {
_controller.text = initialValue;
} else {
_controller.clear();
}
widget.onClear?.call();
setState(() {
_showClearButton = widget.resetToInitialValue;
_dirty = false;
});
}
Expand Down Expand Up @@ -92,11 +109,7 @@ class _TitleTextFieldState extends State<TitleTextField> {
size: 30,
semanticLabel: 'discard changes',
),
onPressed: () {
_controller.clear();
widget.onClear?.call();
setState(() => _showClearButton = false);
},
onPressed: onClear,
),
),
],
Expand Down

0 comments on commit 9a2862d

Please sign in to comment.