diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index ea585561f5..6b495fa88b 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -674,7 +674,12 @@ class _DatePickerDialogState extends State with RestorationMix // Constrain the textScaleFactor to the largest supported value to prevent // layout issues. maxScaleFactor: _kMaxTextScaleFactor, - child: Builder(builder: (BuildContext context) { + child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) { + final Size portraitDialogSize = useMaterial3 ? _inputPortraitDialogSizeM3 : _inputPortraitDialogSizeM2; + // Make sure the portrait dialog can fit the contents comfortably when + // resized from the landscape dialog. + final bool isFullyPortrait = constraints.maxHeight >= portraitDialogSize.height; + switch (orientation) { case Orientation.portrait: return Column( @@ -683,8 +688,10 @@ class _DatePickerDialogState extends State with RestorationMix children: [ header, if (useMaterial3) Divider(height: 0, color: datePickerTheme.dividerColor), - Expanded(child: picker), - actions, + if (isFullyPortrait) ...[ + Expanded(child: picker), + actions, + ], ], ); case Orientation.landscape: @@ -2775,14 +2782,25 @@ class _InputDateRangePickerDialog extends StatelessWidget { switch (orientation) { case Orientation.portrait: - return Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - header, - Expanded(child: picker), - actions, - ], + return LayoutBuilder( + builder: (BuildContext context, BoxConstraints constraints) { + final Size portraitDialogSize = useMaterial3 ? _inputPortraitDialogSizeM3 : _inputPortraitDialogSizeM2; + // Make sure the portrait dialog can fit the contents comfortably when + // resized from the landscape dialog. + final bool isFullyPortrait = constraints.maxHeight >= portraitDialogSize.height; + + return Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + header, + if (isFullyPortrait) ...[ + Expanded(child: picker), + actions, + ], + ], + ); + } ); case Orientation.landscape: diff --git a/packages/flutter/test/material/date_picker_test.dart b/packages/flutter/test/material/date_picker_test.dart index 4c47543d14..66cddc20c9 100644 --- a/packages/flutter/test/material/date_picker_test.dart +++ b/packages/flutter/test/material/date_picker_test.dart @@ -1469,6 +1469,22 @@ void main() { expect(tester.takeException(), null); }); }); + + // This is a regression test for https://github.com/flutter/flutter/issues/131989. + testWidgets('Dialog contents do not overflow when resized from landscape to portrait', + (WidgetTester tester) async { + addTearDown(tester.view.reset); + // Initial window size is wide for landscape mode. + tester.view.physicalSize = wideWindowSize; + tester.view.devicePixelRatio = 1.0; + + await prepareDatePicker(tester, (Future date) async { + // Change window size to narrow for portrait mode. + tester.view.physicalSize = narrowWindowSize; + await tester.pump(); + expect(tester.takeException(), null); + }); + }); }); group('Semantics', () { diff --git a/packages/flutter/test/material/date_range_picker_test.dart b/packages/flutter/test/material/date_range_picker_test.dart index 6bd0e10eff..14f7b2e8ad 100644 --- a/packages/flutter/test/material/date_range_picker_test.dart +++ b/packages/flutter/test/material/date_range_picker_test.dart @@ -53,6 +53,9 @@ void main() { saveText = null; }); + const Size wideWindowSize = Size(1920.0, 1080.0); + const Size narrowWindowSize = Size(1070.0, 1770.0); + Future preparePicker( WidgetTester tester, Future Function(Future date) callback, { @@ -1065,6 +1068,22 @@ void main() { // Test the end date text field testInputDecorator(tester.widget(borderContainers.last), border, Colors.transparent); }); + + // This is a regression test for https://github.com/flutter/flutter/issues/131989. + testWidgets('Dialog contents do not overflow when resized from landscape to portrait', + (WidgetTester tester) async { + addTearDown(tester.view.reset); + // Initial window size is wide for landscape mode. + tester.view.physicalSize = wideWindowSize; + tester.view.devicePixelRatio = 1.0; + + await preparePicker(tester, (Future range) async { + // Change window size to narrow for portrait mode. + tester.view.physicalSize = narrowWindowSize; + await tester.pump(); + expect(tester.takeException(), null); + }); + }); }); testWidgets('DatePickerDialog is state restorable', (WidgetTester tester) async {