简体   繁体   English

如何在另一个 stful 小部件颤动中调用 stful 小部件中的方法

[英]how to call method in a stful widget inside a another stful widget flutter

I want to call Future UserSignUp() API connection part under ProOrNot stateful widget Floating Action Button.我想在ProOrNot 有状态小部件浮动操作按钮下调用Future UserSignUp() API 连接部分。 how can I do this?我怎样才能做到这一点? appreciate your help on this.感谢您对此的帮助。 Postman requests are successfully working.邮递员请求已成功运行。 ................................................................................................................................................................................................................................................................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... .........................................................

FloatingActionButton( onPressed: () {}, ), FloatingActionButton( onPressed: () {}, ),

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

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

class _SignUpFormState extends State<SignUpForm> {
  String userName = "";
  String email = "";
  String password = "";
  String mobileNumber = '';
  String postalCode = '';
  String address = '';
  File? image;

  final _formKey = GlobalKey<FormState>();
  bool typing = true;
  bool isLoading = false;
  bool pointerIgnore = false;
  bool _isObscure = true;



  Future UserSignUp() async {
    String fileName = '';
    var file;
    file = profileImage;
    fileName = file.path.split('/').last;
    dioo.FormData data = dioo.FormData.fromMap({
      'username': userName,
      'email': email,
      'mobileNumber': mobileNumber,
      'postalCode': postalCode,
      'address': address,
      "profilePicture": await dioo.MultipartFile.fromFile(file.path,
          filename: fileName, contentType: MediaType.parse('image/jpg')),
      'password': password,
      'device_token': '123456789',
    });
    try {
      print("try");
      var response = await Dio().post(BASE_API + "/user/register",
          data: data,
          options: Options(headers: {'Content-Type': 'application/json'}));
      print(response);

      if (response.data["success"] == true) {
        Get.snackbar(
          "Message",
          "Registered in successfully.",
          backgroundColor: AppGreen,
          colorText: textWhite,
        );
        Get.to(() => const HomeScreenCompanyList());
      } else {
        print(response.data["message"]);
        Get.snackbar("Message", response.data["message"],
            backgroundColor: AppGreen,
            borderWidth: 1,
            borderColor: Colors.grey,
            colorText: textWhite,
            icon: const Icon(
              Icons.error_outline_outlined,
              color: Colors.red,
              size: 30,
            ));
      }
    } catch (e) {
      print("catch");
      Get.snackbar("Error", "Something went wrong.Please contact admin",
          backgroundColor: AppGreen,
          borderWidth: 1,
          borderColor: Colors.grey,
          colorText: textWhite,
          icon: const Icon(
            Icons.error_outline_outlined,
            color: Colors.red,
            size: 30,
          ));
      //print(e.error.toString());
    }
  }



  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Form(
        key: _formKey,
        autovalidateMode: AutovalidateMode.disabled,
        child: Container(
          margin: const EdgeInsets.only(left: 20, right: 20),
          child: Column(
            children: [
              const SizedBox(
                height: 20,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: const [
                  Text("Username",
                      style: TextStyle(
                        fontSize: 17,
                        color: textBlack,
                      )),
                ],
              ),
              SizedBox(
                height: height * 0.005,
              ),
              TextFormField(
                style: const TextStyle(
                  fontSize: 15,
                  // fontWeight: FontWeight.bold
                ),
                textAlignVertical: TextAlignVertical.center,
                decoration: InputDecoration(
                    fillColor: const Color(0XFFC4C4C4).withOpacity(.3),
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    errorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    isDense: true,
                    border: InputBorder.none),
                validator: (String? name) {
                  if (name == null || name.isEmpty) {
                    return "User name can't be empty";
                  }
                  return null;
                },
                onChanged: (String? text) {
                  userName = text!;
                  print(userName);
                },
                // onSaved: (value) {
                //   SignUpUserData['userName'] = value!;
                // },
              ),
              SizedBox(
                height: height * 0.02,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: const [
                  Text("Email",
                      style: TextStyle(
                        fontSize: 17,
                        color: textBlack,
                        // fontWeight: FontWeight.bold,
                      )),
                ],
              ),
              SizedBox(
                height: height * 0.005,
              ),
              TextFormField(
                style: const TextStyle(
                  fontSize: 15,
                  // fontWeight: FontWeight.bold
                ),
                textAlignVertical: TextAlignVertical.center,
                decoration: InputDecoration(
                    fillColor: const Color(0XFFC4C4C4).withOpacity(.3),
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    errorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: errorRed),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: errorRed),
                    ),
                    isDense: true,
                    border: InputBorder.none),
                validator: (email) {
                  if (EmailValidator.validate(email!)) {
                    return null;
                  } else {
                    return 'Enter a valid email address';
                  }
                },
                onChanged: (String? text) {
                  email = text!;
                  print(email);
                },
              ),
              SizedBox(
                height: height * 0.02,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: const [
                  Text("Mobile Number",
                      style: TextStyle(
                        fontSize: 17,
                        color: textBlack,
                      )),
                ],
              ),
              SizedBox(
                height: height * 0.005,
              ),
              TextFormField(
                keyboardType: TextInputType.phone,
                style:
                const TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                textAlignVertical: TextAlignVertical.center,
                decoration: InputDecoration(
                    fillColor: const Color(0XFFC4C4C4).withOpacity(.3),
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    errorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    isDense: true,
                    border: InputBorder.none),
                validator: (String? mobileNumber) {
                  if (mobileNumber == null || mobileNumber.isEmpty) {
                    return "Mobile Number can't be empty";
                  } else if (mobileNumber.length != 10) {
                    return 'Mobile Number must be 10 digit';
                  }

                  return null;
                },
                onChanged: (String? text) {
                  mobileNumber = text!;
                  print(mobileNumber);
                },
              ),
              SizedBox(
                height: height * 0.02,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: const [
                  Text("Postal Code",
                      style: TextStyle(
                        fontSize: 17,
                        color: textBlack,
                      )),
                ],
              ),
              SizedBox(
                height: height * 0.005,
              ),
              TextFormField(
                style: const TextStyle(
                  fontSize: 15,
                  // fontWeight: FontWeight.bold
                ),
                textAlignVertical: TextAlignVertical.center,
                decoration: InputDecoration(
                    fillColor: const Color(0XFFC4C4C4).withOpacity(.3),
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    errorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    isDense: true,
                    border: InputBorder.none),
                validator: (String? text) {
                  if (text == null || text.isEmpty) {
                    return "Postal code can't be empty";
                  } else {
                    return null;
                  }
                },
                onChanged: (String? text) {
                  postalCode = text!;
                  print(postalCode);
                },
              ),
              SizedBox(
                height: height * 0.02,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: const [
                  Text("Address",
                      style: TextStyle(
                        fontSize: 17,
                        color: textBlack,
                      )),
                ],
              ),
              SizedBox(
                height: height * 0.005,
              ),
              TextFormField(
                style: const TextStyle(
                  fontSize: 15,
                  // fontWeight: FontWeight.bold
                ),
                textAlignVertical: TextAlignVertical.center,
                decoration: InputDecoration(
                    fillColor: const Color(0XFFC4C4C4).withOpacity(.3),
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    errorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    isDense: true,
                    border: InputBorder.none),
                validator: (String? text) {
                  if (text == null || text.isEmpty) {
                    return "Address can't be empty";
                  } else {
                    return null;
                  }
                },
                onChanged: (String? text) {
                  address = text!;
                  print(address);
                },
              ),
              SizedBox(
                height: height * 0.02,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: const [
                  Text("Password",
                      style: TextStyle(
                        fontSize: 17,
                        color: textBlack,
                        // fontWeight: FontWeight.bold,
                      )),
                ],
              ),
              SizedBox(
                height: height * 0.005,
              ),
              TextFormField(
                //controller: passwordEditingController,
                obscureText: _isObscure,
                decoration: InputDecoration(
                    fillColor: const Color(0XFFC4C4C4).withOpacity(.3),
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30),
                        borderSide: const BorderSide(
                            width: 0, style: BorderStyle.none)),
                    errorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: const BorderSide(color: Colors.red),
                    ),
                    isDense: true,
                    border: InputBorder.none,
                    suffixIcon: IconButton(
                        icon: Icon(
                          _isObscure ? Icons.visibility : Icons.visibility_off,
                          color: textGrey,
                        ),
                        onPressed: () {
                          setState(() {
                            _isObscure = !_isObscure;
                          });
                        }),
                    hintText: "ex: James@123",
                    hintStyle: const TextStyle(
                        color: textGrey,
                        fontFamily: "Paralucent",
                        fontSize: 14)),
                style: const TextStyle(
                  color: textGrey,
                ),
                validator: (String? Password) {
                  RegExp regex = RegExp(
                      '(?=.*[A-Z])(?=.*[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}');
                  if (Password!.isEmpty) {
                    return "Password can't be empty";
                  } else if (password.length < 8) {
                    return "Password should be more than 8 characters";
                  } else if (!regex.hasMatch(password)) {
                    return "password must be requied format ex:@12Sr%D4";
                  }
                  return null;
                },
                onChanged: (String? text) {
                  password = text!;
                  print(password);
                },
              ),
              SizedBox(
                height: height * 0.05,
              ),
              Center(
                child: GestureDetector(
                    child: MainButton("SignUp"),
                 

                    onTap: () {
                      FocusManager.instance.primaryFocus?.unfocus();
                      if (_formKey.currentState!.validate() &&
                          profileImage != null) {
                        Get.defaultDialog(
                            title: "Are you a Professional?",
                            titleStyle: TextStyle(
                                fontFamily: "Dubai",
                                color: textWhite,
                                fontSize: 20),
                            content: ProOrNot(),
                            backgroundColor: AppGreen.withOpacity(0.8),
                            barrierDismissible: true);
                      }

                      else if (profileImage == null) {
                        Get.snackbar(
                            "Message", "Profile Picture can't be empty",
                            backgroundColor: textWhite.withOpacity(0.5),
                            borderWidth: 1,
                            borderColor: textGrey,
                            colorText: textGrey,
                            icon: Icon(
                              Icons.error_outline_outlined,
                              color: Colors.redAccent,
                              size: 30,
                            ));
                      }

                      else {
                        Get.snackbar("Message", "Please Enter valid Details",
                            backgroundColor: textWhite.withOpacity(0.5),
                            borderWidth: 1,
                            borderColor: textGrey,
                            colorText: textGrey,
                            icon: Icon(
                              Icons.error_outline_outlined,
                              color: Colors.redAccent,
                              size: 30,
                            ));
                      }
                    }
                    ),
              ),
            ],
          ),
        ),
      ),
    );
  }


}

