From 6dc3bfaa98e13c0f174e0cc229e7b43678d4d0c0 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Fri, 7 Feb 2020 18:28:02 -0800 Subject: [PATCH] Fix Dropdown `'itemHeights' was called on null` crash (#50366) --- .../flutter/lib/src/material/dropdown.dart | 13 ++++- .../flutter/test/material/dropdown_test.dart | 56 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index d13a90ef55..d6675efbb5 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -1143,6 +1143,17 @@ class _DropdownButtonState extends State> with WidgetsBindi menuItems[index] = _MenuItem( item: widget.items[index], onLayout: (Size size) { + // If [_dropdownRoute] is null and onLayout is called, this means + // that performLayout was called on a _DropdownRoute that has not + // left the widget tree but is already on its way out. + // + // Since onLayout is used primarily to collect the desired heights + // of each menu item before laying them out, not having the _DropdownRoute + // collect each item's height to lay out is fine since the route is + // already on its way out. + if (_dropdownRoute == null) + return; + _dropdownRoute.itemHeights[index] = size.height; }, ); @@ -1162,7 +1173,7 @@ class _DropdownButtonState extends State> with WidgetsBindi ); Navigator.push(context, _dropdownRoute).then((_DropdownRouteResult newValue) { - _dropdownRoute = null; + _removeDropdownRoute(); if (!mounted || newValue == null) return; if (widget.onChanged != null) diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index e00add869d..4a41053f7f 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -1915,6 +1915,62 @@ void main() { expect(tester.getCenter(item40.first).dy, tester.getCenter(item40.last).dy); }); + testWidgets('DropdownButton menu items do not resize when its route is popped', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/44877. + const List items = [ + 'one', + 'two', + 'three', + ]; + String item = items[0]; + MediaQueryData mediaQuery; + + await tester.pumpWidget( + StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MaterialApp( + builder: (BuildContext context, Widget child) { + mediaQuery ??= MediaQuery.of(context); + return MediaQuery( + data: mediaQuery, + child: child, + ); + }, + home: Scaffold( + body: DropdownButton( + value: item, + items: items.map((String item) => DropdownMenuItem( + value: item, + child: Text(item), + )).toList(), + onChanged: (String newItem) { + setState(() { + item = newItem; + mediaQuery = mediaQuery.copyWith( + textScaleFactor: mediaQuery.textScaleFactor + 0.1, + ); + }); + }, + ), + ), + ); + }, + ), + ); + + // Verify that the first item is showing. + expect(find.text('one'), findsOneWidget); + + // Select a different item to trigger setState, which updates mediaQuery + // and forces a performLayout on the popped _DropdownRoute. This operation + // should not cause an exception. + await tester.tap(find.text('one')); + await tester.pumpAndSettle(); + await tester.tap(find.text('two').last); + await tester.pumpAndSettle(); + expect(find.text('two'), findsOneWidget); + }); + testWidgets('DropdownButton hint is selected item', (WidgetTester tester) async { const double hintPaddingOffset = 8; const List itemValues = ['item0', 'item1', 'item2', 'item3'];