diff --git a/lib/common/logic/show_select.dart b/lib/common/logic/show_select.dart index 316895e3..9f6f7d7e 100644 --- a/lib/common/logic/show_select.dart +++ b/lib/common/logic/show_select.dart @@ -15,14 +15,14 @@ Future showSelectBottomSheet( List actions = const [], }) async { List? selectedIndices; + List currentSelectedIndices = List.of(getCurrentSelectedIndices()); + List choices = getChoices(); await showModalBottomSheet>( context: context, isScrollControlled: true, enableDrag: true, builder: (BuildContext context) { - List currentSelectedIndices = getCurrentSelectedIndices(); - List choices = getChoices(); return StatefulBuilder( builder: (BuildContext context, StateSetter setState) { void handleSelect(List indices) { @@ -35,7 +35,7 @@ Future showSelectBottomSheet( currentSelectedIndices.add(indices[0]); } } else { - currentSelectedIndices = indices; + currentSelectedIndices = List.of(indices); } } else { if (indices.length == 1) { @@ -61,7 +61,7 @@ Future showSelectBottomSheet( actions: actions, reload: () => setState(() { choices = getChoices(); - currentSelectedIndices = getCurrentSelectedIndices(); + currentSelectedIndices = List.of(getCurrentSelectedIndices()); }), ); }, diff --git a/test/common/widgets/fields/select_field_test.dart b/test/common/widgets/fields/select_field_test.dart index ca305c56..bc49b900 100644 --- a/test/common/widgets/fields/select_field_test.dart +++ b/test/common/widgets/fields/select_field_test.dart @@ -1,3 +1,4 @@ +import 'package:clock_app/common/logic/show_select.dart'; import 'package:clock_app/common/types/select_choice.dart'; import 'package:clock_app/common/widgets/fields/select_field/option_cards/text_option_card.dart'; import 'package:clock_app/common/widgets/fields/select_field/select_field.dart'; @@ -132,6 +133,43 @@ void main() { // } // }); }); + testWidgets('keeps multi-select state after rotation', (tester) async { + List selectedIndices = []; + tester.view.physicalSize = const Size(1080, 1920); + tester.view.devicePixelRatio = 1.0; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + + await _renderMultiSelectLauncher( + tester, + getSelectedIndices: () => selectedIndices, + onChanged: (indices) => selectedIndices = indices, + ); + await tester.tap(find.text('Open multi-select')); + await tester.pumpAndSettle(); + await tester.tap(find.byIcon(Icons.select_all_rounded)); + await tester.pumpAndSettle(); + + expect(selectedIndices, isEmpty); + final checkboxFinder = find.byType(Checkbox); + expect(checkboxFinder, findsNWidgets(choices.length)); + for (final checkbox in tester.widgetList(checkboxFinder)) { + expect(checkbox.value, isTrue); + } + + tester.view.physicalSize = const Size(1920, 1080); + await tester.pumpAndSettle(); + + expect(selectedIndices, isEmpty); + expect(checkboxFinder, findsNWidgets(choices.length)); + for (final checkbox in tester.widgetList(checkboxFinder)) { + expect(checkbox.value, isTrue); + } + + await tester.tap(find.text('Save')); + await tester.pumpAndSettle(); + expect(selectedIndices, [0, 1, 2]); + }); }); }); }); @@ -155,3 +193,37 @@ Future _renderWidget(WidgetTester tester, ), ); } + +Future _renderMultiSelectLauncher( + WidgetTester tester, { + required List Function() getSelectedIndices, + required void Function(List) onChanged, +}) async { + await tester.pumpWidget( + MaterialApp( + locale: const Locale('en'), + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Builder( + builder: (context) => Scaffold( + body: TextButton( + onPressed: () => showSelectBottomSheet( + context, + (indices) { + if (indices != null) { + onChanged(indices); + } + }, + multiSelect: true, + title: title, + description: null, + getChoices: () => choices, + getCurrentSelectedIndices: getSelectedIndices, + ), + child: const Text('Open multi-select'), + ), + ), + ), + ), + ); +}