close

how to override the back button in flutter? [duplicate]

Vacillate! Handle back catch in a shudder Application | Override back bolt button in App bar()

Want to go back?

An application has numerous screens for item highlight posting and incorporates various screens to explores. so based on the current activity, a client needs to make another move when press back catch. how to deal with in vacillate?

  1. We need to show a spring up before the nearby application or close some association that application utilizing.
  2. That can be utilized to affirm that the client needs to dispose of their progressions when the press back catch or back route bolt.

In what capacity can get the back squeezed occasion in Flutter?

You can deal with a back squeezed occasion in the Flutter with assistance of WillPopScope gadget. also, you will discover onWillPop technique

@override
Widget build(BuildContext context) {
  return WillPopScope(
    onWillPop: _onBackPressed,
    child: new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "On Back pressed",
          style: new TextStyle(color: Colors.white),
        ),
      ),
      body: new Center(
        child: new Text("Home Page"),
      ),
    ),
  );
}

Execute _onBackPressed Method with ready exchange

Future<bool> _onBackPressed() {
  return showDialog(
    context: context,
    builder: (context) => new AlertDialog(
      title: new Text('Are you sure?'),
      content: new Text('Do you want to exit an App'),
      actions: <Widget>[
        new GestureDetector(
          onTap: () => Navigator.of(context).pop(false),
          child: Text("NO"),
        ),
        SizedBox(height: 16),
        new GestureDetector(
          onTap: () => Navigator.of(context).pop(true),
          child: Text("YES"),
        ),
      ],
    ),
  ) ??
      false;
}
Image for post

Abrogate back bolt button in Appbar()

Screen Navigation:

first screen → second screen (Press second screen application bar back catch) → Third screen

→ abrogate the main technique in AppBar

new AppBar(
  title: new Text(
    "Second screen",
    style: new TextStyle(color: Colors.white),
  ),
  leading: new IconButton(
    icon: new Icon(Icons.arrow_back),
    onPressed: () {
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => ThirdScreen()),
      );
    },
  ),
)
Image for post

See y’all!

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment