onKey method
Implementation
@override
bool onKey(KeyEvent event, RenderContext ctx) {
var cur = state?.cursor ?? value.length;
var v = state?.value ?? value;
if (cur > v.length) cur = v.length;
String? next;
if (event.key != null) {
switch (event.key!) {
case NamedKey.enter:
onSubmit?.call();
return true;
case NamedKey.backspace:
if (cur > 0) {
next = v.substring(0, cur - 1) + v.substring(cur);
cur -= 1;
}
case NamedKey.delete:
if (cur < v.length) {
next = v.substring(0, cur) + v.substring(cur + 1);
}
case NamedKey.arrowLeft:
if (cur > 0) cur -= 1;
if (state != null) state!.cursor = cur;
return true;
case NamedKey.arrowRight:
if (cur < v.length) cur += 1;
if (state != null) state!.cursor = cur;
return true;
case NamedKey.home:
cur = 0;
if (state != null) state!.cursor = cur;
return true;
case NamedKey.end:
cur = v.length;
if (state != null) state!.cursor = cur;
return true;
default:
return false;
}
} else {
if (event.ctrl || event.alt) return false;
final c = event.char;
if (c != null && c.runes.length == 1 && c.runes.first >= 0x20) {
next = v.substring(0, cur) + c + v.substring(cur);
cur += c.length;
}
}
if (next != null) {
if (state != null) {
state!.value = next;
state!.cursor = cur;
}
onChanged?.call(next);
return true;
}
return false;
}