Add ability to control if bottom child focus can be excluded in AnimatedCrossFade (#96593)

This commit is contained in:
Taha Tesser
2022-01-22 02:15:18 +02:00
committed by GitHub
parent ec37a4eb02
commit 0b7fddaead
2 changed files with 37 additions and 0 deletions

View File

@@ -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<AnimatedCrossFade> 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,

View File

@@ -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;