【问题标题】:How to pass TextEditingController's reference variable Form one class to another如何将 TextEditingController 引用变量从一个类传递到另一个类
【发布时间】:2021-08-06 09:31:09
【问题描述】:

我有一页叫registerUser.dart 示例代码如下:

import 'package:http/http.dart' as http;
import 'package:login/URL/urls.dart';

class UserRegister extends StatefulWidget {
  const UserRegister({Key key}) : super(key: key);

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

class _UserRegisterState extends State<UserRegister> {
  GlobalKey<FormState> key = GlobalKey<FormState>();

  TextEditingController firstName = TextEditingController();
  TextEditingController LastName = TextEditingController();
  TextEditingController email = TextEditingController();

  // var listOFUrl =listOFallUrl(firstName,this.LastName,this.email);

final surj =new listOFallUrl.myUrlConstructor(firstName.text);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Lab Work"),
        ),
        body: SingleChildScrollView(
          child: Form(
            key: key,
            child: Column(
              children: <Widget>[
                TextFormField(
                  controller: firstName,
                  decoration: InputDecoration(labelText: 'Item Name'),
                  validator: (value) =>
                  value.isEmpty ? 'this field cannot be empty' : null,
                ),
                TextFormField(
                  decoration: InputDecoration(labelText: 'Item Price'),
                  validator: (value) =>
                  value.isEmpty ? 'this field cannot be empty' : null,
                  controller: LastName,
                ),
                TextFormField(
                  controller: email,
                  decoration: InputDecoration(labelText: 'Item Description'),
                  validator: (value) =>
                  value.isEmpty ? 'this field cannot be empty' : null,
                ),
                ElevatedButton(
                  onPressed: () {
                    if (key.currentState.validate()) {
                      // putProduct();
                      print("record inserted");
                    }
                  },
                  child: Text('Save'),
                )
              ],
            ),
          ),
        ));
  }
}

并希望在firstNamelastNameemail TextEditingController 上方使用名为urls.dart 的页面。怎么可能呢?我尝试使用listOFallUrl 类的参考变量传递textEditorController,但没有成功。

import 'package:http/http.dart' as http;
class listOFallUrl{

  listOFallUrl(TextEditingController firtname){}

  listOFallUrl.myUrlConstructor(){
      print("this is example");
  }
  void URL(TextEditingController firtname){
        
  }
} ``` 
 

【问题讨论】:

    标签: flutter oop dart


    【解决方案1】:

    需要在你的代码中声明 List,试试下面的代码:

    class UserRegister extends StatefulWidget {
      //const UserRegister({Key key}) : super(key: key);
    
      @override
      _UserRegisterState createState() => _UserRegisterState();
    }
    
    class _UserRegisterState extends State<UserRegister> {
      GlobalKey<FormState> key = GlobalKey<FormState>();
    
      TextEditingController firstName = TextEditingController();
      TextEditingController LastName = TextEditingController();
      TextEditingController email = TextEditingController();
    
      putProduct(List<TextEditingController> surj){
        listOFallUrl(surj);
      }
    
      @override
      Widget build(BuildContext context) {
        List<TextEditingController> surj;
        return Scaffold(
            appBar: AppBar(
              title: Text("Lab Work"),
            ),
            body: SingleChildScrollView(
              child: Form(
                key: key,
                child: Column(
                  children: <Widget>[
                    TextFormField(
                        controller: firstName,
                        decoration: InputDecoration(labelText: 'Item Name'),
                        validator: (String? value) {
                          if (value != null && value.isEmpty) {
                            return "this field cannot be empty";
                          }
                          return null;
                        }),
                    TextFormField(
                        controller: LastName,
                        decoration: InputDecoration(labelText: 'Item Price'),
                        validator: (String? value) {
                          if (value != null && value.isEmpty) {
                            return "this field cannot be empty";
                          }
                          return null;
                        }),
                    TextFormField(
                        controller: email,
                        decoration: InputDecoration(labelText: 'Item Description'),
                        validator: (String? value) {
                          if (value != null && value.isEmpty) {
                            return "this field cannot be empty";
                          }
                          return null;
                        }),
                    ElevatedButton(
                      onPressed: () {
                        if (key.currentState!.validate()) {
                          surj=[firstName,LastName,email];
                          putProduct(surj);
                          print("record inserted");
                        }
                      },
                      child: Text('Save'),
                    )
                  ],
                ),
              ),
            ));
      }
    }
    
    class listOFallUrl {
      List<TextEditingController> l;
      listOFallUrl(this.l);
      void URL(List<TextEditingController> l) {
        print(l[0]);
        print(l[1]);
        print(l[2]);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2013-02-08
      • 2020-11-01
      • 1970-01-01
      相关资源
      最近更新 更多