How can I hide the AppBar back button based on the platform?

432 views Asked by At

I am hoping to hide all AppBar back buttons throughout my app for Android users, so that they must use the device's back button instead. But for iOS users, I would like for the AppBar's back button to be present since that is the standard way to navigate.

What is the best way to achieve this and is there a way to do it globally using AppBarTheme?

I am aware of AppBar(automaticallyImplyLeading: false, ...) but that hides the back button on both iOS and Android devices.

2

There are 2 answers

0
Randal Schwartz On BEST ANSWER

Set that value based on the platform. You can get the platform with https://pub.dev/packages/platform, like so:

AppBar(automaticallyImplyLeading: !Platform.isAndroid, ...)

which will make it false on Android, true everywhere else. You might need to add other platforms if you find it necessary.

2
creativecreatorormaybenot On

You can simply hide it conditionally:

import 'dart:io';

AppBar(
  automaticallyImplyLeading: !Platform.isAndroid,
  ...,
)

This will only hide the back button on Android.


Learn more about Platform.isAndroid.