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

feat: Add support for onTapDown and onTapUp #534

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 18 additions & 11 deletions packages/mix/lib/mix.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/// /\\\\ /\\\\ /\\\\\\\\\\\ /\\\ /\\\
/// \/\\\\\\ /\\\\\\ \/////\\\/// \///\\\ /\\\/
/// \/\\\//\\\ /\\\//\\\ \/\\\ \///\\\\\\/
/// \/\\\\///\\\/\\\/ \/\\\ \/\\\ \//\\\\
/// \/\\\ \///\\\/ \/\\\ \/\\\ \/\\\\
/// \/\\\ \/// \/\\\ \/\\\ /\\\\\\
/// \/\\\ \/\\\ \/\\\ /\\\////\\\
/// \/\\\ \/\\\ /\\\\\\\\\\\ /\\\/ \///\\\
/// \/// \/// \/////////// \/// \///
/// /\\\\ /\\\\ /\\\\\\\\\\\ /\\\ /\\\
/// \/\\\\\\ /\\\\\\ \/////\\\/// \///\\\ /\\\/
/// \/\\\//\\\ /\\\//\\\ \/\\\ \///\\\\\\/
/// \/\\\\///\\\/\\\/ \/\\\ \/\\\ \//\\\\
/// \/\\\ \///\\\/ \/\\\ \/\\\ \/\\\\
/// \/\\\ \/// \/\\\ \/\\\ /\\\\\\
/// \/\\\ \/\\\ \/\\\ /\\\////\\\
/// \/\\\ \/\\\ /\\\\\\\\\\\ /\\\/ \///\\\
/// \/// \/// \/////////// \/// \///
///
/// https://fluttermix.com
///
/// https://fluttermix.com
///
/// /\///////////////////////////////////////////////////\
/// \/\ ***** GENERATED CODE ***** \ \
/// \/\ ** DO NOT EDIT THIS FILE ** \ \
Expand Down Expand Up @@ -54,6 +54,7 @@ export 'src/attributes/strut_style/strut_style_dto.dart';
export 'src/attributes/text_height_behavior/text_height_behavior_dto.dart';
export 'src/attributes/text_style/text_style_dto.dart';
export 'src/attributes/text_style/text_style_util.dart';

/// CORE
export 'src/core/attribute.dart';
export 'src/core/attributes_map.dart';
Expand All @@ -70,7 +71,9 @@ export 'src/core/spec.dart';
export 'src/core/styled_widget.dart';
export 'src/core/utility.dart';
export 'src/core/variant.dart';
export 'src/core/widget_state/on_tap_event_inherited.dart';
export 'src/core/widget_state/widget_state_controller.dart';

/// MODIFIERS
export 'src/modifiers/align_widget_modifier.dart';
export 'src/modifiers/aspect_ratio_widget_modifier.dart';
Expand All @@ -87,6 +90,7 @@ export 'src/modifiers/sized_box_widget_modifier.dart';
export 'src/modifiers/transform_widget_modifier.dart';
export 'src/modifiers/visibility_widget_modifier.dart';
export 'src/modifiers/widget_modifiers_util.dart';

/// SPECS
export 'src/specs/box/box_spec.dart';
export 'src/specs/box/box_widget.dart';
Expand All @@ -104,6 +108,7 @@ export 'src/specs/stack/stack_widget.dart';
export 'src/specs/text/text_directives_util.dart';
export 'src/specs/text/text_spec.dart';
export 'src/specs/text/text_widget.dart';

/// THEME
export 'src/theme/material/material_theme.dart';
export 'src/theme/material/material_tokens.dart';
Expand All @@ -116,6 +121,7 @@ export 'src/theme/tokens/space_token.dart';
export 'src/theme/tokens/text_style_token.dart';
export 'src/theme/tokens/token_resolver.dart';
export 'src/theme/tokens/token_util.dart';

/// VARIANTS
export 'src/variants/context_variant.dart';
export 'src/variants/context_variant_util/on_breakpoint_util.dart';
Expand All @@ -127,5 +133,6 @@ export 'src/variants/context_variant_util/on_platform_util.dart';
export 'src/variants/context_variant_util/on_util.dart';
export 'src/variants/variant_attribute.dart';
export 'src/variants/widget_state_variant.dart';

/// WIDGETS
export 'src/widgets/pressable_widget.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:flutter/material.dart';

import '../on_tap_event_inherited.dart';
import '../widget_state_controller.dart';

