0

I am new to flutter, I handed back press event by onWillPop () to show alert dialog. But I need to show the alert dialog for home page only (like Are you want to exit?). In the code I have a type like current page if the current page is home, then only show Alert dialog or otherwise switch to home page.

Code

if(currentType == Home)
{
    showExitDialog();
}
else
{
   switchToHome();
}

Where can i handle this ?

Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60

1 Answers1

2

set custom leading property on your app bar.

The default value is to go back one page, so if you dont set nothing, back arrow will be there and it will do Navigator.of(context).maybePop();

From Navigator docs

If this is null and [automaticallyImplyLeading] is set to true, the [AppBar] will imply an appropriate widget. For example, if the [AppBar] is in a [Scaffold] that also has a [Drawer], the [Scaffold] will fill this widget with an [IconButton] that opens the drawer (using [Icons.menu]). If there's no [Drawer] and the parent [Navigator] can go back, the [AppBar] will use a [BackButton] that calls [Navigator.maybePop].

But if you want to override it do this

on your home page

  appBar: AppBar(
    leading: IconButton(icon: Icon(Icons.exit_to_app),onPressed: (){
      exitTheApp();
    },),
  ),

on your other AppBars

  appBar: AppBar(
    leading: IconButton(icon: Icon(Icons.home),onPressed: (){
      showHomePage();
    },),
  ),
Tree
  • 29,135
  • 24
  • 78
  • 98
  • thanks for your answer but if user click mobile default back button then how to handle ? – Magesh Pandian Jun 02 '18 at 13:43
  • you can use WillPopScope. check this question https://stackoverflow.com/questions/49356664/how-to-override-the-back-button-in-flutter – Tree Jun 02 '18 at 17:00