Flutter -如何为文本字段创建控制器?

64jmpszr  于 2023-04-22  发布在  Flutter
关注(0)|答案(1)|浏览(157)

我正在使用Postman创建一个“创建帐户”页面。该页面包含文本字段“电子邮件,密码,名字,姓氏”。我已经创建了远程服务和产品控制器。并在创建帐户页面上调用该控制器。
如何为账户创建页面上的每个字段创建一个控制器。如何调用上面创建的方法的控制器?
产品控制器代码:

Future createaccount(email, password, firstname, lastname) async {
    dio.Response? response = await remoteServices.createaccount(email, password, firstname, lastname);

    if (response!.statusCode == 200) {
    }
  }

远程服务代码:

Future<Response?> createaccount(email, password, firstname, lastname) async {
    var uri = '$_url/create-customer';
    print(uri);

    try {
      final response = await dio.post(uri,data: {'email': email, 'password':password,'firstname':firstname, 'lastname':lastname});
      print(response);
      return response;
    } on DioError catch (e) {
      debugPrint(e.response!.data.toString());
      return e.response!.data;
    }
  }

创建帐户代码:

ProductController productController = Get.find();

  createAccount(){
  }
.....
Column(
            children: [
              Padding(
                padding: const EdgeInsets.only(top: 35),
                child: Container(
                  width: 310,
                  child: TextField(
                    decoration: InputDecoration(
                      label: RichText(
                        text: TextSpan(
                            text: 'First Name',
                            style: GoogleFonts.poppins(
                                color: Color(0xffc1c1c1),
                                fontSize: 17,
                                letterSpacing: 0.4),
                            children: const [
                              TextSpan(
                                  text: ' *',
                                  style: TextStyle(
                                    color: Colors.red,
                                    fontSize: 21,
                                  )),
                            ]),
                      ),
                    ),
                    onSubmitted: (Value){
                    },
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 20),
                child: Container(
                  width: 310,
                  child: TextField(
                    decoration: InputDecoration(
                      label: RichText(
                        text: TextSpan(
                            text: 'Last Name',
                            style: GoogleFonts.poppins(
                                color: Color(0xffc1c1c1),
                                fontSize: 17,
                                letterSpacing: 0.4),
                            children: const [
                              TextSpan(
                                  text: ' *',
                                  style: TextStyle(
                                    color: Colors.red,
                                    fontSize: 21,
                                  )),
                            ]),
                      ),
                    ),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 20),
                child: Container(
                  width: 310,
                  child: TextField(
                    decoration: InputDecoration(
                      label: RichText(
                        text: TextSpan(
                            text: 'Enter Your Email',
                            style: GoogleFonts.poppins(
                                color: Color(0xffc1c1c1),
                                fontSize: 17,
                                letterSpacing: 0.4),
                            children: const [
                              TextSpan(
                                  text: ' *',
                                  style: TextStyle(
                                    color: Colors.red,
                                    fontSize: 21,
                                  )),
                            ]),
                      ),
                    ),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 20),
                child: Container(
                  width: 310,
                  child: TextField(
                    obscureText: true,
                    decoration: InputDecoration(
                      label: RichText(
                        text: TextSpan(
                            text: 'Enter Your Password',
                            style: GoogleFonts.poppins(
                                color: Color(0xffc1c1c1),
                                fontSize: 17,
                                letterSpacing: 0.4),
                            children: const [
                              TextSpan(
                                  text: ' *',
                                  style: TextStyle(
                                    color: Colors.red,
                                    fontSize: 21,
                                  )),
                            ]),
                      ),
                      suffixIcon: Icon(CupertinoIcons.eye_slash_fill, size: 25),
                    ),
                  ),
                ),
              ),
cczfrluj

cczfrluj1#

它很简单,你需要为每个字段定义TextEditingController,然后传递给它们,并使用它们为你的字段定义控制器,因为你需要一个控制器

class MyApp extends StateLessWidget{
final TextEditingController _nameController=TextEditingController();
final TextEditingController _emailController=TextEditingController();  
@override
 Widget build(BuildContext context){
  return Scaffold(
  body:Column(
    children:[
//name feild
    TextFormFeild(
  controller:_nameController
)
//email feild
   TextFormFeild(
 controller:_emailController
)
//button
TextButton(onPress:(){
 //check if values are provided then doing api call , you could do form validation instead but this is traditional way 
if(_emailController.text.isNotEmpty&&_nameController.text.isNotEmpty){
  //call you api and do rest
  // call your controllers and use the text parammet to access user typed value
   createaccount(_nameController.text,_emailController.text,and....)
}
},
child:Text('login'),),])
);
}}

你定义的每个控制器都有一个名为text的参数,它可以给予你访问用户在字段中输入的值,你可以用它们把它们传递给服务器或..

相关问题