@@ -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+ }
0 commit comments