在计算器中使用颤振构建中选择什么输出字段



我正在制作一个计算器应用程序。我想问我可以使用哪个小部件来显示数字。如果我使用Text那么我就不能用光标编辑它,如果我使用TextField键盘会弹出。

我找到了一个可能适合你的EditableText的实现。只需输入此代码:

class NoKeyboardEditableText extends EditableText {
NoKeyboardEditableText({
@required TextEditingController controller,
@required TextStyle style,
@required Color cursorColor,
bool autofocus = false,
Color selectionColor
}):super(
controller: controller,
focusNode: NoKeyboardEditableTextFocusNode(),
style: style,
cursorColor: cursorColor,
autofocus: autofocus,
selectionColor: selectionColor,
backgroundCursorColor: Colors.black
);
@override
EditableTextState createState() {
return NoKeyboardEditableTextState();
}
}
class NoKeyboardEditableTextState extends EditableTextState {
@override
void requestKeyboard() {
super.requestKeyboard();
//hide keyboard
SystemChannels.textInput.invokeMethod('TextInput.hide');
}
}
class NoKeyboardEditableTextFocusNode extends FocusNode {
@override
bool consumeKeyboardToken() {
// prevents keyboard from showing on first focus
return false;
}
}

这将创建一个不打开键盘的可编辑文本,但您仍然可以正常移动光标。为了添加文本,我会使用 text((、value((、selection(( 方法的组合来制作 TextEditController。首先获取字段中的值,然后使用 selection(( 获取光标的位置,在选择索引处输入所需的任何文本,最后将控制器的文本设置为结果字符串。

让我知道这是否有帮助!

所以,你可以试试这个

TextField(showCursor: true, readOnly: true);

最新更新