FocusableActionDetector now exposes descendantsAreFocusable property (#77288)

This commit is contained in:
William Oprandi 2021-03-17 22:40:04 +01:00 committed by GitHub
parent b35fbfec1d
commit e6f2bc8637
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View file

@ -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<Type, Action<Intent>>? actions;
@ -1459,6 +1463,7 @@ class _FocusableActionDetectorState extends State<FocusableActionDetector> {
child: Focus(
focusNode: widget.focusNode,
autofocus: widget.autofocus,
descendantsAreFocusable: widget.descendantsAreFocusable,
canRequestFocus: _canRequestFocus,
onFocusChange: _handleFocusChange,
child: widget.child,

View file

@ -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', () {