From 0b7fddaeaddba5dd23d3d75ae6d7d675519511e4 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Sat, 22 Jan 2022 02:15:18 +0200 Subject: [PATCH] Add ability to control if bottom child focus can be excluded in `AnimatedCrossFade` (#96593) --- .../lib/src/widgets/animated_cross_fade.dart | 12 +++++++++ .../widgets/animated_cross_fade_test.dart | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/packages/flutter/lib/src/widgets/animated_cross_fade.dart b/packages/flutter/lib/src/widgets/animated_cross_fade.dart index 94b32b0ba7..de72f09a6a 100644 --- a/packages/flutter/lib/src/widgets/animated_cross_fade.dart +++ b/packages/flutter/lib/src/widgets/animated_cross_fade.dart @@ -130,6 +130,7 @@ class AnimatedCrossFade extends StatefulWidget { required this.duration, this.reverseDuration, this.layoutBuilder = defaultLayoutBuilder, + this.excludeBottomFocus = true, }) : assert(firstChild != null), assert(secondChild != null), assert(firstCurve != null), @@ -139,6 +140,7 @@ class AnimatedCrossFade extends StatefulWidget { assert(crossFadeState != null), assert(duration != null), assert(layoutBuilder != null), + assert(excludeBottomFocus != null), super(key: key); /// The child that is visible when [crossFadeState] is @@ -206,6 +208,15 @@ class AnimatedCrossFade extends StatefulWidget { /// result in the widgets jumping about when the cross-fade state is changed. final AnimatedCrossFadeBuilder layoutBuilder; + /// When true, this is equivalent to wrapping the bottom widget with an [ExcludeFocus] + /// widget while it is at the bottom of the cross-fade stack. + /// + /// Defaults to true. When it is false, the bottom widget in the cross-fade stack + /// can remain in focus until the top widget requests focus. This is useful for + /// animating between different [TextField]s so the keyboard remains open during the + /// cross-fade animation. + final bool excludeBottomFocus; + /// The default layout algorithm used by [AnimatedCrossFade]. /// /// The top child is placed in a stack that sizes itself to match the top @@ -345,6 +356,7 @@ class _AnimatedCrossFadeState extends State with TickerProvid child: IgnorePointer( child: ExcludeSemantics( // Always exclude the semantics of the widget that's fading out. child: ExcludeFocus( + excluding: widget.excludeBottomFocus, child: FadeTransition( opacity: bottomAnimation, child: bottomChild, diff --git a/packages/flutter/test/widgets/animated_cross_fade_test.dart b/packages/flutter/test/widgets/animated_cross_fade_test.dart index 85c09afaca..7a4ac09b49 100644 --- a/packages/flutter/test/widgets/animated_cross_fade_test.dart +++ b/packages/flutter/test/widgets/animated_cross_fade_test.dart @@ -385,6 +385,31 @@ void main() { expect(hiddenNode.hasPrimaryFocus, isFalse); }); + testWidgets('AnimatedCrossFade bottom child can have focus', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: AnimatedCrossFade( + firstChild: TextButton(onPressed: () {}, child: const Text('AAA')), + secondChild: TextButton(onPressed: () {}, child: const Text('BBB')), + crossFadeState: CrossFadeState.showFirst, + duration: const Duration(milliseconds: 50), + excludeBottomFocus: false, + ), + ), + ); + + final FocusNode visibleNode = Focus.of(tester.element(find.text('AAA')), scopeOk: true); + visibleNode.requestFocus(); + await tester.pump(); + expect(visibleNode.hasPrimaryFocus, isTrue); + + final FocusNode hiddenNode = Focus.of(tester.element(find.text('BBB')), scopeOk: true); + hiddenNode.requestFocus(); + await tester.pump(); + expect(hiddenNode.hasPrimaryFocus, isTrue); + }); + testWidgets('AnimatedCrossFade second child do not receive touch events', (WidgetTester tester) async { int numberOfTouchEventNoticed = 0;