3
0
Fork 0

Remove special keyboard handling for text inputs (#57)

This commit is contained in:
Nate Brown 2022-01-20 10:35:39 -06:00 committed by GitHub
parent 2831c84b57
commit 64b056618c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 126 deletions

View File

@ -3,9 +3,7 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'; import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
//TODO: please let us delete this file /// A normal TextField or CupertinoTextField that looks the same on all platforms
/// A normal TextField or CupertinoTextField that watches for copy, paste, cut, or select all keyboard actions
class SpecialTextField extends StatefulWidget { class SpecialTextField extends StatefulWidget {
const SpecialTextField( const SpecialTextField(
{Key key, {Key key,
@ -63,15 +61,8 @@ class SpecialTextField extends StatefulWidget {
} }
class _SpecialTextFieldState extends State<SpecialTextField> { class _SpecialTextFieldState extends State<SpecialTextField> {
FocusNode _focusNode = FocusNode();
List<TextInputFormatter> formatters; List<TextInputFormatter> formatters;
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override @override
void initState() { void initState() {
formatters = widget.inputFormatters; formatters = widget.inputFormatters;
@ -84,10 +75,7 @@ class _SpecialTextFieldState extends State<SpecialTextField> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return RawKeyboardListener( return PlatformTextField(
focusNode: _focusNode,
onKey: _onKey,
child: PlatformTextField(
autocorrect: widget.autocorrect, autocorrect: widget.autocorrect,
minLines: widget.minLines, minLines: widget.minLines,
maxLines: widget.maxLines, maxLines: widget.maxLines,
@ -124,81 +112,6 @@ class _SpecialTextFieldState extends State<SpecialTextField> {
placeholder: widget.placeholder, placeholder: widget.placeholder,
suffix: widget.suffix), suffix: widget.suffix),
style: widget.style, style: widget.style,
controller: widget.controller)); controller: widget.controller);
}
_onKey(RawKeyEvent event) {
// We don't care about key up events
if (event is RawKeyUpEvent) {
return;
}
if (event.logicalKey == LogicalKeyboardKey.tab) {
// Handle tab to the next node
if (widget.nextFocusNode != null) {
FocusScope.of(context).requestFocus(widget.nextFocusNode);
}
return;
}
// Handle special keyboard events with control key
if (event.data.isControlPressed) {
// Handle paste
if (event.logicalKey == LogicalKeyboardKey.keyV) {
Clipboard.getData("text/plain").then((data) {
// Adjust our clipboard entry to confirm with the leftover space if we have maxLength
var text = data.text;
if (widget.maxLength != null && widget.maxLength > 0) {
var leftover = widget.maxLength - widget.controller.text.length;
if (leftover < data.text.length) {
text = text.substring(0, leftover);
}
}
// If maxLength took us to 0 then bail
if (text.length == 0) {
return;
}
var end = widget.controller.selection.end;
var start = widget.controller.selection.start;
// Insert our paste buffer into the selection, which can be 0 selected text (normal caret)
widget.controller.text = widget.controller.selection.textBefore(widget.controller.text) +
text +
widget.controller.selection.textAfter(widget.controller.text);
// Adjust our caret to be at the end of the pasted contents, need to take into account the size of the selection
// We may want runes instead of
end += text.length - (end - start);
widget.controller.selection = TextSelection(baseOffset: end, extentOffset: end);
});
return;
}
// Handle select all
if (event.logicalKey == LogicalKeyboardKey.keyA) {
widget.controller.selection = TextSelection(baseOffset: 0, extentOffset: widget.controller.text.length);
return;
}
// Handle copy
if (event.logicalKey == LogicalKeyboardKey.keyC) {
Clipboard.setData(ClipboardData(text: widget.controller.selection.textInside(widget.controller.text)));
return;
}
// Handle cut
if (event.logicalKey == LogicalKeyboardKey.keyX) {
Clipboard.setData(ClipboardData(text: widget.controller.selection.textInside(widget.controller.text)));
var start = widget.controller.selection.start;
widget.controller.text = widget.controller.selection.textBefore(widget.controller.text) +
widget.controller.selection.textAfter(widget.controller.text);
widget.controller.selection = TextSelection(baseOffset: start, extentOffset: start);
return;
}
}
} }
} }