Unverified Commit 73f7945b authored by xubaolin's avatar xubaolin Committed by GitHub

fix DropdownMenu crash when resize window during menu showing (#124855)

parent ea34f7df
...@@ -542,7 +542,6 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> { ...@@ -542,7 +542,6 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
controller: _controller, controller: _controller,
menuChildren: menu, menuChildren: menu,
crossAxisUnconstrained: false, crossAxisUnconstrained: false,
onClose: () { setState(() {}); }, // To update the status of the IconButton
builder: (BuildContext context, MenuController controller, Widget? child) { builder: (BuildContext context, MenuController controller, Widget? child) {
assert(_initialMenu != null); assert(_initialMenu != null);
final Widget trailingButton = Padding( final Widget trailingButton = Padding(
......
...@@ -342,7 +342,7 @@ class _MenuAnchorState extends State<MenuAnchor> { ...@@ -342,7 +342,7 @@ class _MenuAnchorState extends State<MenuAnchor> {
// Needs to update the overlay entry on the next frame, since it's in the // Needs to update the overlay entry on the next frame, since it's in the
// overlay. // overlay.
SchedulerBinding.instance.addPostFrameCallback((Duration _) { SchedulerBinding.instance.addPostFrameCallback((Duration _) {
_overlayEntry!.markNeedsBuild(); _overlayEntry?.markNeedsBuild();
}); });
} }
} }
...@@ -556,8 +556,9 @@ class _MenuAnchorState extends State<MenuAnchor> { ...@@ -556,8 +556,9 @@ class _MenuAnchorState extends State<MenuAnchor> {
// Notify that _childIsOpen changed state, but only if not // Notify that _childIsOpen changed state, but only if not
// currently disposing. // currently disposing.
_parent?._childChangedOpenState(); _parent?._childChangedOpenState();
widget.onClose?.call();
setState(() {});
} }
widget.onClose?.call();
} }
void _closeChildren({bool inDispose = false}) { void _closeChildren({bool inDispose = false}) {
......
...@@ -405,6 +405,70 @@ void main() { ...@@ -405,6 +405,70 @@ void main() {
expect(menuMaterial, findsOneWidget); expect(menuMaterial, findsOneWidget);
}); });
testWidgets('Leading IconButton status test', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(useMaterial3: true);
await tester.pumpWidget(buildTest(themeData, menuChildren, width: 100.0, menuHeight: 100.0));
await tester.pump();
Finder iconButton = find.widgetWithIcon(IconButton, Icons.arrow_drop_up);
expect(iconButton, findsNothing);
iconButton = find.widgetWithIcon(IconButton, Icons.arrow_drop_down).first;
expect(iconButton, findsOneWidget);
await tester.tap(iconButton);
await tester.pump();
iconButton = find.widgetWithIcon(IconButton, Icons.arrow_drop_up).first;
expect(iconButton, findsOneWidget);
iconButton = find.widgetWithIcon(IconButton, Icons.arrow_drop_down);
expect(iconButton, findsNothing);
// Tap outside
await tester.tapAt(const Offset(500.0, 500.0));
await tester.pump();
iconButton = find.widgetWithIcon(IconButton, Icons.arrow_drop_up);
expect(iconButton, findsNothing);
iconButton = find.widgetWithIcon(IconButton, Icons.arrow_drop_down).first;
expect(iconButton, findsOneWidget);
});
testWidgets('Do not crash when resize window during menu opening', (WidgetTester tester) async {
addTearDown(tester.view.reset);
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: StatefulBuilder(
builder: (BuildContext context, StateSetter setState){
return DropdownMenu<TestMenu>(
width: MediaQuery.of(context).size.width,
dropdownMenuEntries: menuChildren,
);
},
),
),
));
final Finder iconButton = find.widgetWithIcon(IconButton, Icons.arrow_drop_down).first;
expect(iconButton, findsOneWidget);
await tester.tap(iconButton);
await tester.pump();
final Finder menuMaterial = find.ancestor(
of: find.widgetWithText(MenuItemButton, TestMenu.mainMenu0.label),
matching: find.byType(Material),
).last;
expect(menuMaterial, findsOneWidget);
// didChangeMetrics
tester.view.physicalSize = const Size(700.0, 700.0);
await tester.pump();
// Go without throw.
});
testWidgets('DropdownMenu can customize trailing icon button', (WidgetTester tester) async { testWidgets('DropdownMenu can customize trailing icon button', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(); final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp( await tester.pumpWidget(MaterialApp(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment