繁体   English   中英

为什么 If 语句在 Flutter 中处理 TextFormField?

[英]Why did If statement dispose TextFormField in flutter?

在我的应用程序中,我需要预览一个表格,如果用户输入了一些数据,该表格就会出现。 所以我添加了一个 if 语句。 还有一个按钮可以清除表单。 我的 TextFormField 位于表格下方。 在我清除表格之前它不会被处理。 但是在我清除表格后,它会被处理掉。 我在表格下方添加了另一个 TextFormField 以检查表格是否导致错误。 那个 TextFormField 也被处理掉了。 为什么会这样?

// This is the code of the Table
if (_data.length != 0)
      Row(children: [
        Expanded(
            child: Padding(
                padding: EdgeInsets.only(top: 15, bottom: 10),
                child: DataTable(
                  columnSpacing: 20,
                  columns: const <DataColumn>[
                    DataColumn(label: Center(child: Text('Medications'))),
                    DataColumn(label: Center(child: Text('Amount'))),
                    DataColumn(label: Center(child: Text('Days'))),
                    DataColumn(label: Center(child: Text('When'))),
                    DataColumn(
                        label: Text(
                      '-',
                      style: TextStyle(fontSize: 22),
                      textAlign: TextAlign.center,
                    ))
                  ],
                  rows: _data
                      .map(
                        ((element) => DataRow(
                              cells: [
                                (DataCell(Text(element["drug"]))),
                                DataCell(Text(element["amount"].toString(),
                                    textAlign: TextAlign.center)),
                                DataCell(Text(element["days"].toString(),
                                    textAlign: TextAlign.center)),
                                DataCell(Text(element["when"],
                                    textAlign: TextAlign.center)),
                                DataCell(RButton(
                                  onPressed: onPressedDrugs,
                                  text: element["drug"],
                                ))
                              ],
                            )),
                      )
                      .toList(),
                )))
      ]),
    // This is the code of the TextFormField
    Padding(
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 10),
      child: Row(
        children: [
          Expanded(
            child: TextFieldSearch(
              controller: controllerInvestigation,
              initialList: allInvestigations,
              label: "Enter the Investigation",
              decoration: InputDecoration(
                focusColor: Colors.purple,
                border: OutlineInputBorder(),
                labelText: "Enter the Investigation",
                icon: FaIcon(FontAwesomeIcons.vial),
              ),
            ),
          ),
          PopupMenuButton<String>(
              elevation: 16,
              icon: Icon(Icons.arrow_drop_down),
              itemBuilder: (context) =>
                  List<String>.from(starInvestigations)
                      .map<PopupMenuItem<String>>((String value) {
                    return PopupMenuItem<String>(
                        value: value,
                        child: Container(
                            color: Colors.transparent,
                            width: double.infinity,
                            child: TextButton(
                              child: ListTile(
                                  title: Text(value,
                                      textAlign: TextAlign.left)),
                              onPressed: () {
                                setState(() {
                                  controllerInvestigation.text = value;
                                });
                                Navigator.pop(context);
                              },
                            )));
                  }).toList()),
        ],
      ),
    ),

       // This is the code of the Clear button
        Padding(
            padding: EdgeInsets.symmetric(
                horizontal:
                    (MediaQuery.of(context).size.width / 2 - 150) / 2,
                vertical: 10),
            child: SizedBox(
              width: 150,
              child: FloatingActionButton.extended(
                onPressed: () {
                  controllerName.text = '';
                  controllerAmount.text = '';
                  controllerDrug.text = '';
                  controllerAgeYears.text = '';
                  controllerDays.text = '';
                  controllerAgeMonths.text = '';
                  controllerInvestigation.text = '';
                  controllerSystolicPressure.text = '';
                  controllerDiastolicPressure.text = '';
                  controllerComplain.text = '';
                  _isSelected = [false, false];
                  setState(() {
                    _data = [];
                    selectedInvestigations = [];
                    getData();
                  });
                },
                focusColor: Colors.cyan[400],
                label: const Text('Clear  '),
                icon: const Icon(Icons.clear_all),
                backgroundColor: Colors.cyan[300],
              ),
            )),

问题是 TextEditingController 在我清除表单后被处理掉。 所以重新定义TextEditingController是解决这个问题的关键。 所以我重新定义了变量,如下所示。

controllerInvestigation = TextEditingController();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM