Skip to content

Commit fabbf56

Browse files
committed
- improve form state management with text controllers
Add text editing controllers for form fields to enable programmatic updates Update reset functionality to reflect changes immediately in UI
1 parent a7238ac commit fabbf56

2 files changed

Lines changed: 58 additions & 8 deletions

File tree

lib/widgets/settings_form.dart

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,35 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
1919
bool _autoStartWork = false;
2020
int _sessionsUntilLongBreak = 4;
2121
bool _isLoading = true;
22+
23+
// Controllers for text fields to enable programmatic updates
24+
late TextEditingController _workDurationController;
25+
late TextEditingController _shortBreakDurationController;
26+
late TextEditingController _longBreakDurationController;
27+
late TextEditingController _sessionsController;
2228

2329
@override
2430
void initState() {
2531
super.initState();
32+
_initializeControllers();
2633
_loadSettings();
2734
}
35+
36+
void _initializeControllers() {
37+
_workDurationController = TextEditingController(text: _workDuration.toString());
38+
_shortBreakDurationController = TextEditingController(text: _shortBreakDuration.toString());
39+
_longBreakDurationController = TextEditingController(text: _longBreakDuration.toString());
40+
_sessionsController = TextEditingController(text: _sessionsUntilLongBreak.toString());
41+
}
42+
43+
@override
44+
void dispose() {
45+
_workDurationController.dispose();
46+
_shortBreakDurationController.dispose();
47+
_longBreakDurationController.dispose();
48+
_sessionsController.dispose();
49+
super.dispose();
50+
}
2851

2952
Future<void> _loadSettings() async {
3053
try {
@@ -39,6 +62,9 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
3962
_sessionsUntilLongBreak = settings['sessionsUntilLongBreak'];
4063
_isLoading = false;
4164
});
65+
66+
// Update controllers to reflect loaded values
67+
_updateControllers();
4268
}
4369
} catch (e) {
4470
print('Error loading settings: $e');
@@ -47,6 +73,13 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
4773
}
4874
}
4975
}
76+
77+
void _updateControllers() {
78+
_workDurationController.text = _workDuration.toString();
79+
_shortBreakDurationController.text = _shortBreakDuration.toString();
80+
_longBreakDurationController.text = _longBreakDuration.toString();
81+
_sessionsController.text = _sessionsUntilLongBreak.toString();
82+
}
5083

5184
Future<void> _saveSettings() async {
5285
if (_formKey.currentState!.validate()) {
@@ -94,18 +127,21 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
94127
'Work Duration (minutes)',
95128
_workDuration,
96129
(value) => setState(() => _workDuration = value),
130+
controller: _workDurationController,
97131
),
98132
const SizedBox(height: 16),
99133
_buildDurationField(
100134
'Short Break Duration (minutes)',
101135
_shortBreakDuration,
102136
(value) => setState(() => _shortBreakDuration = value),
137+
controller: _shortBreakDurationController,
103138
),
104139
const SizedBox(height: 16),
105140
_buildDurationField(
106141
'Long Break Duration (minutes)',
107142
_longBreakDuration,
108143
(value) => setState(() => _longBreakDuration = value),
144+
controller: _longBreakDurationController,
109145
),
110146
const SizedBox(height: 16),
111147
_buildDurationField(
@@ -114,6 +150,7 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
114150
(value) => setState(() => _sessionsUntilLongBreak = value),
115151
min: 1,
116152
max: 10,
153+
controller: _sessionsController,
117154
),
118155
const SizedBox(height: 16),
119156
_buildSwitchField(
@@ -165,7 +202,22 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
165202
if (confirm == true) {
166203
await SettingsService.resetToDefaults();
167204

168-
// Immediately update the timer service
205+
// Update local state to reflect the reset values
206+
if (mounted) {
207+
setState(() {
208+
_workDuration = 25;
209+
_shortBreakDuration = 5;
210+
_longBreakDuration = 15;
211+
_autoStartBreak = true;
212+
_autoStartWork = true;
213+
_sessionsUntilLongBreak = 4;
214+
});
215+
216+
// Update controllers to show changes instantly in UI
217+
_updateControllers();
218+
}
219+
220+
// Update the timer service
169221
final timerService = ref.read(timerServiceProvider.notifier);
170222
timerService.updateSettings(
171223
newWorkDuration: 25,
@@ -179,11 +231,8 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
179231
// Reset the timer to initial state
180232
timerService.resetTimer();
181233

182-
// Pop back to previous screen immediately
234+
// Show success message
183235
if (mounted) {
184-
Navigator.of(context).pop();
185-
186-
// Show snackbar on the main screen
187236
ScaffoldMessenger.of(context).showSnackBar(
188237
const SnackBar(
189238
content: Text('Settings reset to defaults'),
@@ -210,13 +259,14 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
210259
ValueChanged<int> onChanged, {
211260
int? min,
212261
int? max,
262+
TextEditingController? controller,
213263
}) {
214264
return TextFormField(
215265
decoration: InputDecoration(
216266
labelText: label,
217267
border: const OutlineInputBorder(),
218268
),
219-
initialValue: value.toString(),
269+
controller: controller,
220270
keyboardType: TextInputType.number,
221271
validator: (value) {
222272
if (value == null || value.isEmpty) {
@@ -248,4 +298,4 @@ class SettingsFormState extends ConsumerState<SettingsForm> {
248298
onChanged: onChanged,
249299
);
250300
}
251-
}
301+
}

macos/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ EXTERNAL SOURCES:
5353
SPEC CHECKSUMS:
5454
audioplayers_darwin: dcad41de4fbd0099cb3749f7ab3b0cb8f70b810c
5555
bitsdojo_window_macos: 44e3b8fe3dd463820e0321f6256c5b1c16bb6a00
56-
flutter_local_notifications: 3805ca215b2fb7f397d78b66db91f6a747af52e4
56+
flutter_local_notifications: 4b427ffabf278fc6ea9484c97505e231166927a5
5757
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
5858
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
5959
screen_retriever: 59634572a57080243dd1bf715e55b6c54f241a38

0 commit comments

Comments
 (0)