From 8f3696303018d4a30cc4af8e7fcb59b48d5418e5 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Tue, 2 Jul 2024 18:49:43 -0300 Subject: [PATCH 1/2] Update pressable_widget.dart --- .../widget_state/pressable_widget.dart | 121 ++++++++++-------- 1 file changed, 67 insertions(+), 54 deletions(-) diff --git a/packages/mix/lib/src/widgets/widget_state/pressable_widget.dart b/packages/mix/lib/src/widgets/widget_state/pressable_widget.dart index dec89b3da..2636a8a7b 100644 --- a/packages/mix/lib/src/widgets/widget_state/pressable_widget.dart +++ b/packages/mix/lib/src/widgets/widget_state/pressable_widget.dart @@ -167,6 +167,12 @@ abstract class _MixStateWidgetBuilderState ); int _pressCount = 0; + late final Map> _actionMap = >{ + ActivateIntent: CallbackAction(onInvoke: activateOnIntent), + ButtonActivateIntent: + CallbackAction(onInvoke: activateOnIntent), + }; + void _handleFocusUpdate(bool hasFocus) { updateState(() => _isFocused = hasFocus); } @@ -235,6 +241,31 @@ abstract class _MixStateWidgetBuilderState } } + void _onTapUp(TapUpDetails details) { + if (_preventEvent) return; + _handlePressUpdate(false); + } + + void _onTapCancel() { + if (_preventEvent) return; + _handlePressUpdate(false); + } + + void _onLongPressStart(LongPressStartDetails details) { + if (_preventEvent) return; + handleLongPressUpdate(true); + } + + void _onLongPressEnd(LongPressEndDetails details) { + if (_preventEvent) return; + handleLongPressUpdate(false); + } + + void _onLongPressCancel() { + if (_preventEvent) return; + handleLongPressUpdate(false); + } + void handleLongPressUpdate(bool isLongPressed) { if (isLongPressed == _isLongPressed) return; @@ -265,31 +296,6 @@ abstract class _MixStateWidgetBuilderState if (widget.enableFeedback) Feedback.forTap(context); } - void _onTapUp(TapUpDetails details) { - if (_preventEvent) return; - _handlePressUpdate(false); - } - - void _onTapCancel() { - if (_preventEvent) return; - _handlePressUpdate(false); - } - - void _onLongPressStart(LongPressStartDetails details) { - if (_preventEvent) return; - handleLongPressUpdate(true); - } - - void _onLongPressEnd(LongPressEndDetails details) { - if (_preventEvent) return; - handleLongPressUpdate(false); - } - - void _onLongPressCancel() { - if (_preventEvent) return; - handleLongPressUpdate(false); - } - void handleOnLongPress() { if (_preventEvent) return; @@ -297,38 +303,45 @@ abstract class _MixStateWidgetBuilderState if (widget.enableFeedback) Feedback.forLongPress(context); } + void activateOnIntent(Intent? intent) { + handleOnPress(); + } + @override Widget build(BuildContext context) { - return GestureDetector( - onTapUp: _onTapUp, - onTap: handleOnPress, - onTapCancel: _onTapCancel, - onLongPressCancel: _onLongPressCancel, - onLongPress: handleOnLongPress, - onLongPressStart: _onLongPressStart, - onLongPressEnd: _onLongPressEnd, - onPanDown: _handlePanDown, - onPanUpdate: _handlePanUpdate, - onPanEnd: _handlePanUp, - behavior: widget.hitTestBehavior, - child: CustomFocusableActionDetector( - enabled: widget.enabled, - focusNode: widget.focusNode, - autofocus: widget.autofocus, - onShowFocusHighlight: _handleFocusUpdate, - onShowHoverHighlight: _handleHoverUpdate, - onFocusChange: widget.onFocusChange, - onMouseEnter: _handleOnMouseEnter, - onMouseExit: _handleOnMouseExit, - onMouseHover: _handleMouseHover, - child: WidgetStateModel( + return Actions( + actions: _actionMap, + child: GestureDetector( + onTapUp: _onTapUp, + onTap: handleOnPress, + onTapCancel: _onTapCancel, + onLongPressCancel: _onLongPressCancel, + onLongPress: handleOnLongPress, + onLongPressStart: _onLongPressStart, + onLongPressEnd: _onLongPressEnd, + onPanDown: _handlePanDown, + onPanUpdate: _handlePanUpdate, + onPanEnd: _handlePanUp, + behavior: widget.hitTestBehavior, + child: CustomFocusableActionDetector( enabled: widget.enabled, - hovered: _isHovered, - focused: _isFocused, - pressed: _isPressed, - longPressed: _isLongPressed, - pointerPosition: _pointerPosition, - child: widget.child, + focusNode: widget.focusNode, + autofocus: widget.autofocus, + onShowFocusHighlight: _handleFocusUpdate, + onShowHoverHighlight: _handleHoverUpdate, + onFocusChange: widget.onFocusChange, + onMouseEnter: _handleOnMouseEnter, + onMouseExit: _handleOnMouseExit, + onMouseHover: _handleMouseHover, + child: WidgetStateModel( + enabled: widget.enabled, + hovered: _isHovered, + focused: _isFocused, + pressed: _isPressed, + longPressed: _isLongPressed, + pointerPosition: _pointerPosition, + child: widget.child, + ), ), ), ); From 44053d911d26e72b68bdffa3d6d0c52a12261f23 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Tue, 2 Jul 2024 18:59:54 -0300 Subject: [PATCH 2/2] Update pressable_widget_test.dart --- .../pressable/pressable_widget_test.dart | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/mix/test/src/widgets/pressable/pressable_widget_test.dart b/packages/mix/test/src/widgets/pressable/pressable_widget_test.dart index a4bac5f85..fe66ea3a9 100644 --- a/packages/mix/test/src/widgets/pressable/pressable_widget_test.dart +++ b/packages/mix/test/src/widgets/pressable/pressable_widget_test.dart @@ -1,5 +1,6 @@ import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mix/mix.dart'; @@ -104,6 +105,30 @@ void main() { expect(counter, 0); }, ); + + testWidgets('Pressable responds to keyboard events', + (WidgetTester tester) async { + var wasPressed = false; + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Pressable( + autofocus: true, + unpressDelay: Duration.zero, + onPress: () { + wasPressed = true; + }, + child: const Text('Tap me'), + ), + ), + ), + ); + + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + + expect(wasPressed, isTrue); + }); }); group('PressableBox', () { @@ -159,6 +184,30 @@ void main() { }, ); + testWidgets('PressableBox responds to keyboard events', + (WidgetTester tester) async { + var wasPressed = false; + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: PressableBox( + autofocus: true, + unpressDelay: Duration.zero, + onPress: () { + wasPressed = true; + }, + child: const Text('Tap me'), + ), + ), + ), + ); + + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + + expect(wasPressed, isTrue); + }); + testWidgets(r'must change to attributes in $on.hover variant when hovered', (WidgetTester tester) async { await pumpTestCase(