diff --git a/lib/widgets/message_translation_button.dart b/lib/widgets/message_translation_button.dart index 1ccfb42f..90e07fa8 100644 --- a/lib/widgets/message_translation_button.dart +++ b/lib/widgets/message_translation_button.dart @@ -41,31 +41,33 @@ Future showMessageTranslationSheet({ required ValueChanged onEnabledChanged, required ValueChanged onLanguageSelected, }) { - final callerMedia = MediaQuery.of(context); - final navMedia = - MediaQuery.maybeOf(Navigator.of(context).context) ?? callerMedia; - final callerBottom = math.max( - callerMedia.viewInsets.bottom, - math.max(callerMedia.viewPadding.bottom, callerMedia.padding.bottom), - ); - final navBottom = math.max( - navMedia.viewInsets.bottom, - math.max(navMedia.viewPadding.bottom, navMedia.padding.bottom), - ); - final extraBottom = math.max(0.0, callerBottom - navBottom); - final safeTop = math.max( - math.max(callerMedia.viewPadding.top, callerMedia.padding.top), - math.max(navMedia.viewPadding.top, navMedia.padding.top), - ); - final bottomSafeInset = math.max( - math.max(callerMedia.viewPadding.bottom, callerMedia.padding.bottom), - math.max(navMedia.viewPadding.bottom, navMedia.padding.bottom), - ); - return showMeshSheet( context, builder: (sheetContext) { final sheetMedia = MediaQuery.of(sheetContext); + final callerMedia = + (context.mounted ? MediaQuery.maybeOf(context) : null) ?? sheetMedia; + final navContext = Navigator.maybeOf(sheetContext)?.context; + final navMedia = + (navContext != null ? MediaQuery.maybeOf(navContext) : null) ?? + callerMedia; + final callerBottom = math.max( + callerMedia.viewInsets.bottom, + math.max(callerMedia.viewPadding.bottom, callerMedia.padding.bottom), + ); + final navBottom = math.max( + navMedia.viewInsets.bottom, + math.max(navMedia.viewPadding.bottom, navMedia.padding.bottom), + ); + final extraBottom = math.max(0.0, callerBottom - navBottom); + final safeTop = math.max( + math.max(callerMedia.viewPadding.top, callerMedia.padding.top), + math.max(navMedia.viewPadding.top, navMedia.padding.top), + ); + final bottomSafeInset = math.max( + math.max(callerMedia.viewPadding.bottom, callerMedia.padding.bottom), + math.max(navMedia.viewPadding.bottom, navMedia.padding.bottom), + ); final screenHeight = math.min( callerMedia.size.height, sheetMedia.size.height, diff --git a/test/widgets/message_translation_button_test.dart b/test/widgets/message_translation_button_test.dart index 5030298b..06cbdd8d 100644 --- a/test/widgets/message_translation_button_test.dart +++ b/test/widgets/message_translation_button_test.dart @@ -54,13 +54,7 @@ Future _openSheet( builder: (context, child) { final media = MediaQuery.of(context); return MediaQuery( - data: media.copyWith( - size: size, - padding: padding, - viewPadding: viewPadding, - viewInsets: viewInsets, - textScaler: TextScaler.linear(textScale), - ), + data: media.copyWith(textScaler: TextScaler.linear(textScale)), child: child!, ); }, @@ -128,6 +122,48 @@ void main() { }, ); + testWidgets( + 'sheet recomputes height cap and expands when keyboard inset closes', + (tester) async { + await _openSheet( + tester, + size: const Size(320, 568), + padding: const EdgeInsets.only(top: 48), + viewPadding: const EdgeInsets.only(top: 48, bottom: 34), + viewInsets: const EdgeInsets.only(bottom: 300), + ); + + final listFinder = find.byType(CustomScrollView); + expect(listFinder, findsOneWidget); + final compressedHeaderTop = tester + .getTopLeft(find.byType(BottomSheetHeader)) + .dy; + final compressedBottom = tester.getBottomLeft(listFinder).dy; + final compressedContentHeight = compressedBottom - compressedHeaderTop; + expect(compressedContentHeight, lessThanOrEqualTo(220.0)); + + // Dismiss the keyboard while the sheet remains open. + tester.view.viewInsets = FakeViewPadding.zero; + tester.view.padding = const FakeViewPadding(top: 48, bottom: 34); + await tester.pumpAndSettle(); + + final expandedHeaderTop = tester + .getTopLeft(find.byType(BottomSheetHeader)) + .dy; + final expandedListFinder = find.byType(ListView); + expect(expandedListFinder, findsOneWidget); + final expandedBottom = tester.getBottomLeft(expandedListFinder).dy; + final expandedContentHeight = expandedBottom - expandedHeaderTop; + + expect(expandedContentHeight, greaterThan(compressedContentHeight)); + expect(expandedContentHeight, greaterThan(400.0)); + expect( + tester.getRect(find.byType(BottomSheet)).height, + greaterThan(450.0), + ); + }, + ); + testWidgets( 'toggling switch, filtering languages, and selecting target or app language works', (tester) async {