在 Flutter 中从 TextFormField 小部件检索保存的值时出现问题?

时间:2020-12-18 19:20:09

标签: flutter textformfield

需要帮助。
我在同一页面上有多个页面和多次,我需要使用 TextFormField 以特定方式寻找填充和分隔符等等。所以,我决定创建一个可以重复使用的小部件,以避免复制粘贴。
所以,
当使用“onSaved”函数通过 TextFormField 传入时,我试图将“leadBloc.currentEditLead['first_name']”的值重新分配给“value”。

当我直接在使用它的页面中使用 TextFormField 小部件并传入变量时,会重新分配“leadBloc.currentEditLead['first_name']”变量。

但是如果我尝试使用小部件,它无法更改。我不知道为什么。

这是我尝试过的小部件,

  • 直接放置 TextFormField 小部件和
  • 使用自定义小部件“CreateTextFormFieldwidget”

然后将此小部件放置在该页面的“小部件构建(BuildContext 上下文)”中。

"""

Widget _buildForm() {
    return Container(
      child: Form(
        key: _createLeadFormKey,
        child: Column(
          children: [
            CreateTextFormField('First Name', TextInputType.text,
                leadBloc.currentEditLead['first_name'], firstNameController),]
)))}

"""

这是自定义小部件。
我什至尝试使用“TextEditingController”,在阅读了一些在线修复后注释掉“onSaved”。都没有工作。它似乎只在我直接在“_buildForm”小部件中使用原始 TextFormField 小部件时才有效。 所以,我也尝试在我需要使用的页面内声明小部件,但没有用。

"""

class CreateTextFormField extends StatefulWidget {
  BuildContext context;
  String data;
  String title;
  TextInputType textInputType;
  TextEditingController textController;

  CreateTextFormField(
      this.title, this.textInputType, this.data, this.textController);

  @override
  _CreateTextFormFieldState createState() => _CreateTextFormFieldState();
}

class _CreateTextFormFieldState extends State<CreateTextFormField> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Container(
              alignment: Alignment.centerLeft,
              margin: EdgeInsets.only(bottom: 5.0),
              child: RichText(
                text: TextSpan(
                  text: widget.title,
                  style: GoogleFonts.robotoSlab(
                      textStyle: TextStyle(
                          color: Theme.of(context).secondaryHeaderColor,
                          fontWeight: FontWeight.w500,
                          fontSize: screenWidth / 25)),
                  children: <TextSpan>[
                    TextSpan(
                        text: '* ',
                        style: GoogleFonts.robotoSlab(
                            textStyle: TextStyle(color: Colors.red))),
                    TextSpan(text: ': ', style: GoogleFonts.robotoSlab())
                  ],
                ),
              )),
          Container(
            margin: EdgeInsets.only(bottom: 10.0),
            child: TextFormField(
              controller: widget.textController,
              initialValue: widget.data,
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(12.0),
                  enabledBorder: boxBorder(),
                  focusedErrorBorder: boxBorder(),
                  focusedBorder: boxBorder(),
                  errorBorder: boxBorder(),
                  fillColor: Colors.white,
                  filled: true,
                  hintText: 'Enter ${widget.title}',
                  errorStyle: GoogleFonts.robotoSlab(),
                  hintStyle: GoogleFonts.robotoSlab(
                      textStyle: TextStyle(fontSize: 14.0))),
              keyboardType: widget.textInputType,
              validator: (value) {
                if (value.isEmpty) {
                  return 'This field is required.';
                }
                return null;
              },
              // onSaved: (value) {
              //   setState(() {
              //     // widget.data = value;
              //   });
              // },
            ),
          ),
          Divider(color: Colors.grey)
        ],
      ),
    );
  }
}

"""

0 个答案:

没有答案