Home > Software engineering >  How can I control a text typed in textField in Flutter?
How can I control a text typed in textField in Flutter?

Time:07-23

 signInButton(
              context,
              () {
                if (_mail.text.isEmpty || _password.text.isEmpty) {
                  Fluttertoast.showToast(
                      msg: "Yanlış veya eksik bilgi girdiniz.",
                      toastLength: Toast.LENGTH_LONG,
                      gravity: ToastGravity.BOTTOM_RIGHT,
                      timeInSecForIosWeb: 3,
                      backgroundColor: Colors.red,
                      textColor: Colors.white,
                      fontSize: 16.0);
                } else {
                  Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => const StudentWidget(),
                  ));
                }
              },
              false,
            )

I created a widget called signInButton. This widget will control the texts during login and will redirect the user to different pages accordingly. For example, if he enters the mail section as @gmail.com on the login page, he will be redirected to one page, and if he enters as @hotmail.com, he will be redirected to another page. How can I check if the user is trying to login as @hotmail or @gmail?

  Container signInButton(
  BuildContext context,
  Function onTap,
  bool isLogin,
) {
  return Container(
    width: 250,
    height: 50,
    margin: const EdgeInsets.fromLTRB(0, 30, 0, 20),
    decoration: BoxDecoration(borderRadius: BorderRadius.circular(20.0)),
    child: ElevatedButton(
      style: ElevatedButton.styleFrom(
          primary: const Color.fromARGB(255, 221, 144, 56)),
      onPressed: () {
        onTap();
      },
      child: Text('Giriş Yap',
          style: GoogleFonts.poppins(
            fontWeight: FontWeight.w500,
          )),
    ),
  );
}

This is my signInButton widget

   TextFormField textFormField(
    String text, TextEditingController controller, bool isPasswordType) {
  return TextFormField(
      controller: controller,
      obscureText: isPasswordType,
      enableSuggestions: !isPasswordType,
      autocorrect: !isPasswordType,
      style: GoogleFonts.montserrat(),
      decoration: InputDecoration(
          labelText: text,
          border: const OutlineInputBorder(),
          labelStyle: const TextStyle(
            fontWeight: FontWeight.bold,
          )),
      textAlign: TextAlign.center,
      validator: (value) {
        if (value == "@hotmail") return 'Hata';
        return null;
      });
}

TextFormField Widget where I get the user's login information

CodePudding user response:

Assuming that the text of the email is obtained from a textFormField or textField, you could within that widget, that is, the onChanged attribute and within the function, make the necessary checks to obtain what you are requesting. something like doing a split of the value of the mail between the '@' and the '.' that comes after the at sign to get the text that is in between this, and then see if that text obtained from the split is '==' to 'hotmail' or 'gmail'.

CodePudding user response:

You can use contains to check ifa string has a specific string

 signInButton(
              context,
              () {
                if (_mail.text.isEmpty || _password.text.isEmpty) {
                  Fluttertoast.showToast(
                      msg: "Yanlış veya eksik bilgi girdiniz.",
                      toastLength: Toast.LENGTH_LONG,
                      gravity: ToastGravity.BOTTOM_RIGHT,
                      timeInSecForIosWeb: 3,
                      backgroundColor: Colors.red,
                      textColor: Colors.white,
                      fontSize: 16.0);
                } else {
                  if(_mail.text.contains("@gmail.com"){
Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => const StudentWidget(),
                  ));
                 }
                else{
               //Navigate to another route
                 }
                }
              },
              false,
            )
  • Related