enum Proffesional { Yes, No }
Proffesional? _character;

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

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

class _ProOrNotState extends State<ProOrNot> {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        //professional___________________________
        FloatingActionButton(
          onPressed: () {
           // print(username);
            Get.to(() => ProSignup());
          },
          backgroundColor: textWhite,
          child: Icon(
            Icons.check_sharp,
            size: 30,
            color: AppGreen,
          ),
        ),
        SizedBox(
          width: 50,
        ),
        //user_____________________
        FloatingActionButton(
          onPressed: () {
    
          },
         
          backgroundColor: textWhite,
          child: Icon(
            Icons.clear_sharp,
            size: 30,
            color: Colors.redAccent,
          ),
        ),
      ],
    );
  }
}

I am not sure if this will be a valid solution for your specific case but you can make我不确定这是否是针对您的特定情况的有效解决方案,但您可以
Future UserSignUp(){ } static like this: static Future UserSignUp(){ } . Future UserSignUp(){ }像这样静态: static Future UserSignUp(){ } This way, it can be used inside other classes.这样,它可以在其他类中使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数字(通过单击按钮“OnPressed 属性”)从 stful 小部件(应用程序的第一个屏幕)传递到第二页的 ListView.builder - How to pass a number (through clicking Button "OnPressed property) from a stful widget (First screen on the app) to ListView.builder at Second Page 我怎么能解决 flutter 中的那种 stful setState - How could i solve that kind of stful setState in flutter 从另一个有状态小部件调用一个有状态小部件中的方法 - Flutter - call method in one stateful widget from another stateful widget - Flutter 如何在小部件中调用动态方法 - flutter - How to call dynamic method in widget - flutter Flutter,如何从返回的 Widget 调用 Stateful Widget 内部的函数? - Flutter, how to call a function inside Stateful Widget from a returned Widget? Flutter:如何知道我的小部件是否在另一个特定类型的小部件中? - Flutter: How to know if my widget is inside another widget of a specific type? Flutter 如何以编程方式调用自定义小部件中的方法? - Flutter how to programmatically call method in custom widget? 如何在Flutter中将Webview嵌入另一个小部件中? - How to embed webview inside another widget in Flutter? Flutter 测试:在另一个小部件中寻找一个小部件 - Flutter Test: Look for a widget inside of another widget 如何从另一个有状态小部件调用一个有状态小部件中的方法 - how to Call method in one stateful widget from another stateful widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM