【问题标题】:Flutter setting a value of TextFormField according to setState of a widget variableFlutter根据一个widget变量的setState设置TextFormField的值
【发布时间】:2020-11-15 18:04:41
【问题描述】:

当这个变量在构建小部件后根据setstate 操作而不是这个textformfeild 的初始值更改时,我如何从小部件变量设置TextFormField 的值
我知道我可以通过使用TextEditingController 来更改它的值,但是我有太多TextFormField 并创建TextEditingController 通过使用传统方式将它们添加到整个小部件中需要大量代码: 1-创建一个 TextEditingController。
2-将 TextEditingController 连接到文本字段。
3-创建一个函数来更新最新值。
4-听控制器的变化。
有什么方法可以构建此控制器并仅在 TextFormField 的代码中执行这些步骤?还是其他方式?

【问题讨论】:

  • 我假设您想要一种非常简单的方法来在每次调用 setState 时将文本放入文本字​​段?
  • 是的,我需要这样的

标签: flutter widget


【解决方案1】:

将控制器放入

TextFormField(
      controller: TextEditingController(text: daytask),....

然后像这样放置setstate() txt='';

setState(() { txt='' }

【讨论】:

    【解决方案2】:

    最好的方法是简单地制作一个小部件。对于这个,这是我将使用的代码

    class StateTextField extends StatelessWidget {
      final String text;
      const StateTextField({Key key, this.text = ''}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return TextField(
          controller: TextEditingController(text: text),
        );
      }
    }
    

    应该做的工作。如果你想通过控制器,你也可以通过它。要使用它,只需在 build 方法中调用该类即可。

    【讨论】:

      【解决方案3】:

      TextFormFieldTextEditingControllersuffixIcon 可点击 selectDate showDatePicker(这是标准材质“lambda”对话框)的类似示例,其中 setState 正在更新初始设置日期(不是按initialValue,而不是 controller 参数)和一个新的日期。

      import 'package:flutter/material.dart';
      
      class CreateOrderForm extends StatefulWidget {
        const CreateOrderForm({Key? key}):super(key: key);
      
        @override
        CreateOrderFormState createState() => CreateOrderFormState();
      }
      
      class CreateOrderFormState extends State<CreateOrderForm>
      {
        final _formKey = GlobalKey<FormState>();
      
        late DateTime selectedDate;
        
        @override
        void initState() {
          super.initState();
          selectedDate = DateTime.now();
          print("initState selectedDate = ${selectedDate.toLocal()}");
        }
      
        @override
        Widget build(BuildContext context) {
      
          print("build prepare selectedDate: ${selectedDate.toLocal()}");
      
          return Form(
            key: _formKey,
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.all(8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [             
                    TextFormField(
                      //initialValue: "${selectedDate.toLocal()}",
                      controller: TextEditingController(text: "${selectedDate.toLocal()}"),
                      decoration: InputDecoration(
                          icon: Icon(Icons.event),
                          border: OutlineInputBorder(),
                          labelText: "Assignment Date & Time",
                          suffixIcon: IconButton(
                              onPressed: () => _selectDate(context),
                              icon: Icon(Icons.event))
                      ),
                    ),             
                    const Padding(padding: EdgeInsets.only(top: 8)),
                    Align(
                      alignment: Alignment.bottomRight,
                    child: ElevatedButton(
                        onPressed: ((){}),
                        child: Text("Submit")),
                    )
                  ],
                ),
              ),
            )
          );
        }
      
        _selectDate(BuildContext context) async{
          await showDatePicker(
              context: context,
              initialDate: selectedDate,
              firstDate: DateTime(1970),
              lastDate: DateTime(9999)
          ).then((value){
            if(value != null && value != selectedDate)
            {
              this.setState(() {
                selectedDate = value;
                print("New date ${selectedDate.toLocal()}");
              });
            }
          });
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-10
        • 2014-10-06
        • 2021-08-17
        • 2010-12-01
        • 1970-01-01
        • 2018-02-09
        相关资源
        最近更新 更多