Skip to content

Commit

Permalink
Merge pull request #235 from conceptadev/fix/pressable-delay
Browse files Browse the repository at this point in the history
Fix/pressable delay
  • Loading branch information
leoafarias authored Mar 25, 2024
2 parents 8c4bf00 + e01e3ec commit 9d57c86
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 65 deletions.
26 changes: 10 additions & 16 deletions lib/src/factory/mix_provider_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MixData with Comparable {
}

/// Alias for animation.isAnimated
bool get isAnimated => animation?.isAnimated ?? false;
bool get isAnimated => animation != null;

/// Getter for [MixTokenResolver].
///
Expand Down Expand Up @@ -127,17 +127,16 @@ List<StyleAttribute> applyContextToVisualAttributes(
final multiVariants = mix.variants.whereType<MultiVariantAttribute>();

// Once there are no more context variants to apply, return the mix
if (contextVariants.isEmpty) {
// TODO: Clean this up
return style.styles.values.toList();
if (contextVariants.isEmpty && multiVariants.isEmpty) {
return style.styles.values;
}

List<WhenVariant> contextVariantTypes = [];
List<WhenVariant> gestureVariantTypes = [];
List<WhenVariant> pressableVariantTypes = [];

for (ContextVariantAttribute attr in contextVariants) {
if (attr.variant is PressableStateVariant) {
gestureVariantTypes.add(attr);
pressableVariantTypes.add(attr);
} else {
contextVariantTypes.add(attr);
}
Expand All @@ -146,7 +145,7 @@ List<StyleAttribute> applyContextToVisualAttributes(
for (MultiVariantAttribute attr in multiVariants) {
if (attr.variant.variants
.any((variant) => variant is PressableStateVariant)) {
gestureVariantTypes.add(attr);
pressableVariantTypes.add(attr);
} else {
contextVariantTypes.add(attr);
}
Expand All @@ -156,9 +155,9 @@ List<StyleAttribute> applyContextToVisualAttributes(
style = _applyVariants(context, style, variant);
}

// Gesture Variants are applied after Context Variants
// Pressable Variants are applied after Context Variants
// As they take precedence over Context Variants from a styling perspective
for (WhenVariant variant in gestureVariantTypes) {
for (WhenVariant variant in pressableVariantTypes) {
style = _applyVariants(context, style, variant);
}

Expand All @@ -182,18 +181,13 @@ M? _mergeAttributes<M extends StyleAttribute>(Iterable<M> mergeables) {
}

class AnimatedData with Comparable {
final bool isAnimated;
final Duration duration;
final Curve curve;

const AnimatedData({
required this.isAnimated,
required this.duration,
required this.curve,
});
const AnimatedData({required this.duration, required this.curve});

@override
List<Object> get props => [isAnimated, duration, curve];
List<Object> get props => [duration, curve];
}

Iterable<Attribute> _applyStyleBuilder(
Expand Down
14 changes: 3 additions & 11 deletions lib/src/factory/style_mix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ class AnimatedStyle extends Style {
return AnimatedStyle._(
styles: style.styles,
variants: style.variants,
animatedData: AnimatedData(
isAnimated: true,
duration: duration,
curve: curve,
),
animatedData: AnimatedData(duration: duration, curve: curve),
);
}

Expand Down Expand Up @@ -248,18 +244,14 @@ class Style with Comparable {
SpreadFunctionParams(addAttributes);

/// Returns a `AnimatedStyle` from this `Style` with the provided [duration] and [curve].
AnimatedStyle toAnimated({
AnimatedStyle animate({
Duration duration = const Duration(milliseconds: 150),
Curve curve = Curves.linear,
}) {
return AnimatedStyle._(
styles: styles,
variants: variants,
animatedData: AnimatedData(
isAnimated: true,
duration: duration,
curve: curve,
),
animatedData: AnimatedData(duration: duration, curve: curve),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/widgets/pressable/pressable_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ class PressableState extends InheritedModel<PressableStateAspect> {
}

class PointerPosition with Comparable {
final Alignment alignment;
final Alignment position;
final Offset offset;
const PointerPosition({required this.alignment, required this.offset});
const PointerPosition({required this.position, required this.offset});

@override
get props => [alignment, offset];
get props => [position, offset];
}
19 changes: 10 additions & 9 deletions lib/src/widgets/pressable/pressable_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PressableBox extends StatelessWidget {
this.focusNode,
this.autofocus = false,
this.enableFeedback = false,
this.unpressDelay = const Duration(milliseconds: 200),
this.unpressDelay,
this.onFocusChange,
@Deprecated('Use onTap instead') VoidCallback? onPressed,
VoidCallback? onPress,
Expand Down Expand Up @@ -50,7 +50,7 @@ class PressableBox extends StatelessWidget {
final bool disabled;
final FocusNode? focusNode;
final bool autofocus;
final Duration unpressDelay;
final Duration? unpressDelay;
final Function(bool focus)? onFocusChange;
final Duration animationDuration;
final Curve animationCurve;
Expand Down Expand Up @@ -79,7 +79,7 @@ class Pressable extends _PressableBuilderWidget {
required super.child,
super.disabled,
super.enableFeedback,
@Deprecated('Use onTap instead') VoidCallback? onPressed,
@Deprecated('Use onPress instead') VoidCallback? onPressed,
VoidCallback? onPress,
@Deprecated('Use hitTestBehavior instead') HitTestBehavior? behavior,
HitTestBehavior? hitTestBehavior,
Expand All @@ -89,11 +89,12 @@ class Pressable extends _PressableBuilderWidget {
super.focusNode,
super.onKey,
super.onKeyEvent,
super.unpressDelay,
Duration? unpressDelay,
}) : super(
onPress: onPress ?? onPressed,
hitTestBehavior:
hitTestBehavior ?? behavior ?? HitTestBehavior.opaque,
unpressDelay: unpressDelay ?? const Duration(milliseconds: 150),
);

@override
Expand All @@ -116,7 +117,7 @@ abstract class _PressableBuilderWidget extends StatefulWidget {
this.onKey,
this.onKeyEvent,
this.hitTestBehavior = HitTestBehavior.opaque,
this.unpressDelay,
required this.unpressDelay,
});

final Widget child;
Expand Down Expand Up @@ -162,7 +163,7 @@ abstract class _PressableBuilderWidget extends StatefulWidget {
final HitTestBehavior hitTestBehavior;

/// The duration to wait after the press is released before the state of pressed is removed
final Duration? unpressDelay;
final Duration unpressDelay;
}

abstract class _PressableBuilderWidgetState<T extends _PressableBuilderWidget>
Expand All @@ -173,7 +174,7 @@ abstract class _PressableBuilderWidgetState<T extends _PressableBuilderWidget>
bool _isLongPressed = false;

PointerPosition? _pointerPosition = const PointerPosition(
alignment: Alignment.center,
position: Alignment.center,
offset: Offset.zero,
);
int _pressCount = 0;
Expand Down Expand Up @@ -203,7 +204,7 @@ abstract class _PressableBuilderWidgetState<T extends _PressableBuilderWidget>

setState(() {
_pointerPosition = PointerPosition(
alignment: cursorAlignment,
position: cursorAlignment,
offset: cursorOffset,
);
});
Expand Down Expand Up @@ -265,7 +266,7 @@ abstract class _PressableBuilderWidgetState<T extends _PressableBuilderWidget>
Future<void> unpressAfterDelay(int initialPressCount) async {
final delay = widget.unpressDelay;

if (delay != null && delay != Duration.zero) {
if (delay != Duration.zero) {
await Future.delayed(delay);
}

Expand Down
50 changes: 25 additions & 25 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.17.1"
dart_code_metrics_presets:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -75,6 +75,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
js:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
url: "https://pub.dev"
source: hosted
version: "0.6.7"
lints:
dependency: transitive
description:
Expand All @@ -87,26 +95,26 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.15"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.2.0"
meta:
dependency: "direct main"
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.9.1"
path:
dependency: transitive
description:
Expand All @@ -124,26 +132,26 @@ packages:
dependency: transitive
description:
name: source_span
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.9.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
url: "https://pub.dev"
source: hosted
version: "1.11.1"
version: "1.11.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
version: "2.1.1"
string_scanner:
dependency: transitive
description:
Expand All @@ -164,10 +172,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
url: "https://pub.dev"
source: hosted
version: "0.6.1"
version: "0.5.1"
vector_math:
dependency: transitive
description:
Expand All @@ -176,14 +184,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.3.0"
sdks:
dart: ">=3.2.0-194.0.dev <4.0.0"
dart: ">=3.0.0-0 <4.0.0"
flutter: ">=3.10.6"
2 changes: 1 addition & 1 deletion test/src/widgets/pressable/pressable_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void main() {
expect(pressableFinder, findsOneWidget);

await tester.tap(pressableFinder);
await tester.pumpAndSettle();
await tester.pumpAndSettle(const Duration(milliseconds: 200));

expect(counter, 1);
},
Expand Down

0 comments on commit 9d57c86

Please sign in to comment.