From 874804e511109a4028fd9b874836e5f82fe44103 Mon Sep 17 00:00:00 2001 From: Faisal Ansari Date: Fri, 8 Mar 2024 02:16:03 +0530 Subject: [PATCH] =?UTF-8?q?Fixed=20->=20DropdownMenu=20throws=20exception?= =?UTF-8?q?=20when=20it=20is=20in=20any=20scrollable=20l=E2=80=A6=20(#1405?= =?UTF-8?q?66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed -> DropdownMenu throws exception when it is in any scrollable list view and scrolls quickly #139871 --- .../lib/src/material/dropdown_menu.dart | 3 ++ .../test/material/dropdown_menu_test.dart | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 0ec91f143ab..58d34be5dd8 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -493,6 +493,9 @@ class _DropdownMenuState extends State> { void refreshLeadingPadding() { WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted) { + return; + } setState(() { leadingPadding = getWidth(_leadingKey); }); diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index f3ba1aeff20..097e4ddd206 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -1910,6 +1910,34 @@ void main() { expect(tester.takeException(), isNull); }); + // Regression test for https://github.com/flutter/flutter/issues/139871. + testWidgets('setState is not called through addPostFrameCallback after DropdownMenu is unmounted', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ListView.builder( + itemCount: 500, + itemBuilder: (BuildContext context, int index) { + if (index == 250) { + return DropdownMenu( + dropdownMenuEntries: menuChildren, + ); + } else { + return Container(height: 50); + } + }, + ), + ), + ), + ); + + await tester.fling(find.byType(ListView), const Offset(0, -20000), 200000.0); + + await tester.pumpAndSettle(); + + expect(tester.takeException(), isNull); + }); + testWidgets('Menu shows scrollbar when height is limited', (WidgetTester tester) async { final List> menuItems = >[ DropdownMenuEntry(