Samuel
Samuel

Reputation: 592

Navigator.push not working as its supposed to be

I am trying to go to the LoginScreen from my StartScreen but it does not work when I click on the elevated button

PersonStatus(
              label: 'EMPLOYER',
              imageProvider: 'assets/images/employer.png',
              onPressed: (context) {
                int employerNum = 1;
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => LoginScreen(
                        employerNumber: employerNum,
                      ),
                    ));
              },
            ),

Following is the LoginScreen

class LoginScreen extends StatefulWidget {
  final int employerNumber;
  const LoginScreen({Key? key, required this.employerNumber}) : super(key: key);

  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  @override
  Widget build(BuildContext context) {
    return Form(
      child: ListView(
        children: [
          PersonStatus(
              label: widget.employerNumber == 1 ? 'EMPLOYER' : 'EMPLOYEE',
              imageProvider: widget.employerNumber == 1
                  ? 'assets/images/employer.png'
                  : 'assets/images/employee.png'),
        ],
      ),
    );
  }
}

Although I passed some parameters forward I expected it to work.

Upvotes: 0

Views: 610

Answers (3)

Yash Bhansali
Yash Bhansali

Reputation: 450

I guess the problem is with the Person Status where in the paramaters you have used "onPressed", I think you have not written that part correctly(meaning you have to define your custom function). that is why it is not working.. try changing it so share that part of code(Person Status) so I can suggest some workaround. If anything works in that part kindly upvote and do share your mistake ;)

Upvotes: 1

Cyrus the Great
Cyrus the Great

Reputation: 5942

You can create callback for PersonStatus:

PersonStatus(
              Function(int result) onTap// it's a callback
              label: 'EMPLOYER',
              imageProvider: 'assets/images/employer.png',
              onPressed: (context) {
                int employerNum = 1;
                onTap(employerNum);
              },
            )

and then use it:

class LoginScreen extends StatefulWidget {
  final int employerNumber;
  const LoginScreen({Key? key, required this.employerNumber}) : super(key: key);

  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  @override
  Widget build(BuildContext context) {
    return Form(
      child: ListView(
        children: [
          PersonStatus(
              label: widget.employerNumber == 1 ? 'EMPLOYER' : 'EMPLOYEE',
              imageProvider: widget.employerNumber == 1
                  ? 'assets/images/employer.png'
                  : 'assets/images/employee.png',
            onTap: (result) {
               Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => LoginScreen(
                    employerNumber: employerNum,
                  ),
                ));

            },),
        ],
      ),
    );
  }
}

Upvotes: 0

Wouter
Wouter

Reputation: 849

Maybe you are passing the wrong context to the navigator, try removing the context parameter in the onpressed:

PersonStatus(
              label: 'EMPLOYER',
              imageProvider: 'assets/images/employer.png',
              onPressed: () {
                int employerNum = 1;
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => LoginScreen(
                        employerNumber: employerNum,
                      ),
                    ));
              },
            ),

Upvotes: 0

Related Questions