Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename disable to enabled #274

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions lib/src/widgets/pressable/pressable_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
this.onFocusChange,
this.onPress,
this.hitTestBehavior = HitTestBehavior.opaque,
this.disabled = false,
this.enabled = true,
required this.child,
});

Expand All @@ -42,7 +42,7 @@

final Style? style;
final Widget child;
final bool disabled;
final bool enabled;
final FocusNode? focusNode;
final bool autofocus;
final Duration unpressDelay;
Expand All @@ -53,7 +53,7 @@
@override
Widget build(BuildContext context) {
return Pressable(
disabled: disabled,
enabled: enabled,
onPress: onPress,
hitTestBehavior: hitTestBehavior,
onLongPress: onLongPress,
Expand All @@ -70,7 +70,7 @@
const Pressable({
super.key,
required super.child,
super.disabled,
super.enabled,
super.enableFeedback,
super.onPress,
super.hitTestBehavior,
Expand All @@ -93,7 +93,7 @@
const _PressableBuilderWidget({
super.key,
required this.child,
this.disabled = false,
this.enabled = true,
this.enableFeedback = false,
this.onPress,
this.onLongPress,
Expand All @@ -108,7 +108,7 @@

final Widget child;

final bool disabled;
final bool enabled;

/// Should gestures provide audible and/or haptic feedback
///
Expand Down Expand Up @@ -140,7 +140,7 @@
final FocusNode? focusNode;

/// {@macro flutter.widgets.Focus.onKey}
final FocusOnKeyCallback? onKey;

Check notice on line 143 in lib/src/widgets/pressable/pressable_widget.dart

View workflow job for this annotation

GitHub Actions / Test

'FocusOnKeyCallback' is deprecated and shouldn't be used. Use FocusOnKeyEventCallback instead. This feature was deprecated after v3.18.0-2.0.pre.

Try replacing the use of the deprecated member with the replacement. See https://dart.dev/diagnostics/deprecated_member_use to learn more about this problem.

/// {@macro flutter.widgets.Focus.onKeyEvent}
final FocusOnKeyEventCallback? onKeyEvent;
Expand Down Expand Up @@ -261,11 +261,6 @@
}
}

bool get isEnabled {
return !widget.disabled &&
(widget.onPress != null || widget.onLongPress != null);
}

void handleOnPress() {
if (!mounted) return;
_handlePressUpdate(true);
Expand All @@ -283,7 +278,7 @@
@override
Widget build(BuildContext context) {
final focusableDetector = CustomFocusableActionDetector(
enabled: isEnabled,
enabled: widget.enabled,
focusNode: widget.focusNode,
autofocus: widget.autofocus,
onShowFocusHighlight: _handleFocusUpdate,
Expand All @@ -293,7 +288,7 @@
onMouseExit: _handleOnMouseExit,
onMouseHover: _handleMouseHover,
child: PressableState(
enabled: isEnabled,
enabled: widget.enabled,
hovered: _isHovered,
focused: _isFocused,
pressed: _isPressed,
Expand All @@ -304,16 +299,19 @@
);

return GestureDetector(
onTapUp: isEnabled ? (_) => _handlePressUpdate(false) : null,
onTap: isEnabled ? handleOnPress : null,
onTapCancel: isEnabled ? () => _handlePressUpdate(false) : null,
onLongPressCancel: isEnabled ? () => handleLongPressUpdate(false) : null,
onLongPress: isEnabled ? handleOnLongPress : null,
onLongPressStart: isEnabled ? (_) => handleLongPressUpdate(true) : null,
onLongPressEnd: isEnabled ? (_) => handleLongPressUpdate(false) : null,
onPanDown: isEnabled ? _handlePanDown : null,
onPanUpdate: isEnabled ? _handlePanUpdate : null,
onPanEnd: isEnabled ? _handlePanUp : null,
onTapUp: widget.enabled ? (_) => _handlePressUpdate(false) : null,
onTap: widget.enabled ? handleOnPress : null,
onTapCancel: widget.enabled ? () => _handlePressUpdate(false) : null,
onLongPressCancel:
widget.enabled ? () => handleLongPressUpdate(false) : null,
onLongPress: widget.enabled ? handleOnLongPress : null,
onLongPressStart:
widget.enabled ? (_) => handleLongPressUpdate(true) : null,
onLongPressEnd:
widget.enabled ? (_) => handleLongPressUpdate(false) : null,
onPanDown: widget.enabled ? _handlePanDown : null,
onPanUpdate: widget.enabled ? _handlePanUpdate : null,
onPanEnd: widget.enabled ? _handlePanUp : null,
behavior: widget.hitTestBehavior,
child: focusableDetector,
);
Expand Down
32 changes: 20 additions & 12 deletions test/src/widgets/pressable/pressable_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ void main() {
final thirdKey = UniqueKey();
await tester.pumpWidget(Column(
children: [
Pressable(onPress: null, child: Container(key: firstKey)),
Pressable(
disabled: true,
onPress: null,
enabled: false,
child: Container(key: firstKey),
),
Pressable(
enabled: false,
onPress: null,
child: Container(key: secondKey),
),
// Test with a onpress function
Pressable(onPress: () {}, child: Container(key: thirdKey)),
Pressable(
onPress: () {},
enabled: true,
child: Container(key: thirdKey),
),
],
));

Expand All @@ -48,13 +56,13 @@ void main() {
});

testWidgets(
'must be clickable when isDisabled is setted to false',
'must be clickable when enabled is setted to true',
(tester) async {
int counter = 0;

await tester.pumpWidget(
Pressable(
disabled: false,
enabled: true,
onPress: () {
counter++;
},
Expand All @@ -73,13 +81,13 @@ void main() {
);

testWidgets(
'must be unclickable when isDisabled is setted to true',
'must be unclickable when enabled is setted to false',
(tester) async {
int counter = 0;

await tester.pumpWidget(
Pressable(
disabled: true,
enabled: false,
onPress: () {
counter++;
},
Expand All @@ -100,7 +108,7 @@ void main() {

group('PressableBox', () {
testWidgets(
'must be clickable when isDisabled is setted to false',
'must be clickable when enable is setted to true',
(tester) async {
int counter = 0;

Expand All @@ -110,7 +118,7 @@ void main() {
onPress: () {
counter++;
},
disabled: false,
enabled: true,
child: Container(),
),
);
Expand All @@ -126,7 +134,7 @@ void main() {
);

testWidgets(
'must be unclickable when isDisabled is setted to true',
'must be unclickable when enable is setted to false',
(tester) async {
int counter = 0;

Expand All @@ -136,7 +144,7 @@ void main() {
onPress: () {
counter++;
},
disabled: true,
enabled: false,
child: Container(),
),
);
Expand Down Expand Up @@ -384,7 +392,7 @@ Future<void> pumpTestCase({
PressableBox(
unpressDelay: duration,
onPress: () {},
disabled: false,
enabled: true,
style: Style(
$with.opacity(0.5),
$box.height(50),
Expand Down
Loading