skip to Main Content

The name parameter ‘on Pressed’ isn’t defined

I am trying to make an app with a sign in page but i can’t make the button work


import 'package:flutter/material.dart';

class SignIn extends StatefulWidget {
  const SignIn({super.key});


  @override
  State<SignIn> createState() => _SignInState();
}

class _SignInState extends State<SignIn> {


  // ignore: unused_field
  final AuthService _auth = AuthService();

  // text field state
  String email = '';
  String password = '';



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.brown[100],
      appBar: AppBar(
        backgroundColor: Colors.brown[400],
        elevation: 0.0,
        title: const Text('Sign in to LejweConnect'),

      ),
      body: Container(
        padding: const EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
        child: Form(
          child: Column(
            children: <Widget>[
              const SizedBox(height: 20.0),
              TextFormField(
                onChanged: (val){
                  setState(() => email = val);

                }
              ),
              const SizedBox(height: 20.0),
                TextFormField(
                  obscureText: true,
                  onChanged: (val){
                    setState(() => password = val);

                  }
                ),
                const SizedBox(height: 20.0),
                Container(
                  color: Colors.pink[400],
                  // ignore: prefer_const_constructors
                  child: Text(
                   'Sign in',
                  style: const  TextStyle(color: Colors.white),
                  ),
                  
                  onPressed: () async {
                    
                  }

The named parameter ‘onPressed’ isn’t defined.
Try correcting the name to an existing named parameter’s name, or defining a named parameter with the name ‘onPressed’.dartundefined_named_parameter

How do i fix this problem

2

Answers


  1. You are passing onPressed to a container which doesn’t accept onPressed as a parameter that’s why you are getting onPressed error.

    Only certain widgets accept onPressed parameters like buttons which you can use to do the press behaviour. Equivalently if you want to make the container clickable you can wrap it with InkWell and pass a the function to onTap. Eg:

    InkWell(
      onTap: () {}, // paste your onPressed fn here
      child: Container(
        child: Text("test"),
      ),
    )
    
    
    Login or Signup to reply.
  2. The Container widget doesn’t have onTap or onPress method.

    • Using InkWell widget to make a clickable Container

       InkWell(child: Container(...),
       onTap: () {
       print("Tapped on container");
       },);
      
    • Using GestureDetector widget to detect a gesture

      GestureDetector(
      onTap: () {
      print("Tapped a Container");
      },
      child: Container(…),
      )

    Both onPressed and onTap are callbacks used to handle user interactions such as taps or clicks. They are commonly used with different widgets.

    onPressed is mostly used with buttons like RaisedButton, TextButton, IconButton, ElevatedButton

    onTap is mainly used with GestureDetector, InkWell, InkResponse, or other tappable areas

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search