She*_*ode 2 mobile-application flutter flutter-layout
我已经实现了自定义键盘(在表格->卡片->按钮小部件的帮助下),如下所示,如果用户按下按钮,那么我捕获文本并将其显示在屏幕上。
通过使用此功能,用户可以输入数据,但不能编辑数据。在这种情况下如何实现文本编辑功能?
我尝试使用 TextField 小部件来适应我的情况,它提供了包括文本编辑在内的所有功能。但它不允许使用自定义键盘。
您可以像这样使用readOnly和showCursor属性TextField:
TextField(
controller: _textController,
readOnly: true,
showCursor: true,
),
Run Code Online (Sandbox Code Playgroud)
然后您可以替换所选文本或插入新文本,如下所示:
@override
void initState() {
super.initState();
_textController = TextEditingController();
}
_insertText(String textToInsert) {
if (_textController.selection.start >= 0) {
int newPosition = _textController.selection.start + textToInsert.length;
_textController.text = _textController.text.replaceRange(
_textController.selection.start,
_textController.selection.end,
textToInsert,
);
_textController.selection = TextSelection(
baseOffset: newPosition,
extentOffset: newPosition,
);
} else {
_textController.text += textToInsert;
}
}
Widget _buildRow(List<String> texts) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: texts
.map((text) => RaisedButton(
child: Text(text), onPressed: () => _insertText(text)))
.toList());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Calc")),
body: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(16.0),
child: TextField(
controller: _textController,
readOnly: true,
showCursor: true,
),
),
_buildRow(["1", "2", "3"]),
_buildRow(["4", "5", "6"]),
_buildRow(["7", "8", "9"]),
],
),
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1937 次 |
| 最近记录: |