Contents
Outline
Let’s see how to go back to the previous screen using the BackButton
widget provided by Flutter.
Back button of AppBar
Until now, I have implemented the back button of AppBar
as follows.
AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back_ios_new),
splashRadius: 20.0,
onPressed: () => Get.back(),
),
...
),
In my case, I developed the app using GetX
, so I implemented the screen back using Get.back()
.
BacckButton widget
However, in Flutter, you can implement the back button more simply using the BackButton
widget provided by default.
AppBar(
leading: const BackButton(),
...
),
- Official document: https://api.flutter.dev/flutter/material/BackButton-class.html
AppBar’s automaticallyImplyLeading
You can automatically show or hide the BackButton
using the automaticallyImplyLeading
property of AppBar
. The default value of automaticallyImplyLeading
of AppBar
is true
, so even if you don’t use BackButton
, the back button is displayed on AppBar
automatically.
AppBar(
...
),
Utilization of BackButton
The back button is displayed basically if the automaticallyImplyLeading
property of AppBar
is not set to false
. So, we don’t need to use BackButton
intentionally.
However, if you need to call a specific function when you go back to the previous screen, you can implement it using BackButton
as follows.
AppBar(
leading: BackButton(
onPressed: () {
callback();
Get.back();
},
),
...
),
Completed
Done! We’ve seen how to use the BackButton
widget provided by Flutter to go back to the previous screen. If you didn’t know about the BackButton
widget that Flutter provides by default and have been implementing it using IconButton
like me, you can implement it more simply using BackButton
.
Also, don’t forget that if you use the BackButton
that doesn’t do anything in the leading
of AppBar
, the back button is automatically displayed using the automaticallyImplyLeading
option of AppBar
.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
App promotion
Deku
.Deku
created the applications with Flutter.If you have interested, please try to download them for free.