class GestureMixStateWidget extends StatefulWidget {
Expand Down Expand Up @@ -86,6 +87,7 @@ class GestureMixStateWidget extends StatefulWidget {
class _GestureMixStateWidgetState extends State<GestureMixStateWidget> {
int _pressCount = 0;
Timer? _timer;
OnTapEvent? _onTapEvent;
late final MixWidgetStateController _controller;

@override
Expand All @@ -94,6 +96,12 @@ class _GestureMixStateWidgetState extends State<GestureMixStateWidget> {
_controller = widget.controller ?? MixWidgetStateController();
}

void _restoreTapEvent() {
setState(() {
_onTapEvent = null;
});
}

void _onPanUpdate(DragUpdateDetails event) {
widget.onPanUpdate?.call(event);
}
Expand All @@ -117,27 +125,44 @@ class _GestureMixStateWidgetState extends State<GestureMixStateWidget> {
}

void _onTapUp(TapUpDetails details) {
setState(() {
_onTapEvent = OnTapEvent.up;
});
_controller.longPressed = false;
widget.onTapUp?.call(details);
}

void _onTapDown(TapDownDetails details) {
setState(() {
_onTapEvent = OnTapEvent.down;
});
}

void _onTapCancel() {
_controller.longPressed = false;
_controller.pressed = false;
_restoreTapEvent();
widget.onTapCancel?.call();
}

void _onLongPressStart(LongPressStartDetails details) {
_controller.longPressed = true;
_onTapEvent = OnTapEvent.down;
_controller.pressed = true;
widget.onLongPressStart?.call(details);
}

void _onLongPressEnd(LongPressEndDetails details) {
_controller.longPressed = false;
_controller.pressed = false;
_restoreTapEvent();
widget.onLongPressEnd?.call(details);
}

void _onLongPressCancel() {
_controller.longPressed = false;
_controller.pressed = false;
_restoreTapEvent();
widget.onLongPressCancel?.call();
}

Expand All @@ -153,6 +178,7 @@ class _GestureMixStateWidgetState extends State<GestureMixStateWidget> {
void unpressCallback() {
if (_controller.pressed && _pressCount == initialPressCount) {
_controller.pressed = false;
_restoreTapEvent();
}
}

Expand Down Expand Up @@ -188,22 +214,27 @@ class _GestureMixStateWidgetState extends State<GestureMixStateWidget> {

@override
Widget build(BuildContext context) {
return GestureDetector(
onTapUp: widget.onTap != null ? _onTapUp : null,
onTap: widget.onTap != null ? _onTap : null,
onTapCancel: widget.onTap != null ? _onTapCancel : null,
onLongPressCancel: widget.onLongPress != null ? _onLongPressCancel : null,
onLongPress: widget.onLongPress != null ? _onLongPress : null,
onLongPressStart: widget.onLongPress != null ? _onLongPressStart : null,
onLongPressEnd: widget.onLongPress != null ? _onLongPressEnd : null,
onPanDown: widget.onPanDown != null ? _onPanDown : null,
onPanStart: widget.onPanStart != null ? _onPanStart : null,
onPanUpdate: widget.onPanUpdate != null ? _onPanUpdate : null,
onPanEnd: widget.onPanEnd != null ? _onPanEnd : null,
onPanCancel: widget.onPanCancel != null ? _onPanCancel : null,
behavior: widget.hitTestBehavior,
excludeFromSemantics: widget.excludeFromSemantics,
child: widget.child,
return OnTapEventProvider(
_onTapEvent,
child: GestureDetector(
onTapDown: widget.onTap != null ? _onTapDown : null,
onTapUp: widget.onTap != null ? _onTapUp : null,
onTap: widget.onTap != null ? _onTap : null,
onTapCancel: widget.onTap != null ? _onTapCancel : null,
onLongPressCancel:
widget.onLongPress != null ? _onLongPressCancel : null,
onLongPress: widget.onLongPress != null ? _onLongPress : null,
onLongPressStart: widget.onLongPress != null ? _onLongPressStart : null,
onLongPressEnd: widget.onLongPress != null ? _onLongPressEnd : null,
onPanDown: widget.onPanDown != null ? _onPanDown : null,
onPanStart: widget.onPanStart != null ? _onPanStart : null,
onPanUpdate: widget.onPanUpdate != null ? _onPanUpdate : null,
onPanEnd: widget.onPanEnd != null ? _onPanEnd : null,
onPanCancel: widget.onPanCancel != null ? _onPanCancel : null,
behavior: widget.hitTestBehavior,
excludeFromSemantics: widget.excludeFromSemantics,
child: widget.child,
),
);
}
}
21 changes: 21 additions & 0 deletions packages/mix/lib/src/core/widget_state/on_tap_event_inherited.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:flutter/widgets.dart';

enum OnTapEvent {
up,
down;
}

class OnTapEventProvider extends InheritedWidget {
const OnTapEventProvider(this.event, {super.key, required super.child});

static OnTapEventProvider? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType();
}

final OnTapEvent? event;

@override
bool updateShouldNotify(OnTapEventProvider oldWidget) {
return event != oldWidget.event;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'on_platform_util.dart';

import '../../theme/tokens/breakpoints_token.dart';
import '../widget_event.dart';
import '../widget_state_variant.dart';
import 'on_breakpoint_util.dart';
import 'on_brightness_util.dart';
import 'on_directionality_util.dart';
import 'on_not_util.dart';
import 'on_orientation_util.dart';
import 'on_platform_util.dart';

class OnContextVariantUtility {
// Platform variants
Expand Down Expand Up @@ -52,6 +53,9 @@ class OnContextVariantUtility {
final dragged = const OnDraggedVariant();
final error = const OnErrorVariant();

// Event variants
final tap = const OnTapEventVariant();

/// Creates an [OnNotVariant] with the specified [variant].
///
/// This reverses the result of the specified [variant].
Expand Down
42 changes: 42 additions & 0 deletions packages/mix/lib/src/variants/widget_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/widgets.dart';

import '../core/factory/style_mix.dart';
import '../core/variant.dart';
import '../core/widget_state/on_tap_event_inherited.dart';
import '../core/widget_state/widget_state_controller.dart';
import 'context_variant.dart';
import 'variant_attribute.dart';
import 'widget_state_variant.dart';

class OnTapEventVariant extends IVariant {
const OnTapEventVariant();

VariantAttribute call(Style Function(OnTapEvent) fn) {
return ContextVariantBuilder(
(BuildContext context) {
final event = OnTapEventProvider.of(context)?.event;
if (event == null) {
return const Style.empty();
}

return fn(event);
},
const OnPressVariant(),
);
}

@override
bool when(BuildContext context) {
return MixWidgetState.hasStateOf(context, MixWidgetState.pressed) &&
OnTapEventProvider.of(context) != null;
}

@override
VariantPriority get priority => VariantPriority.normal;

@override
Object get mergeKey => '$runtimeType';

@override
get props => [priority];
}
Loading
Loading