我有簡單的文本欄位。在單擊特殊按鈕之前,用戶不允許在此文本欄位中寫入。這是我的代碼,此功能不起作用,但其中的所有其他內容都可以正常作業,我做錯了什么?
TextEditingController _textFieldController = TextEditingController();
bool isTextFieldEnable = false;
FocusNode _focusNode = FocusNode();
Row(
children: [
Container(
width: 305,
margin: EdgeInsets.only(left: 34),
height: 250,
child: SingleChildScrollView(
child: TextField(
controller: _textFieldController,
decoration: new InputDecoration.collapsed(hintText: "Описание"),
maxLines: null,
focusNode: _focusNode,
enabled: isTextFieldEnable,
onChanged: (value) {
CheckListModel.checkLists[arg['index']].description = value;
},
onSubmitted: (value) async {
FocusScope.of(context).unfocus();
_textFieldController.text = CheckListModel.checkLists[arg['index']].description;
CheckListModel checkList = CheckListModel.checkLists[arg['index']];
await checkList.writeToFile(checkList.toJson());
},
textInputAction: TextInputAction.done,
)),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
IconButton(
onPressed: () {
setState(() {
isTextFieldEnable = true;
print(isTextFieldEnable);
});
},
icon: Icon(Icons.edit),
),
],
)
],
),
uj5u.com熱心網友回復:
請參考以下代碼
我們可以通過將其 readOnly 屬性設定為 true 或 false 來控制是否禁用 TextField,
TextEditingController _textFieldController = TextEditingController();
bool isTextFieldEnable = true;
FocusNode _focusNode = FocusNode();
Row(
children: [
Container(
width: 305,
margin: EdgeInsets.only(left: 34),
height: 250,
child: SingleChildScrollView(
child: TextField(
controller: _textFieldController,
decoration: new InputDecoration.collapsed(hintText: "Описание"),
maxLines: null,
focusNode: _focusNode,
readOnly: isTextFieldEnable, /* change this if read only is set to false you can enter in text field or if it is set to true you cannot enter any text in TextField. */
onChanged: (value) {
CheckListModel.checkLists[arg['index']].description = value;
},
onSubmitted: (value) async {
FocusScope.of(context).unfocus();
_textFieldController.text = CheckListModel.checkLists[arg['index']].description;
CheckListModel checkList = CheckListModel.checkLists[arg['index']];
await checkList.writeToFile(checkList.toJson());
},
textInputAction: TextInputAction.done,
)),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
IconButton(
onPressed: () {
setState(() {
isTextFieldEnable = false;
print(isTextFieldEnable);
});
},
icon: Icon(Icons.edit),
),
],
)
],
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377666.html
上一篇:引數型別“字串?”不能分配給引數型別“字串和方法不能無條件呼叫,因為接收者可以為空
下一篇:顫動大小容器到其中的內容
