Home > OS >  How do I navigate to next screen after validation?
How do I navigate to next screen after validation?

Time:01-21

SizedBox(
  height: 50,
  width: 160,
  child: Container(
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: const Color(0xFF0890F2)),
      child: TextButton(
          style: TextButton.styleFrom(
            backgroundColor: const Color(0xFF223843),
          ),
          onPressed: () {
            final form = _formkey.currentState;
            if (form != null && form.validate()) {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => MyLoginPage()));
            }
          },
          child: const Text(
            'Sign Up',
            style: TextStyle(color: Colors.white),
          ))),
),

CodePudding user response:

You can assign false on null case like

onPressed: () {
  final isValided =
      _formkey.currentState?.validate() ?? false;
  if (isValided) {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => MyLoginPage()));
  }
},

Find more about forms/validation

CodePudding user response:

Not need to create variable for these you can use directly bcz validate() function returns bool value.

Demo Code :

onPressed: () {
   if (_formkey.currentState!.validate()) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => MyLoginPage()));
   }
},
  • Related