Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/common/logic/show_select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ Future<void> showSelectBottomSheet(
List<MenuAction> actions = const [],
}) async {
List<int>? selectedIndices;
List<int> currentSelectedIndices = List.of(getCurrentSelectedIndices());
List<SelectChoice> choices = getChoices();

await showModalBottomSheet<List<int>>(
context: context,
isScrollControlled: true,
enableDrag: true,
builder: (BuildContext context) {
List<int> currentSelectedIndices = getCurrentSelectedIndices();
List<SelectChoice> choices = getChoices();
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
void handleSelect(List<int> indices) {
Expand All @@ -35,7 +35,7 @@ Future<void> showSelectBottomSheet(
currentSelectedIndices.add(indices[0]);
}
} else {
currentSelectedIndices = indices;
currentSelectedIndices = List.of(indices);
}
} else {
if (indices.length == 1) {
Expand All @@ -61,7 +61,7 @@ Future<void> showSelectBottomSheet(
actions: actions,
reload: () => setState(() {
choices = getChoices();
currentSelectedIndices = getCurrentSelectedIndices();
currentSelectedIndices = List.of(getCurrentSelectedIndices());
}),
);
},
Expand Down
72 changes: 72 additions & 0 deletions test/common/widgets/fields/select_field_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -132,6 +133,43 @@ void main() {
// }
// });
});
testWidgets('keeps multi-select state after rotation', (tester) async {
List<int> selectedIndices = <int>[];
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<Checkbox>(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<Checkbox>(checkboxFinder)) {
expect(checkbox.value, isTrue);
}

await tester.tap(find.text('Save'));
await tester.pumpAndSettle();
expect(selectedIndices, [0, 1, 2]);
});
});
});
});
Expand All @@ -155,3 +193,37 @@ Future<void> _renderWidget(WidgetTester tester,
),
);
}

Future<void> _renderMultiSelectLauncher(
WidgetTester tester, {
required List<int> Function() getSelectedIndices,
required void Function(List<int>) 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'),
),
),
),
),
);
}