Flutter - 使用 GetX 预先填充 TextFormField 的值

发布于 2025-01-18 14:03:58 字数 2211 浏览 0 评论 0原文

的方法,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2025-01-25 14:03:58

我认为您需要使用分配给字段的textedingcontroller,并设置text属性,并从getxController中使用变量的值

I think you will need to use a TextEditingController assigned to your field and set the text property with the value of your variable from the GetxController

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文