wahyu
wahyu

Reputation: 2425

Back button becomes disabled when using onWillPop and it happens in the first build of apps

I am using onWillPop to navigate the user to my homescreen when user clicks back button from their phone, here is part of the code:

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(
            builder: (BuildContext context) {
              return HomeAssesmentScreen();
            },
          ),
        );
        return Future.value(true);
      },
      child: Scaffold(

However, the problem is that I can not click my back button. Back button becomes disabled and I can not navigate user to another screen. Is there a way to re-activate the back button?

Upvotes: 0

Views: 789

Answers (2)

Zah
Zah

Reputation: 116

this should work: probably you're missing the async / await

Future<bool> _onWillPop() async {
    await Navigator.of(context).pushReplacement(
          MaterialPageRoute(
            builder: (BuildContext context) {
              return HomeAssesmentScreen();
            },
          ),
        );
    return true;
  }


Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: _onWillPop,
        child: Scaffold(

Upvotes: 1

savke
savke

Reputation: 296

You use Navigator.of(context).pushReplacement() method. This method just replace current page on Stack of pages with your pushed MaterialPageRoute().

If you want a back button, then use method push() instead of pushReplacement().

Upvotes: 1

Related Questions