From 37f562fa3682ccfb030f8bfe64974cc2fc8cdb9f Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Fri, 29 May 2020 15:57:05 -0700 Subject: [PATCH] Remove callback asserts on FocusableActionDetector (#58272) This makes the callback arguments to FocusableActionDetector optional, if you (for instance) only want to define shortcuts and actions and a focus node for something. --- packages/flutter/lib/src/widgets/actions.dart | 2 - .../flutter/test/widgets/actions_test.dart | 39 ++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/widgets/actions.dart b/packages/flutter/lib/src/widgets/actions.dart index 4c2dd95b7b0..60e753f41cc 100644 --- a/packages/flutter/lib/src/widgets/actions.dart +++ b/packages/flutter/lib/src/widgets/actions.dart @@ -985,7 +985,6 @@ class _FocusableActionDetectorState extends State { bool _hovering = false; void _handleMouseEnter(PointerEnterEvent event) { - assert(widget.onShowHoverHighlight != null); if (!_hovering) { _mayTriggerCallback(task: () { _hovering = true; @@ -994,7 +993,6 @@ class _FocusableActionDetectorState extends State { } void _handleMouseExit(PointerExitEvent event) { - assert(widget.onShowHoverHighlight != null); if (_hovering) { _mayTriggerCallback(task: () { _hovering = false; diff --git a/packages/flutter/test/widgets/actions_test.dart b/packages/flutter/test/widgets/actions_test.dart index 78ef49150de..4a51de1be28 100644 --- a/packages/flutter/test/widgets/actions_test.dart +++ b/packages/flutter/test/widgets/actions_test.dart @@ -600,6 +600,7 @@ void main() { WidgetTester tester, { bool enabled = true, bool directional = false, + bool supplyCallbacks = true, @required Key key, }) async { await tester.pumpWidget( @@ -620,8 +621,8 @@ void main() { actions: >{ TestIntent: testAction, }, - onShowHoverHighlight: (bool value) => hovering = value, - onShowFocusHighlight: (bool value) => focusing = value, + onShowHoverHighlight: supplyCallbacks ? (bool value) => hovering = value : null, + onShowFocusHighlight: supplyCallbacks ? (bool value) => focusing = value : null, child: Container(width: 100, height: 100, key: key), ), ), @@ -709,6 +710,40 @@ void main() { await tester.pump(); expect(focusing, isTrue); }); + testWidgets('FocusableActionDetector can be used without callbacks', (WidgetTester tester) async { + FocusManager.instance.highlightStrategy = FocusHighlightStrategy.alwaysTraditional; + final GlobalKey containerKey = GlobalKey(); + + await pumpTest(tester, enabled: true, key: containerKey, supplyCallbacks: false); + focusNode.requestFocus(); + await tester.pump(); + final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + addTearDown(gesture.removePointer); + await gesture.moveTo(tester.getCenter(find.byKey(containerKey))); + await tester.pump(); + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + expect(hovering, isFalse); + expect(focusing, isFalse); + expect(invoked, isTrue); + + invoked = false; + await pumpTest(tester, enabled: false, key: containerKey, supplyCallbacks: false); + expect(hovering, isFalse); + expect(focusing, isFalse); + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.pump(); + expect(invoked, isFalse); + await pumpTest(tester, enabled: true, key: containerKey, supplyCallbacks: false); + expect(focusing, isFalse); + expect(hovering, isFalse); + await pumpTest(tester, enabled: false, key: containerKey, supplyCallbacks: false); + expect(focusing, isFalse); + expect(hovering, isFalse); + await gesture.moveTo(Offset.zero); + await pumpTest(tester, enabled: true, key: containerKey, supplyCallbacks: false); + expect(hovering, isFalse); + expect(focusing, isFalse); + }); }); group('Diagnostics', () {