1

我想创建一个注册表单,但出现如下错误。输入表单元素有效,但是当 buildMail 处于活动状态时,我使用 buildName 创建的小部件验证不起作用。

编辑:总共最多验证7个Textformfield,但是当Textformfield超过7个时,它不会检查更多,是否有Textformfield数量限制进行验证?

截屏

我的代码


class RegisterForm extends StatefulWidget {
  @override
  _RegisterFormState createState() => _RegisterFormState();
}

class _RegisterFormState extends State<RegisterForm> {
  final _formkey = GlobalKey<FormState>();
  TextEditingController nameController = TextEditingController();
  TextEditingController surNameController = TextEditingController();
  TextEditingController userNameController = TextEditingController();
  TextEditingController passWordController = TextEditingController();
  TextEditingController passWordController2 = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController phoneController2 = TextEditingController();
  TextEditingController phoneController3 = TextEditingController();
  TextEditingController cityCodeController = TextEditingController();
  TextEditingController epostaController = TextEditingController();
  int genderInt;
  bool hatirlaDeger = false;

  Widget buildName() {
    return TextFormField(
      controller: nameController,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Ad",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      keyboardType: TextInputType.text,
      validator: (value) {
        if (value.isEmpty) {
          return "Bu alan boş bırakılamaz";
        } else if (value.length < 2) {
          return "Ad alanı en az 2 karakterli olmalıdır";
        } else if (value.length > 15) {
          return "Ad alanı en fazla 15 karakterli olabilir";
        }
      },
    );
  }

  Widget buildSurame() {
    return TextFormField(
      controller: surNameController,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Soyad",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      keyboardType: TextInputType.text,
      validator: (value) {
        if (value.isEmpty) {
          return "Bu alan boş bırakılamaz";
        } else if (value.length < 2) {
          return "Soyad alanı en az 2 karakterli olmalıdır";
        } else if (value.length > 15) {
          return "Soyad alanı en fazla 15 karakterli olabilir";
        }
      },
    );
  }

  // Widget buildMail() {
  //   return TextFormField(
  //     controller: epostaController,
  //     decoration: InputDecoration(
  //       prefixIcon: Icon(Icons.mail),
  //       labelText: "E-Mail",
  //       helperText: "",
  //       fillColor: Colors.red,
  //       border: OutlineInputBorder(
  //         borderRadius: BorderRadius.circular(10),
  //         borderSide: BorderSide(),
  //       ),
  //     ),
  //     keyboardType: TextInputType.text,
  //     validator: (value) {
  //       if (value.length == 2) {
  //         return "test";
  //       }
  //     },
  //   );
  // }

  Widget buildUsername() {
    return TextFormField(
      controller: userNameController,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Kullanıcı Adı",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      keyboardType: TextInputType.text,
      validator: (value) {
        if (value.isEmpty) {
          return "Bu alan boş bırakılamaz";
        } else if (value.length < 4) {
          return "Kullanıc adı alanı en az 4 karakterli olmalıdır";
        } else if (value.length > 15) {
          return "Kullanıc adı alanı en fazla 15 karakterli olabilir";
        }
      },
    );
  }

  Widget buildPassWord() {
    return TextFormField(
      controller: passWordController,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Parola",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      obscureText: true,
      keyboardType: TextInputType.visiblePassword,
      validator: (value) {
        if (value.isEmpty) {
          return "Bu alan boş bırakılamaz";
        } else if (value.length < 6) {
          return "Parola alanı en az 6 karakterli olmalıdır";
        }
      },
    );
  }

  Widget buildConfirmPassWord() {
    return TextFormField(
      controller: passWordController2,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Parola Doğrulama",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      obscureText: true,
      keyboardType: TextInputType.visiblePassword,
      validator: (value) {
        if (passWordController2.text != passWordController.text) {
          return "Parolalar eşleşmiyor";
        }
      },
    );
  }

  Widget buildPhone() {
    return TextFormField(
      controller: phoneController,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Telefon",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      keyboardType: TextInputType.number,
      validator: (value) {
        if (value.length != 10) {
          return "Telefon alanı 10 karakterli olmalıdır";
        }
        if (value[0] == "0") {
          return "0 ile başlayamaz";
        }
      },
    );
  }

