From e6f2bc8637abf192874aca1a4bac63acfc9f3a34 Mon Sep 17 00:00:00 2001 From: William Oprandi Date: Wed, 17 Mar 2021 22:40:04 +0100 Subject: [PATCH] FocusableActionDetector now exposes descendantsAreFocusable property (#77288) --- packages/flutter/lib/src/widgets/actions.dart | 5 +++ .../flutter/test/widgets/actions_test.dart | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/packages/flutter/lib/src/widgets/actions.dart b/packages/flutter/lib/src/widgets/actions.dart index bb2000f541..1dd926350a 100644 --- a/packages/flutter/lib/src/widgets/actions.dart +++ b/packages/flutter/lib/src/widgets/actions.dart @@ -1246,6 +1246,7 @@ class FocusableActionDetector extends StatefulWidget { this.enabled = true, this.focusNode, this.autofocus = false, + this.descendantsAreFocusable = true, this.shortcuts, this.actions, this.onShowFocusHighlight, @@ -1274,6 +1275,9 @@ class FocusableActionDetector extends StatefulWidget { /// {@macro flutter.widgets.Focus.autofocus} final bool autofocus; + /// {@macro flutter.widgets.Focus.descendantsAreFocusable} + final bool descendantsAreFocusable; + /// {@macro flutter.widgets.actions.actions} final Map>? actions; @@ -1459,6 +1463,7 @@ class _FocusableActionDetectorState extends State { child: Focus( focusNode: widget.focusNode, autofocus: widget.autofocus, + descendantsAreFocusable: widget.descendantsAreFocusable, canRequestFocus: _canRequestFocus, onFocusChange: _handleFocusChange, child: widget.child, diff --git a/packages/flutter/test/widgets/actions_test.dart b/packages/flutter/test/widgets/actions_test.dart index 7b764c665a..3fa24f44c8 100644 --- a/packages/flutter/test/widgets/actions_test.dart +++ b/packages/flutter/test/widgets/actions_test.dart @@ -919,6 +919,50 @@ void main() { expect(hovering, isFalse); expect(focusing, isFalse); }); + + testWidgets( + 'FocusableActionDetector can prevent its descendants from being focusable', + (WidgetTester tester) async { + final FocusNode buttonNode = FocusNode(debugLabel: 'Test'); + + await tester.pumpWidget( + MaterialApp( + home: FocusableActionDetector( + descendantsAreFocusable: true, + child: MaterialButton( + focusNode: buttonNode, + child: const Text('Test'), + onPressed: () {}, + ), + ), + ), + ); + + // Button is focusable + expect(buttonNode.hasFocus, isFalse); + buttonNode.requestFocus(); + await tester.pump(); + expect(buttonNode.hasFocus, isTrue); + + await tester.pumpWidget( + MaterialApp( + home: FocusableActionDetector( + descendantsAreFocusable: false, + child: MaterialButton( + focusNode: buttonNode, + child: const Text('Test'), + onPressed: () {}, + ), + ), + ), + ); + + // Button is NOT focusable + expect(buttonNode.hasFocus, isFalse); + buttonNode.requestFocus(); + await tester.pump(); + expect(buttonNode.hasFocus, isFalse); + }); }); group('Diagnostics', () {