From 8dcae5ace426c2cd50f3d9751e6a65ad60131abf Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Thu, 11 Jan 2024 20:26:04 +0200 Subject: [PATCH] Fix `ListWheelScrollView` in an `AnimatedContainer` with zero height throw an error (#141372) fixes [`ListWheelScrollView` Throws Unexpected Error Inside `AnimatedContainer`](https://github.com/flutter/flutter/issues/140780) fixes [`CupertinoDatePicker` throw exception when parent height is 0](https://github.com/flutter/flutter/issues/55630) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( body: AnimatedContainer( height: 0, duration: Duration.zero, child: ListWheelScrollView( itemExtent: 20.0, children: [ for (int i = 0; i < 20; i++) Container(), ], ), ), ), ); } } ```
--- .../src/rendering/list_wheel_viewport.dart | 2 +- .../widgets/list_wheel_scroll_view_test.dart | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/rendering/list_wheel_viewport.dart b/packages/flutter/lib/src/rendering/list_wheel_viewport.dart index fa6077dac4b9..321e8279fe0f 100644 --- a/packages/flutter/lib/src/rendering/list_wheel_viewport.dart +++ b/packages/flutter/lib/src/rendering/list_wheel_viewport.dart @@ -879,7 +879,7 @@ class RenderListWheelViewport // renderChildrenOutsideViewport is true. Otherwise, only children within // suitable angles (via _first/lastVisibleLayoutOffset) reach the paint // phase. - if (angle > math.pi / 2.0 || angle < -math.pi / 2.0) { + if (angle > math.pi / 2.0 || angle < -math.pi / 2.0 || angle.isNaN) { return; } diff --git a/packages/flutter/test/widgets/list_wheel_scroll_view_test.dart b/packages/flutter/test/widgets/list_wheel_scroll_view_test.dart index 90bfe7900545..8eb69a85ceac 100644 --- a/packages/flutter/test/widgets/list_wheel_scroll_view_test.dart +++ b/packages/flutter/test/widgets/list_wheel_scroll_view_test.dart @@ -8,8 +8,8 @@ library; import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; -import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import '../rendering/rendering_tester.dart' show TestCallbackPainter, TestClipPaintingContext; @@ -1883,4 +1883,28 @@ void main() { expect(tester.layers.whereType(), hasLength(1)); }); + + // This is a regression test for https://github.com/flutter/flutter/issues/140780. + testWidgets('ListWheelScrollView in an AnimatedContainer with zero height does not throw an error', + (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: AnimatedContainer( + height: 0, + duration: Duration.zero, + child: ListWheelScrollView( + itemExtent: 20.0, + children: [ + for (int i = 0; i < 20; i++) + Container(), + ], + ), + ), + ), + ), + ); + + expect(tester.takeException(), isNull); + }); }