如何根据平台隐藏AppBar后退按钮?

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

我希望为 Android 用户在整个应用程序中隐藏所有 AppBar 后退按钮,以便他们必须使用设备的后退按钮。但是对于 iOS 用户,我希望 AppBar 的后退按钮存在,因为这是标准的导航方式。

实现此目标的最佳方法是什么?有没有办法使用 AppBarTheme 在全球范围内做到这一点?

我知道 AppBar(automaticallyImplyLeading: false, ...) 但它隐藏了 iOS 和 Android 设备上的后退按钮。

您可以简单地有条件地隐藏它:

import 'dart:io';

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

这将 仅在 Android. 上隐藏后退按钮。


Learn more about Platform.isAndroid.

根据平台设置该值。您可以使用 https://pub.dev/packages/platform 获取平台,例如:

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

这将使它在 Android 上为假,在其他任何地方都为真。如果您觉得有必要,您可能需要添加其他平台。