skip to Main Content
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),
          ))),
),

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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()));
       }
    },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search