Skip to content

Instantly share code, notes, and snippets.

@quetool
Last active March 6, 2020 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quetool/9203fc136d217b6144f68e202118d7a4 to your computer and use it in GitHub Desktop.
Save quetool/9203fc136d217b6144f68e202118d7a4 to your computer and use it in GitHub Desktop.
(Missing feature) Flutter onLongPressUp emulation
// Currently Flutter (0.8.2) doesn't provide an onLongPressUp method in GestureDetector as you can see here https://github.com/flutter/flutter/pull/18949
// So I came up with this solution
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class LongPressUp extends SingleChildRenderObjectWidget {
final VoidCallback _callback;
LongPressUp(this._callback);
@override
RenderLongPressUp createRenderObject(BuildContext context) =>
RenderLongPressUp(this._callback);
}
class RenderLongPressUp extends RenderConstrainedBox {
final VoidCallback _callback;
RenderLongPressUp(this._callback)
: super(additionalConstraints: const BoxConstraints.expand());
// Makes this render box hittable so that we'll get pointer events.
@override
bool hitTestSelf(Offset position) => true;
@override
void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
if (event is PointerUpEvent || event is PointerCancelEvent) {
_callback();
}
}
}
// You can call this by adding LongPressUp widget as a GestureDetector child like this
GestureDetector(
onTapDown: (details) {
print("tapDown");
},
onTapUp: (details) {
print("tapUp");
},
onTap: () {
print("tap");
},
onTapCancel: () {
print("tapCancel");
},
onLongPress: () {
print("longPress");
},
child: LongPressUp(() { //
print("onLongPressUp"); // NEW FEATURE
}), //
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment