diff --git a/packages/flutter/lib/src/widgets/animated_cross_fade.dart b/packages/flutter/lib/src/widgets/animated_cross_fade.dart index fea3c5849f..1214a0fcc2 100644 --- a/packages/flutter/lib/src/widgets/animated_cross_fade.dart +++ b/packages/flutter/lib/src/widgets/animated_cross_fade.dart @@ -6,6 +6,7 @@ import 'package:flutter/rendering.dart'; import 'animated_size.dart'; import 'basic.dart'; +import 'focus_scope.dart'; import 'framework.dart'; import 'ticker_provider.dart'; import 'transitions.dart'; @@ -343,9 +344,11 @@ class _AnimatedCrossFadeState extends State with TickerProvid enabled: _isTransitioning, child: ExcludeSemantics( excluding: true, // Always exclude the semantics of the widget that's fading out. - child: FadeTransition( - opacity: bottomAnimation, - child: bottomChild, + child: ExcludeFocus( + child: FadeTransition( + opacity: bottomAnimation, + child: bottomChild, + ), ), ), ); @@ -354,9 +357,12 @@ class _AnimatedCrossFadeState extends State with TickerProvid enabled: true, // Top widget always has its animations enabled. child: ExcludeSemantics( excluding: false, // Always publish semantics for the widget that's fading in. - child: FadeTransition( - opacity: topAnimation, - child: topChild, + 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 61b5a937a9..19edcc6424 100644 --- a/packages/flutter/test/widgets/animated_cross_fade_test.dart +++ b/packages/flutter/test/widgets/animated_cross_fade_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/material.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -360,6 +361,30 @@ void main() { expect(find.text('BBB'), findsOneWidget); expect(find.text('AAA'), findsNothing); }); + + testWidgets('AnimatedCrossFade test 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), + ), + ), + ); + + 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, isFalse); + }); } class _TickerWatchingWidget extends StatefulWidget {