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

问题描述 投票:0回答:1

我正在使用 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),
                    ),
                  ),
                ),
              ),
flutter dart post controller postman
1个回答
0
投票

太简单了 您需要为每个字段定义

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
的参数,它使您可以访问字段中用户键入的值,您可以使用它们将它们传递给服务器或..

© www.soinside.com 2019 - 2024. All rights reserved.