Flutter - 使用 GetX 预先填充 TextFormField 的值
的方法,
class FieldOwnerController extends GetxController {
static FieldOwnerController instance = Get.find();
var fieldAddress = "".obs;
...
//method to populate text field
assignAddress() {
dynamic argumentData = Get.arguments;
fieldAddress.value = argumentData["address"];
}
这是控制器,它包含填充文本表单字段UI
@override
Widget build(BuildContext context) {
WidgetsBinding.instance!
.addPostFrameCallback((_) => fieldOwnerController.assignAddress());
...
Obx(
() => MultiLineTextField(
textEditingController: fieldOwnerController.addressCtrlr,
hintText: "",
icon: null,
initialValue: fieldOwnerController.fieldAddress.value,
),
),
class MultiLineTextField extends StatelessWidget {
const MultiLineTextField({
Key? key,
required this.textEditingController,
this.hintText,
this.icon,
this.initialValue,
}) : super(key: key);
final TextEditingController textEditingController;
final String? hintText;
final Icon? icon;
final String? initialValue;
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: SizeConfig.screenWidth / 1.2,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(kBorderRadiusMin),
color: kTextFieldFillColor,
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
child: TextFormField(
/// the new value is assigned correctly here
initialValue: initialValue,
minLines: 2,
maxLines: 5,
//controller: textEditingController,
decoration: InputDecoration(
icon: icon,
border: InputBorder.none,
hintText: hintText,
),
),
),
),
],
);
}
}
我得到了正确的值,但它没有显示在文本字段中。我的意思是,首先,呈现 UI(文本字段为空)。然后 fieldAddress 值发生更改,UI 被重建,但文本字段不显示该值。
这种方法有什么问题吗?
This is the controller and it contains the method to populate the textformfield
class FieldOwnerController extends GetxController {
static FieldOwnerController instance = Get.find();
var fieldAddress = "".obs;
...
//method to populate text field
assignAddress() {
dynamic argumentData = Get.arguments;
fieldAddress.value = argumentData["address"];
}
UI
@override
Widget build(BuildContext context) {
WidgetsBinding.instance!
.addPostFrameCallback((_) => fieldOwnerController.assignAddress());
...
Obx(
() => MultiLineTextField(
textEditingController: fieldOwnerController.addressCtrlr,
hintText: "",
icon: null,
initialValue: fieldOwnerController.fieldAddress.value,
),
),
class MultiLineTextField extends StatelessWidget {
const MultiLineTextField({
Key? key,
required this.textEditingController,
this.hintText,
this.icon,
this.initialValue,
}) : super(key: key);
final TextEditingController textEditingController;
final String? hintText;
final Icon? icon;
final String? initialValue;
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: SizeConfig.screenWidth / 1.2,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(kBorderRadiusMin),
color: kTextFieldFillColor,
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
child: TextFormField(
/// the new value is assigned correctly here
initialValue: initialValue,
minLines: 2,
maxLines: 5,
//controller: textEditingController,
decoration: InputDecoration(
icon: icon,
border: InputBorder.none,
hintText: hintText,
),
),
),
),
],
);
}
}
I get the value alright, but it doesn't show up in the textfield. I mean first, the UI is rendered(textfield is empty). Then the fieldAddress value changes, the UI gets rebuilt, but the textfield doesn't show the value.
What is wrong with this approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要使用分配给字段的
textedingcontroller
,并设置text
属性,并从getxController中使用变量的值I think you will need to use a
TextEditingController
assigned to your field and set thetext
property with the value of your variable from the GetxController