Flutter Dart,文本字段打印相同的内容,即使它们具有不同的控制器

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

这是页面,正如您所看到的,一些特殊的SizedBox确实有不同的控制器来处理来自用户的不同输入。然而,当我写入电子邮件或密码或密码确认时,它也会打印到其他两个中,但它不会对controller.name 执行此操作。我不知道如何解决它。

class SignUpController extends GetxController {
  static SignUpController get instance => Get.find();

  final email = TextEditingController();
  final password = TextEditingController();
  final name = TextEditingController();
  final confirmPassword = TextEditingController();

  void registerUser(String email, String password, String name) {
    AuthentificationRepository.instance.createUser(email, password, name);
  }
  @override
  void onClose() {
    email.dispose();
    password.dispose();
    name.dispose();
    confirmPassword.dispose();
    super.onClose();
  }
}
class _AskIfCompanyState extends State<AskIfCompany> {
  final FocusNode _focusNode = FocusNode();
  final _formKey = GlobalKey<FormState>();

  bool isJobSeeker = false;
  bool isCompany = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  void _handleCheckBoxChange(bool? selected, String type) {
    setState(() {
      if (type == 'jobSeeker') {
        isJobSeeker = selected ?? false;
        if (isJobSeeker) {
          isCompany = false;
        }
      } else if (type == 'company') {
        isCompany = selected ?? false;
        if (isCompany) {
          isJobSeeker = false;
        }
      }
    });
  }

  void _requestFocus() {
    FocusScope.of(context).requestFocus(_focusNode);
  }

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(SignUpController());


    var screenWidth = MediaQuery.of(context).size.width;
    var screenHeight = MediaQuery.of(context).size.height;

    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Row(
                children: <Widget>[
                  buildLogo(screenWidth, screenHeight),
                ],
              ),
              const SizedBox(height: 25),
              Row(
                children: [
                  SizedBox(width: screenWidth * 0.15),
                  const Text(
                    'Nom entier:',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 5),
              SizedBox(
                child: buildTextField(
                  context: context,
                  controller: controller.name,
                  hinttext: 'Entrez votre nom et prénom',
                ),
              ),
              const SizedBox(height: 5),
              Row(
                children: [
                  SizedBox(width: screenWidth * 0.15),
                  const Text(
                    'Email:',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 5),
              SizedBox(
                child: buildTextField(
                  context: context,
                  controller: controller.email,
                  hinttext: 'Entrez votre mail',
                ),
              ),
              const SizedBox(height: 5),
              Row(
                children: [
                  SizedBox(width: screenWidth * 0.15),
                  const Text(
                    'Mot de passe:',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 5),
              SizedBox(
                child: buildTextFieldPassword(
                  context: context,
                  controller: controller.password,
                  hinttext: 'Entrez votre mot de passe',
                ),
              ),
              const SizedBox(height: 5),
              Row(
                children: [
                  SizedBox(width: screenWidth * 0.15),
                  const Text(
                    'Confirmez votre mot de passe:',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 5),
              SizedBox(
                child: buildTextFieldPassword(
                  controller: controller.confirmPassword,
                  context: context,
                  hinttext: 'Confirmez votre mot de passe',
                ),
              ),
              const SizedBox(height: 15),
              Row(
                children: [
                  SizedBox(width: screenWidth * 0.15),
                  buildRoundCheckBox(
                    isChecked: isJobSeeker,
                    onChanged: (selected) =>
                        _handleCheckBoxChange(selected, 'jobSeeker'),
                    borderColor: const Color(0xffffd5c2),
                    checkedColor: const Color(0xffffd5c2),
                    uncheckedColor: Colors.grey[200] ?? Colors.grey,
                    checkedWidget: const Icon(Icons.check, color: Colors.black),
                    size: 24,
                  ),
                  const SizedBox(width: 10),
                  const Text(
                    'Je suis un candidat',
                    style: TextStyle(fontSize: 15),
                  ),
                ],
              ),
              const SizedBox(height: 20),
              Row(
                children: [
                  SizedBox(width: screenWidth * 0.15),
                  buildRoundCheckBox(
                    isChecked: isCompany,
                    onChanged: (selected) =>
                        _handleCheckBoxChange(selected, 'company'),
                    borderColor: const Color(0xffffd5c2),
                    checkedColor: const Color(0xffffd5c2),
                    uncheckedColor: Colors.grey[200] ?? Colors.grey,
                    checkedWidget: const Icon(Icons.check, color: Colors.black),
                    size: 24,
                  ),
                  const SizedBox(width: 10),
                  const Text(
                    'Je suis une entreprise (HR)',
                    style: TextStyle(fontSize: 15),
                  ),
                ],
              ),
              const SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  buildElevatedButton(
                    buttonText: 'S"inscrire',
                    onPressed: () {
                      if (_formKey.currentState!.validate()) {
                        SignUpController.instance.registerUser(
                          controller.email.text.trim(),
                          controller.password.text.trim(),
                          controller.name.text.trim(),
                        );
                      }
                    },
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  GestureDetector(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const LogIn()),
                      );
                    },
                    child: const Text(
                      'Déja un compte? Se connecter',
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 12,
                        fontWeight: FontWeight.w400,
                        decoration: TextDecoration.underline,
                        height: 0,
                      ),
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

我尝试重新输入控制器,但它没有做任何事情,我还向 chatGPT 询问了有关不当行为的更多信息,但它帮不了我太多,所以我陷入了困境。

flutter dart mobile
1个回答
0
投票

我发现了我的问题,它实际上来自盒子的构建功能!

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