I want to call my custom function onDoubleClick
on a double tap in a TextField
still preserving the default behaviour of the double tapped word to get selected.
TextField
does not provide an onDoubleTap
attribute, so I wrapped it into a GestureDetector
like so:
GestureDetector(
onDoubleTap: () {
if (widget.onDoubleClick != null) {
widget.onDoubleClick!(_textEditingController.value);
}
},
child: TextField(
controller: _textEditingController,
),
)
This makes my function onDoubleClick
successfully execute onDoubleTap
but the default behavior of the double tapped word to get selected is unfortunately eliminated. Trying using all kinds of HitTestBehavior
for the GestureDetector
attribute behavior
does not change a thing.
I probably could go the way not to wrap with GestureDetector
and to use the second call of the onTap
handler, but then I would need to use an own value for the time interval between the first and the second tap. I do not want to do that because this value is an OS-level user setting that I do not want to deviate from.
So, how do I get my custom function onDoubleClick
called on a double tap in a TextField
still preserving the default behavior of the double tapped word to get selected?
Try to set flag to identify like below:
bool _isEditing = false;
on iniState set
_textEditingController.addListener(() {
setState(() {
_isEditing = true;
});
});
And your gestureDetector like below…
GestureDetector(
onDoubleTap: () {
if (_isEditing) {
print('Custom double tap');
} else {
final TextSelection textSelection = _textEditingController.selection;
final String text = _textEditingController.text;
int start = textSelection.baseOffset;
int end = textSelection.extentOffset;
while (start > 0 && !text[start - 1].contains(RegExp(r's'))) {
start--;
}
while (end < text.length - 1 && !text[end].contains(RegExp(r's'))) {
end++;
}
_textEditingController.selection = TextSelection(
baseOffset: start,
extentOffset: end,
);
}
},
child: TextField(
controller: _textEditingController,
decoration: InputDecoration(
hintText: 'Type something...',
),
),
);