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,121 +75,43 @@ class _SpecialTextFieldState extends State<SpecialTextField> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return RawKeyboardListener( return PlatformTextField(
focusNode: _focusNode, autocorrect: widget.autocorrect,
onKey: _onKey, minLines: widget.minLines,
child: PlatformTextField( maxLines: widget.maxLines,
autocorrect: widget.autocorrect, maxLength: widget.maxLength,
minLines: widget.minLines, maxLengthEnforcement: widget.maxLengthEnforcement,
maxLines: widget.maxLines, keyboardType: widget.keyboardType,
maxLength: widget.maxLength, keyboardAppearance: widget.keyboardAppearance,
maxLengthEnforcement: widget.maxLengthEnforcement, textInputAction: widget.textInputAction,
keyboardType: widget.keyboardType, textCapitalization: widget.textCapitalization,
keyboardAppearance: widget.keyboardAppearance, textAlign: widget.textAlign,
textInputAction: widget.textInputAction, textAlignVertical: widget.textAlignVertical,
textCapitalization: widget.textCapitalization, autofocus: widget.autofocus,
textAlign: widget.textAlign, focusNode: widget.focusNode,
textAlignVertical: widget.textAlignVertical, onChanged: widget.onChanged,
autofocus: widget.autofocus, enabled: widget.enabled,
focusNode: widget.focusNode, onSubmitted: (_) {
onChanged: widget.onChanged, if (widget.nextFocusNode != null) {
enabled: widget.enabled, FocusScope.of(context).requestFocus(widget.nextFocusNode);
onSubmitted: (_) { }
if (widget.nextFocusNode != null) { },
FocusScope.of(context).requestFocus(widget.nextFocusNode); expands: widget.expands,
} inputFormatters: formatters,
}, material: (_, __) => MaterialTextFieldData(
expands: widget.expands, decoration: InputDecoration(
inputFormatters: formatters, border: InputBorder.none,
material: (_, __) => MaterialTextFieldData( contentPadding: EdgeInsets.zero,
decoration: InputDecoration( isDense: true,
border: InputBorder.none, hintText: widget.placeholder,
contentPadding: EdgeInsets.zero, counterText: '',
isDense: true, suffix: widget.suffix)),
hintText: widget.placeholder, cupertino: (_, __) => CupertinoTextFieldData(
counterText: '', decoration: BoxDecoration(),
suffix: widget.suffix)), padding: EdgeInsets.zero,
cupertino: (_, __) => CupertinoTextFieldData( placeholder: widget.placeholder,
decoration: BoxDecoration(), suffix: widget.suffix),
padding: EdgeInsets.zero, style: widget.style,
placeholder: widget.placeholder, controller: widget.controller);
suffix: widget.suffix),
style: widget.style,
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;
}
}
} }
} }