From f50b6c9d0a1614ed108a01f3b35e2fdf1bf4324c Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Mon, 29 Jan 2024 15:45:09 -0300 Subject: [PATCH 1/4] rename files to pressable --- lib/mix.dart | 4 ++-- ...ture_state.notifier.dart => pressable_state.notifier.dart} | 0 .../pressable/{gesture_widget.dart => pressable_widget.dart} | 2 +- lib/src/widgets/pressable/widget_state_util.dart | 2 +- ....notifier_test.dart => pressable_state.notifier_test.dart} | 0 .../{gesture_widget_test.dart => pressable_widget_test.dart} | 0 6 files changed, 4 insertions(+), 4 deletions(-) rename lib/src/widgets/pressable/{gesture_state.notifier.dart => pressable_state.notifier.dart} (100%) rename lib/src/widgets/pressable/{gesture_widget.dart => pressable_widget.dart} (99%) rename test/src/widgets/pressable/{gesture_state.notifier_test.dart => pressable_state.notifier_test.dart} (100%) rename test/src/widgets/pressable/{gesture_widget_test.dart => pressable_widget_test.dart} (100%) diff --git a/lib/mix.dart b/lib/mix.dart index 06360b6e5..0db6fc50e 100644 --- a/lib/mix.dart +++ b/lib/mix.dart @@ -84,6 +84,6 @@ export 'src/utils/context_variant_util/on_orientation_util.dart'; export 'src/utils/helper_util.dart'; export 'src/utils/style_recipe.dart'; export 'src/variants/variant.dart'; -export 'src/widgets/pressable/gesture_state.notifier.dart'; -export 'src/widgets/pressable/gesture_widget.dart'; +export 'src/widgets/pressable/pressable_state.notifier.dart'; +export 'src/widgets/pressable/pressable_widget.dart'; export 'src/widgets/pressable/widget_state_util.dart'; diff --git a/lib/src/widgets/pressable/gesture_state.notifier.dart b/lib/src/widgets/pressable/pressable_state.notifier.dart similarity index 100% rename from lib/src/widgets/pressable/gesture_state.notifier.dart rename to lib/src/widgets/pressable/pressable_state.notifier.dart diff --git a/lib/src/widgets/pressable/gesture_widget.dart b/lib/src/widgets/pressable/pressable_widget.dart similarity index 99% rename from lib/src/widgets/pressable/gesture_widget.dart rename to lib/src/widgets/pressable/pressable_widget.dart index 1dc0e62f7..657577e9e 100644 --- a/lib/src/widgets/pressable/gesture_widget.dart +++ b/lib/src/widgets/pressable/pressable_widget.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import '../../factory/style_mix.dart'; import '../../specs/container/box_widget.dart'; -import 'gesture_state.notifier.dart'; +import 'pressable_state.notifier.dart'; class PressableBox extends StatelessWidget { const PressableBox({ diff --git a/lib/src/widgets/pressable/widget_state_util.dart b/lib/src/widgets/pressable/widget_state_util.dart index ee848bae3..26441235d 100644 --- a/lib/src/widgets/pressable/widget_state_util.dart +++ b/lib/src/widgets/pressable/widget_state_util.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import '../../helpers/string_ext.dart'; import '../../variants/variant.dart'; -import 'gesture_state.notifier.dart'; +import 'pressable_state.notifier.dart'; /// Global context variants for handling common widget states and gestures. diff --git a/test/src/widgets/pressable/gesture_state.notifier_test.dart b/test/src/widgets/pressable/pressable_state.notifier_test.dart similarity index 100% rename from test/src/widgets/pressable/gesture_state.notifier_test.dart rename to test/src/widgets/pressable/pressable_state.notifier_test.dart diff --git a/test/src/widgets/pressable/gesture_widget_test.dart b/test/src/widgets/pressable/pressable_widget_test.dart similarity index 100% rename from test/src/widgets/pressable/gesture_widget_test.dart rename to test/src/widgets/pressable/pressable_widget_test.dart From 89c5460f0641d0b4d382fd0581384ccf872f6f4b Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:33:02 -0300 Subject: [PATCH 2/4] fix isDisable parameter in Pressable widget --- .../widgets/pressable/pressable_widget.dart | 73 +++++++----- .../pressable/pressable_widget_test.dart | 108 +++++++++++++----- 2 files changed, 121 insertions(+), 60 deletions(-) diff --git a/lib/src/widgets/pressable/pressable_widget.dart b/lib/src/widgets/pressable/pressable_widget.dart index 657577e9e..748e250e1 100644 --- a/lib/src/widgets/pressable/pressable_widget.dart +++ b/lib/src/widgets/pressable/pressable_widget.dart @@ -63,7 +63,7 @@ class Pressable extends StatefulWidget { this.onLongPress, this.unpressDelay = const Duration(), this.onPressed, - this.disabled = false, + this.isDisabled = false, super.key, }); @@ -72,7 +72,7 @@ class Pressable extends StatefulWidget { final VoidCallback? onLongPress; final FocusNode? focusNode; final bool autofocus; - final bool disabled; + final bool isDisabled; final Duration unpressDelay; final Function(bool focus)? onFocusChange; @@ -147,7 +147,7 @@ class PressableWidgetState extends State { Widget build(BuildContext context) { final currentGesture = _currentGesture; final currentStatus = - widget.disabled ? WidgetStatus.disabled : WidgetStatus.enabled; + widget.isDisabled ? WidgetStatus.disabled : WidgetStatus.enabled; final onEnabled = currentStatus == WidgetStatus.enabled; @@ -157,35 +157,48 @@ class PressableWidgetState extends State { button: true, focusable: onEnabled && _node.canRequestFocus, focused: _node.hasFocus, - child: GestureDetector( - onTapDown: (_) => updateState(() => _pressed = true), - onTapUp: (_) => handleUnpress(), - onTap: widget.onPressed, - onTapCancel: () => handleUnpress(), - onLongPressCancel: () => updateState(() => _longpressed = false), - onLongPress: widget.onLongPress, - onLongPressStart: (_) => updateState(() => _longpressed = true), - onLongPressEnd: (_) => updateState(() => _longpressed = false), - behavior: widget.behavior, - child: FocusableActionDetector( - enabled: onEnabled, - focusNode: _node, - autofocus: widget.autofocus, - onShowFocusHighlight: (v) => updateState(() => _focus = v), - onShowHoverHighlight: (v) => updateState(() => _hover = v), - onFocusChange: widget.onFocusChange, - child: WidgetStateNotifier( - data: WidgetStateData( - focus: _focus, - status: currentStatus, - state: currentGesture, - hover: _hover, + child: _buildGestureDetector( + isDisabled: widget.isDisabled, + child: FocusableActionDetector( + enabled: onEnabled, + focusNode: _node, + autofocus: widget.autofocus, + onShowFocusHighlight: (v) => updateState(() => _focus = v), + onShowHoverHighlight: (v) => updateState(() => _hover = v), + onFocusChange: widget.onFocusChange, + child: WidgetStateNotifier( + data: WidgetStateData( + focus: _focus, + status: currentStatus, + state: currentGesture, + hover: _hover, + ), + child: widget.child, ), - child: widget.child, - ), - ), - ), + )), ), ); } + + GestureDetector _buildGestureDetector({ + required bool isDisabled, + required Widget child, + }) { + return isDisabled + ? GestureDetector( + child: child, + ) + : GestureDetector( + onTapDown: (_) => updateState(() => _pressed = true), + onTapUp: (_) => handleUnpress(), + onTap: widget.onPressed, + onTapCancel: () => handleUnpress(), + onLongPressCancel: () => updateState(() => _longpressed = false), + onLongPress: widget.onLongPress, + onLongPressStart: (_) => updateState(() => _longpressed = true), + onLongPressEnd: (_) => updateState(() => _longpressed = false), + behavior: widget.behavior, + child: child, + ); + } } diff --git a/test/src/widgets/pressable/pressable_widget_test.dart b/test/src/widgets/pressable/pressable_widget_test.dart index 998ecb1b5..2526c39ac 100644 --- a/test/src/widgets/pressable/pressable_widget_test.dart +++ b/test/src/widgets/pressable/pressable_widget_test.dart @@ -5,42 +5,90 @@ import 'package:mix/mix.dart'; import '../../../helpers/testing_utils.dart'; void main() { - const attribute1 = MockIntScalarAttribute(1); - const attribute2 = MockStringScalarAttribute('attribute2'); - const attribute3 = MockBooleanScalarAttribute(true); - - testWidgets('Pressable', (tester) async { - final firstKey = UniqueKey(); - final secondKey = UniqueKey(); - await tester.pumpWidget(Column( - children: [ - Pressable( - onPressed: () {}, - child: Container( - key: firstKey, + group('Pressable', () { + const attribute1 = MockIntScalarAttribute(1); + const attribute2 = MockStringScalarAttribute('attribute2'); + const attribute3 = MockBooleanScalarAttribute(true); + + testWidgets('Pressable', (tester) async { + final firstKey = UniqueKey(); + final secondKey = UniqueKey(); + await tester.pumpWidget(Column( + children: [ + Pressable( + onPressed: () {}, + child: Container( + key: firstKey, + ), ), - ), - Pressable( - onPressed: null, - disabled: true, - child: Container( - key: secondKey, + Pressable( + onPressed: null, + isDisabled: true, + child: Container( + key: secondKey, + ), ), + ], + )); + + final onEnabledAttr = onEnabled(attribute1, attribute2, attribute3); + + final firstContext = tester.element(find.byKey(firstKey)); + final secondContext = tester.element(find.byKey(secondKey)); + + final firstNotifier = WidgetStateNotifier.of(firstContext); + final secondNotifier = WidgetStateNotifier.of(secondContext); + + expect(onEnabledAttr.when(firstContext), true); + expect(firstNotifier!.status, WidgetStatus.enabled); + expect(onEnabledAttr.when(secondContext), false); + expect(secondNotifier!.status, WidgetStatus.disabled); + }); + + testWidgets('must be clickable when isDisabled is setted to false', + (tester) async { + int counter = 0; + + await tester.pumpWidget( + Pressable( + onPressed: () { + counter++; + }, + isDisabled: false, + child: Container(), ), - ], - )); + ); + + final pressableFinder = find.byType(Pressable); + expect(pressableFinder, findsOneWidget); + + await tester.tap(pressableFinder); + await tester.pumpAndSettle(); - final onEnabledAttr = onEnabled(attribute1, attribute2, attribute3); + expect(counter, 1); + }); + + testWidgets('must be unclickable when isDisabled is setted to true', + (tester) async { + int counter = 0; + + await tester.pumpWidget( + Pressable( + onPressed: () { + counter++; + }, + isDisabled: true, + child: Container(), + ), + ); - final firstContext = tester.element(find.byKey(firstKey)); - final secondContext = tester.element(find.byKey(secondKey)); + final pressableFinder = find.byType(Pressable); + expect(pressableFinder, findsOneWidget); - final firstNotifier = WidgetStateNotifier.of(firstContext); - final secondNotifier = WidgetStateNotifier.of(secondContext); + await tester.tap(pressableFinder); + await tester.pumpAndSettle(); - expect(onEnabledAttr.when(firstContext), true); - expect(firstNotifier!.status, WidgetStatus.enabled); - expect(onEnabledAttr.when(secondContext), false); - expect(secondNotifier!.status, WidgetStatus.disabled); + expect(counter, 0); + }); }); } From a62541d46e0339347c34ae8e4bf89a9c9c036fe3 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:50:16 -0300 Subject: [PATCH 3/4] add isDisable property to PressableBox --- .../widgets/pressable/pressable_widget.dart | 3 ++ .../pressable/pressable_widget_test.dart | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/lib/src/widgets/pressable/pressable_widget.dart b/lib/src/widgets/pressable/pressable_widget.dart index 748e250e1..ec21fcabd 100644 --- a/lib/src/widgets/pressable/pressable_widget.dart +++ b/lib/src/widgets/pressable/pressable_widget.dart @@ -18,10 +18,12 @@ class PressableBox extends StatelessWidget { this.style, this.animationDuration = const Duration(milliseconds: 125), this.animationCurve = Curves.linear, + this.isDisabled = false, }); final Style? style; final Widget child; + final bool isDisabled; final VoidCallback? onPressed; final VoidCallback? onLongPress; final FocusNode? focusNode; @@ -43,6 +45,7 @@ class PressableBox extends StatelessWidget { onLongPress: onLongPress, unpressDelay: unpressDelay, onPressed: onPressed, + isDisabled: isDisabled, child: AnimatedBox( style: style, curve: animationCurve, diff --git a/test/src/widgets/pressable/pressable_widget_test.dart b/test/src/widgets/pressable/pressable_widget_test.dart index 2526c39ac..dc314ad13 100644 --- a/test/src/widgets/pressable/pressable_widget_test.dart +++ b/test/src/widgets/pressable/pressable_widget_test.dart @@ -91,4 +91,56 @@ void main() { expect(counter, 0); }); }); + + group('PressableBox', () { + testWidgets('must be clickable when isDisabled is setted to false', + (tester) async { + int counter = 0; + + await tester.pumpWidget( + PressableBox( + unpressDelay: Duration.zero, + animationDuration: Duration.zero, + onPressed: () { + counter++; + }, + isDisabled: false, + child: Container(), + ), + ); + + final pressableFinder = find.byType(PressableBox); + expect(pressableFinder, findsOneWidget); + + await tester.tap(pressableFinder); + await tester.pumpAndSettle(); + + expect(counter, 1); + }); + + testWidgets('must be unclickable when isDisabled is setted to true', + (tester) async { + int counter = 0; + + await tester.pumpWidget( + PressableBox( + onPressed: () { + counter++; + }, + unpressDelay: Duration.zero, + animationDuration: Duration.zero, + isDisabled: true, + child: Container(), + ), + ); + + final pressableFinder = find.byType(PressableBox); + expect(pressableFinder, findsOneWidget); + + await tester.tap(pressableFinder); + await tester.pumpAndSettle(); + + expect(counter, 0); + }); + }); } From 73e38b4ca5f40db797e7e4aeacbad831ccc2e8b0 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Mon, 29 Jan 2024 23:04:22 -0300 Subject: [PATCH 4/4] rename isDisabled to disabled --- lib/src/widgets/pressable/pressable_widget.dart | 14 +++++++------- .../widgets/pressable/pressable_widget_test.dart | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/src/widgets/pressable/pressable_widget.dart b/lib/src/widgets/pressable/pressable_widget.dart index ec21fcabd..e90534112 100644 --- a/lib/src/widgets/pressable/pressable_widget.dart +++ b/lib/src/widgets/pressable/pressable_widget.dart @@ -18,12 +18,12 @@ class PressableBox extends StatelessWidget { this.style, this.animationDuration = const Duration(milliseconds: 125), this.animationCurve = Curves.linear, - this.isDisabled = false, + this.disabled = false, }); final Style? style; final Widget child; - final bool isDisabled; + final bool disabled; final VoidCallback? onPressed; final VoidCallback? onLongPress; final FocusNode? focusNode; @@ -45,7 +45,7 @@ class PressableBox extends StatelessWidget { onLongPress: onLongPress, unpressDelay: unpressDelay, onPressed: onPressed, - isDisabled: isDisabled, + disabled: disabled, child: AnimatedBox( style: style, curve: animationCurve, @@ -66,7 +66,7 @@ class Pressable extends StatefulWidget { this.onLongPress, this.unpressDelay = const Duration(), this.onPressed, - this.isDisabled = false, + this.disabled = false, super.key, }); @@ -75,7 +75,7 @@ class Pressable extends StatefulWidget { final VoidCallback? onLongPress; final FocusNode? focusNode; final bool autofocus; - final bool isDisabled; + final bool disabled; final Duration unpressDelay; final Function(bool focus)? onFocusChange; @@ -150,7 +150,7 @@ class PressableWidgetState extends State { Widget build(BuildContext context) { final currentGesture = _currentGesture; final currentStatus = - widget.isDisabled ? WidgetStatus.disabled : WidgetStatus.enabled; + widget.disabled ? WidgetStatus.disabled : WidgetStatus.enabled; final onEnabled = currentStatus == WidgetStatus.enabled; @@ -161,7 +161,7 @@ class PressableWidgetState extends State { focusable: onEnabled && _node.canRequestFocus, focused: _node.hasFocus, child: _buildGestureDetector( - isDisabled: widget.isDisabled, + isDisabled: widget.disabled, child: FocusableActionDetector( enabled: onEnabled, focusNode: _node, diff --git a/test/src/widgets/pressable/pressable_widget_test.dart b/test/src/widgets/pressable/pressable_widget_test.dart index dc314ad13..edbe3ad62 100644 --- a/test/src/widgets/pressable/pressable_widget_test.dart +++ b/test/src/widgets/pressable/pressable_widget_test.dart @@ -23,7 +23,7 @@ void main() { ), Pressable( onPressed: null, - isDisabled: true, + disabled: true, child: Container( key: secondKey, ), @@ -54,7 +54,7 @@ void main() { onPressed: () { counter++; }, - isDisabled: false, + disabled: false, child: Container(), ), ); @@ -77,7 +77,7 @@ void main() { onPressed: () { counter++; }, - isDisabled: true, + disabled: true, child: Container(), ), ); @@ -104,7 +104,7 @@ void main() { onPressed: () { counter++; }, - isDisabled: false, + disabled: false, child: Container(), ), ); @@ -129,7 +129,7 @@ void main() { }, unpressDelay: Duration.zero, animationDuration: Duration.zero, - isDisabled: true, + disabled: true, child: Container(), ), );