  Widget buildPhone2() {
    return TextFormField(
      controller: phoneController2,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Telefon2",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      keyboardType: TextInputType.number,
      validator: (value) {
        if (value.length != 30) {
          return "Telefon alanı 10 karakterli olmalıdır";
        }
      },
    );
  }

  Widget buildPhone3() {
    return TextFormField(
      controller: phoneController3,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Telefon3",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      keyboardType: TextInputType.number,
      validator: (value) {
        if (value.length != 30) {
          return "Telefon alanı 10 karakterli olmalıdır";
        }
      },
    );
  }

  Widget buildCityCode() {
    return TextFormField(
      controller: cityCodeController,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.short_text),
        labelText: "Plaka",
        helperText: "",
        fillColor: Colors.red,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
          borderSide: BorderSide(),
        ),
      ),
      keyboardType: TextInputType.number,
      validator: (value) {
        if (value.length != 2) {
          return "Plaka alanı 2 karakterli olmalıdır";
        }
        if (int.parse(value) < 1 || int.parse(value) > 81) {
          return "Plaka kodu geçerli değil";
        }
      },
    );
  }

  Widget genderPick() {
    return GenderPickerWithImage(
      maleText: "Erkek",
      femaleText: "Kadın",
      isCircular: true,
      showOtherGender: false,
      verticalAlignedText: false,
      equallyAligned: false,
      animationDuration: Duration(milliseconds: 300),
      opacityOfGradient: 0.4,
      padding: const EdgeInsets.all(5),
      size: MediaQuery.of(context).size.aspectRatio * 120,
      selectedGender: Gender.Female,
      selectedGenderTextStyle:
          TextStyle(color: Color(0xFF8b32a8), fontWeight: FontWeight.bold),
      unSelectedGenderTextStyle:
          TextStyle(color: Colors.white, fontWeight: FontWeight.normal),
      onChanged: (Gender gender) {
        if (gender == Gender.Female) {
          genderInt = 0;
        } else {
          genderInt = 1;
        }
      },
    );
  }

  Widget datePick() {
    return Container(
      height: 100,
      child: CupertinoDatePicker(
        minimumYear: 1900,
        maximumYear: DateTime.now().year - 12,
        mode: CupertinoDatePickerMode.date,
        initialDateTime: DateTime(DateTime.now().year - 12),
        onDateTimeChanged: (DateTime newDateTime) {},
      ),
    );
  }

  Widget accept() {
    return Row(
      children: [
        Checkbox(
          activeColor: Colors.orange,
          value: hatirlaDeger,
          checkColor: Colors.white,
          onChanged: (checkDeger) {
            setState(() {
              hatirlaDeger = checkDeger;
            });
          },
        ),
        GestureDetector(
          child: Text(
            "Kullanım Koşullarını Kabul Ediyorum",
            style:
                TextStyle(fontSize: MediaQuery.of(context).size.width * 0.031),
          ),
        ),
      ],
    );
  }

  Widget regButton() {
    return Row(
      children: <Widget>[
        Expanded(
          child: RaisedButton(
            child: Text(
              "Kayıt Ol",
              style: TextStyle(color: Colors.white),
            ),
            color: Colors.amber,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
                side: BorderSide(color: Colors.transparent)),
            onPressed: () {
              if (_formkey.currentState.validate()) {
                print("Doğru");
              } else {
                print("Yanlış");
              }
            },
          ),
        ),
      ],
    );
  }

  Widget cancelButton() {
    return Row(
      children: <Widget>[
        Expanded(
          child: RaisedButton(
            color: Colors.blue,
            child: Text(
              "Vazgeç",
              style: TextStyle(color: Colors.white),
            ),
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
                side: BorderSide(color: Colors.transparent)),
            onPressed: () {
              Navigator.popAndPushNamed(context, "/");
            },
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      // autovalidate: true,
      key: _formkey,
      child: ListView(
        children: [
          buildName(),
          buildSurame(),
          buildUsername(),
          buildPassWord(),
          buildConfirmPassWord(),
          buildPhone(),
          buildPhone2(),
          buildPhone3(),
          buildCityCode(),
          genderPick(),
          Divider(),
          datePick(),
          accept(),
          regButton(),
          cancelButton(),
        ],
      ),
    );
  }
}

4

0 回答 0