簡體   English   中英

在 Flutter 的表單中將數據從一個小部件傳遞到另一個小部件

[英]Pass data from one widget to another in a Form in Flutter

https://flutter.dev/docs/cookbook/forms/validation

在這里,他們在Form中使用了兩個小部件: TextFormWidgetElevatedButton 他們說TextFormWidget的驗證器接收用戶輸入的值。

我想在用戶按下ElevatedButton時打印該值。

如何讓ElevatedButtonTextFormWidget接收該值?

@override
  Widget build(BuildContext context) 
  {
    return Form(
      key: _formKey,
      child: Column(
        children: <Widget>[
                   TextFormField(
                        // The validator receives the text that the user has entered.
                        validator: (value) 
                        {
                           if (value == null || value.isEmpty) 
                           {
                               return 'Please enter some text';
                           }
                           return null;
                        },
                    ),


                   ElevatedButton(
                       onPressed: () {
                          if (_formKey.currentState!.validate()) {
                             ....
                          }
                       },
                       child: Text('Submit'),
                   ),
              ],
      ),
    );
  }

設置TextFormFieldcontroller屬性並從那里讀取數據。

按如下方式使用它:

var controller = TextEditingController();

@override
  Widget build(BuildContext context)
  {
    return Form(
      key: GlobalKey(),
      child: Column(
        children: <Widget>[
          TextFormField(
            controller: controller,
            // The validator receives the text that the user has entered.
            validator: (value)
            {
              if (value == null || value.isEmpty)
              {
                return 'Please enter some text';
              }
              return null;
            },
          ),


          ElevatedButton(
            onPressed: () {
              if (_formKey.currentState!.validate()) {
              String currentText = controller.text;
              // ...do something with currentText
            }
            },
            child: Text('Submit'),
          ),
        ],
      ),
    );
  }

您可以使用TextEditingController並將其傳遞給TextFormFieldcontroller參數。 您可以從controller獲取使用給出的value或輸入。

例子

  // Controller
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        children: <Widget>[
          TextFormField(
            // Add the controller to the TextFormField
            controller: _controller,
            // The validator receives the text that the user has entered.
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Please enter some text';
              }
              return null;
            },
          ),
          ElevatedButton(
            onPressed: () {
              // if (_formKey.currentState!.validate()) {
              //    ....
              // }

              // This gives the value
              final String value = _controller.text;
              print(value);
            },
            child: Text('Submit'),
          ),
        ],
      ),
    );
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM