From 12ec91a5a426fb7ac843dc983a15c7056f330baa Mon Sep 17 00:00:00 2001 From: Aliaksei Chorny Date: Thu, 21 Oct 2021 00:28:01 +0300 Subject: [PATCH] The covered child in AnimatedCrossFade should not get touch events (#91349) --- .../lib/src/widgets/animated_cross_fade.dart | 29 +++++++----- .../widgets/animated_cross_fade_test.dart | 47 +++++++++++++++++++ 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/packages/flutter/lib/src/widgets/animated_cross_fade.dart b/packages/flutter/lib/src/widgets/animated_cross_fade.dart index c999be4d38..94b32b0ba7 100644 --- a/packages/flutter/lib/src/widgets/animated_cross_fade.dart +++ b/packages/flutter/lib/src/widgets/animated_cross_fade.dart @@ -342,11 +342,13 @@ class _AnimatedCrossFadeState extends State with TickerProvid bottomChild = TickerMode( key: bottomKey, enabled: _isTransitioning, - child: ExcludeSemantics( // Always exclude the semantics of the widget that's fading out. - child: ExcludeFocus( - child: FadeTransition( - opacity: bottomAnimation, - child: bottomChild, + child: IgnorePointer( + child: ExcludeSemantics( // Always exclude the semantics of the widget that's fading out. + child: ExcludeFocus( + child: FadeTransition( + opacity: bottomAnimation, + child: bottomChild, + ), ), ), ), @@ -354,13 +356,16 @@ class _AnimatedCrossFadeState extends State with TickerProvid topChild = TickerMode( key: topKey, enabled: true, // Top widget always has its animations enabled. - child: ExcludeSemantics( - excluding: false, // Always publish semantics for the widget that's fading in. - child: ExcludeFocus( - excluding: false, - child: FadeTransition( - opacity: topAnimation, - child: topChild, + child: IgnorePointer( + ignoring: false, + child: ExcludeSemantics( + excluding: false, // Always publish semantics for the widget that's fading in. + child: ExcludeFocus( + excluding: false, + child: FadeTransition( + opacity: topAnimation, + child: topChild, + ), ), ), ), diff --git a/packages/flutter/test/widgets/animated_cross_fade_test.dart b/packages/flutter/test/widgets/animated_cross_fade_test.dart index fd029e0068..85c09afaca 100644 --- a/packages/flutter/test/widgets/animated_cross_fade_test.dart +++ b/packages/flutter/test/widgets/animated_cross_fade_test.dart @@ -384,6 +384,53 @@ void main() { await tester.pump(); expect(hiddenNode.hasPrimaryFocus, isFalse); }); + + testWidgets('AnimatedCrossFade second child do not receive touch events', + (WidgetTester tester) async { + int numberOfTouchEventNoticed = 0; + + Future buildAnimatedFrame(CrossFadeState crossFadeState) { + return tester.pumpWidget( + SizedBox( + width: 300, + height: 600, + child: Directionality( + textDirection: TextDirection.ltr, + child: AnimatedCrossFade( + firstChild: const Text('AAA'), + secondChild: TextButton( + style: TextButton.styleFrom(minimumSize: const Size(double.infinity, 600)), + onPressed: () { + numberOfTouchEventNoticed++; + }, + child: const Text('BBB'), + ), + crossFadeState: crossFadeState, + duration: const Duration(milliseconds: 50), + ), + ), + ), + ); + } + + Future touchSecondButton() async { + final TestGesture gestureTouchSecondButton = await tester + .startGesture(const Offset(150, 300)); + + return gestureTouchSecondButton.up(); + } + + await buildAnimatedFrame(CrossFadeState.showSecond); + + await touchSecondButton(); + expect(numberOfTouchEventNoticed, 1); + + await buildAnimatedFrame(CrossFadeState.showFirst); + await touchSecondButton(); + await touchSecondButton(); + + expect(numberOfTouchEventNoticed, 1); + }); } class _TickerWatchingWidget extends StatefulWidget {