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

Add disabled property in PressableBox #183

Merged
merged 4 commits into from
Jan 30, 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
4 changes: 2 additions & 2 deletions lib/mix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -18,10 +18,12 @@ class PressableBox extends StatelessWidget {
this.style,
this.animationDuration = const Duration(milliseconds: 125),
this.animationCurve = Curves.linear,
this.disabled = false,
});

final Style? style;
final Widget child;
final bool disabled;
final VoidCallback? onPressed;
final VoidCallback? onLongPress;
final FocusNode? focusNode;
Expand All @@ -43,6 +45,7 @@ class PressableBox extends StatelessWidget {
onLongPress: onLongPress,
unpressDelay: unpressDelay,
onPressed: onPressed,
disabled: disabled,
child: AnimatedBox(
style: style,
curve: animationCurve,
Expand Down Expand Up @@ -157,35 +160,48 @@ class PressableWidgetState extends State<Pressable> {
button: true,
focusable: onEnabled && _node.canRequestFocus,
focused: _node.hasFocus,
child: GestureDetector(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the disabled should behave only for the onTap behavior, we also should not modify the element in the tree through the function returning a widget.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change the function. However, about the disabled state only affects onTap. Are you sure? In my opinion when disabled is true, all actions involving the Pressable should be deactivated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tilucasoli makes sense!

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.disabled,
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,
);
}
}
2 changes: 1 addition & 1 deletion lib/src/widgets/pressable/widget_state_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
46 changes: 0 additions & 46 deletions test/src/widgets/pressable/gesture_widget_test.dart

This file was deleted.

146 changes: 146 additions & 0 deletions test/src/widgets/pressable/pressable_widget_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mix/mix.dart';

import '../../../helpers/testing_utils.dart';

void main() {
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,
),
),
],
));

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++;
},
disabled: false,
child: Container(),
),
);

final pressableFinder = find.byType(Pressable);
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(
Pressable(
onPressed: () {
counter++;
},
disabled: true,
child: Container(),
),
);

final pressableFinder = find.byType(Pressable);
expect(pressableFinder, findsOneWidget);

await tester.tap(pressableFinder);
await tester.pumpAndSettle();

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++;
},
disabled: 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,
disabled: true,
child: Container(),
),
);

final pressableFinder = find.byType(PressableBox);
expect(pressableFinder, findsOneWidget);

await tester.tap(pressableFinder);
await tester.pumpAndSettle();

expect(counter, 0);
});
});
}
Loading