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
18 changes: 17 additions & 1 deletion packages/material_ui/lib/src/dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

Expand Down Expand Up @@ -1588,8 +1589,23 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
final Orientation newOrientation = _getOrientation(context);
_lastOrientation ??= newOrientation;
if (newOrientation != _lastOrientation) {
_removeDropdownRoute();
_lastOrientation = newOrientation;
// The open menu's layout is computed for the previous orientation, so it
// must be dismissed. Removing the route mutates the Navigator, which is
// not allowed during build, so defer the dismissal until after the
// current frame when we are in the persistent callbacks phase.
// See https://github.com/flutter/flutter/issues/171011
if (_dropdownRoute != null) {
if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks) {
WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
if (mounted) {
_removeDropdownRoute();
}
}, debugLabel: 'DropdownButton.dismissOnOrientationChange');
} else {
_removeDropdownRoute();
}
}
}

// The width of the button and the menu are defined by the widest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog: |
- Fixes a crash when a DropdownButton menu is open inside a dialog and the device orientation changes.
version: patch
59 changes: 59 additions & 0 deletions packages/material_ui/test/dropdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,65 @@ void main() {
},
);

testWidgets(
'Dropdown menu opened inside a dialog is dismissed on orientation change without throwing',
(WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/171011
addTearDown(tester.view.reset);
tester.view.devicePixelRatio = 1.0;
tester.view.physicalSize = const Size(800, 600);

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: DropdownButton<int>(
value: 1,
onChanged: (int? value) {},
items: const <DropdownMenuItem<int>>[
DropdownMenuItem<int>(value: 1, child: Text('1')),
DropdownMenuItem<int>(value: 2, child: Text('2')),
DropdownMenuItem<int>(value: 3, child: Text('3')),
],
),
);
},
);
},
child: const Text('Open dialog'),
),
);
},
),
),
),
);

// Open the dialog, then open the dropdown menu inside it.
await tester.tap(find.text('Open dialog'));
await tester.pumpAndSettle();
await tester.tap(find.byType(DropdownButton<int>));
await tester.pumpAndSettle();
expect(find.byType(ListView), findsOneWidget);

// Rotate the device (simulate by swapping the physical size dimensions).
tester.view.physicalSize = const Size(600, 800);
await tester.pumpAndSettle();

// The route should be dismissed without mutating the Navigator during build.
expect(tester.takeException(), isNull);
expect(find.byType(ListView), findsNothing);
},
);

testWidgets('Semantics Tree contains only selected element', (WidgetTester tester) async {
final semantics = SemanticsTester(tester);
await tester.pumpWidget(buildFrame(onChanged: onChanged));
Expand Down