Last active
March 6, 2020 18:23
-
-
Save quetool/9203fc136d217b6144f68e202118d7a4 to your computer and use it in GitHub Desktop.
(Missing feature) Flutter onLongPressUp emulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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