Flutter开发:对于状态栏、导航栏的沉浸式处理

开发语言:Dart
开发生态:Flutter

准备工作

方案需要使用到插件device_info_plus: ^latest_version,在pubspec.yaml内添加此插件

dependencies:
  device_info_plus: $last_version

实现方法

  /// 对于状态栏、导航栏的整体处理
  static Future redoSystemStyle() async {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    ));

    if (Platform.isAndroid) {
      var darkMode = window.platformBrightness == Brightness.dark;

      final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
      final AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
      final bool edgeToEdge = androidInfo.version.sdkInt != null &&
          androidInfo.version.sdkInt! >= 29;

      SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);

      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        systemNavigationBarColor: edgeToEdge
            ? Colors.transparent
            : darkMode
                ? Colors.black
                : Colors.white,
        systemNavigationBarContrastEnforced: true,
        systemNavigationBarIconBrightness:
            darkMode ? Brightness.light : Brightness.dark,
      ));
    }
  }

小白的进阶之路,感谢大家的支持!

你可能感兴趣的:(Flutter开发:对于状态栏、导航栏的沉浸式处理)