当商店发生变化时,我正在尝试更新 TextEditingController 中的文本。流程是:用户扫描条形码,我必须在文本字段中显示扫描的条形码。还有一个选项可以手动输入条形码到该字段然后传递到商店,这就是我需要控制器的原因。
小部件
final TextEditingController _controller = TextEditingController(text: '');
@override
void initState() {
super.initState();
this.widget.findMyItemStore.startScanBarcodeWidget();
_controller.addListener(() {
this.widget.findMyItemStore.setBarcodeReceivedFromDevice(_controller.text);
});
}
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 0, vertical: 16),
child: Observer(
builder: (_) => TextField(
style: TextStyle(color: CustomColors.text_black),
controller: _controller,
onChanged: (String? value) => _onBarcodeTextChanged(value),
decoration: InputDecoration(border: InputBorder.none, hintText: '1234567890', hintStyle: TextStyle(color: CustomColors.text_grey), fillColor: CustomColors.background_white, filled: true),
),
),
)
店铺
String setBarcodeReceivedFromDevice(String barcodeToCheck) {
return barcodeToCheck;
}
文本永远不会更新。请帮忙!