Skip to content

Commit

Permalink
#73 - feature(training-plan): add unique key to editable table field
Browse files Browse the repository at this point in the history
When swapping between different training plans the old state was sometimes kept due to flutter not being able to swap the displayed elements.

This resulted in a corrupted state which is now avoided, due to flutter being able to properly link the swapped elements and render objects with the widget.
  • Loading branch information
sdresselmann committed May 20, 2024
1 parent 3a8bd00 commit 00c6607
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
13 changes: 8 additions & 5 deletions lib/training_plan/widgets/editable_table_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import 'package:get/get_rx/src/rx_workers/rx_workers.dart';
import 'package:lifting_progress_tracker/core/constants/timer.dart';

class EditableTableField extends StatelessWidget {
final Rx<String> _userInput = "".obs;
final Rx<String> _userInput;
final String initialValue;

EditableTableField({required this.initialValue});
EditableTableField({super.key, required this.initialValue})
: _userInput = initialValue.obs;

@override
Widget build(BuildContext context) {
setupDebounce();
_setupDebounce();

return TextFormField(
initialValue: initialValue,
Expand All @@ -21,10 +22,12 @@ class EditableTableField extends StatelessWidget {
);
}

void setupDebounce() {
void _setupDebounce() {
debounce(
_userInput,
(String input) {},
(String input) {
// Do callback on user input
},
time: const Duration(milliseconds: textFieldDebounceTime),
);
}
Expand Down
20 changes: 16 additions & 4 deletions lib/training_plan/widgets/selected_training_plan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ class SelectedTrainingPlan extends StatelessWidget {
for (final entry in selectedPlan.planEntries.entries)
TableRow(
children: [
EditableTableField(initialValue: entry.value.exerciseName),
EditableTableField(initialValue: entry.value.weight),
EditableTableField(initialValue: entry.value.repeats),
EditableTableField(initialValue: entry.value.comment),
EditableTableField(
key: UniqueKey(),
initialValue: entry.value.exerciseName,
),
EditableTableField(
key: UniqueKey(),
initialValue: entry.value.weight,
),
EditableTableField(
key: UniqueKey(),
initialValue: entry.value.repeats,
),
EditableTableField(
key: UniqueKey(),
initialValue: entry.value.comment,
),
IconButton(
onPressed: () {
controller.removeEntry(entry.key);
Expand Down

0 comments on commit 00c6607

Please sign in to comment.