Skip to content

Commit

Permalink
Android导航栏颜色优化
Browse files Browse the repository at this point in the history
  • Loading branch information
simplezhli committed Feb 13, 2024
1 parent 9f0e812 commit 5296982
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 60 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log:

## 1.3.2

* Android导航栏颜色优化。

## 1.3.1

* Flutter SDK升至3.16.5。
Expand Down
2 changes: 1 addition & 1 deletion ios/Flutter/Flutter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
s.license = { :type => 'BSD' }
s.author = { 'Flutter Dev Team' => '[email protected]' }
s.source = { :git => 'https://github.com/flutter/engine', :tag => s.version.to_s }
s.ios.deployment_target = '11.0'
s.ios.deployment_target = '12.0'
# Framework linking is handled by Flutter tooling, not CocoaPods.
# Add a placeholder to satisfy `s.dependency 'Flutter'` plugin podspecs.
s.vendored_frameworks = 'path/to/nothing'
Expand Down
6 changes: 3 additions & 3 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@
"$(PROJECT_DIR)/Flutter",
);
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -532,7 +532,7 @@
"$(PROJECT_DIR)/Flutter",
);
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -567,7 +567,7 @@
"$(PROJECT_DIR)/Flutter",
);
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
11 changes: 2 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:flutter_deer/setting/provider/theme_provider.dart';
import 'package:flutter_deer/util/device_utils.dart';
import 'package:flutter_deer/util/handle_error_utils.dart';
import 'package:flutter_deer/util/log_utils.dart';
import 'package:flutter_deer/util/theme_utils.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -67,8 +66,8 @@ Future<void> main() async {
runApp(MyApp());
});

/// 隐藏状态栏。为启动页、引导页设置。完成后修改回显示状态栏
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom]);
/// 隐藏状态栏,导航栏。为启动页、引导页设置全屏显示。完成后还原
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
// TODO(weilu): 启动体验不佳。状态栏、导航栏在冷启动开始的一瞬间为黑色,且无法通过隐藏、修改颜色等方式进行处理。。。
// 相关问题跟踪:https://github.com/flutter/flutter/issues/73351
}
Expand Down Expand Up @@ -175,12 +174,6 @@ class MyApp extends StatelessWidget {
locale: localeProvider.locale,
navigatorKey: navigatorKey,
builder: (BuildContext context, Widget? child) {
/// 仅针对安卓
if (Device.isAndroid) {
/// 切换深色模式会触发此方法,这里设置导航栏颜色
ThemeUtils.setSystemNavigationBar(provider.getThemeMode());
}

/// 保证文字大小不受手机系统设置影响 https://www.kikt.top/posts/flutter/layout/dynamic-text/
return MediaQuery(
data: MediaQuery.of(context).copyWith(textScaler: TextScaler.noScaling),
Expand Down
3 changes: 1 addition & 2 deletions lib/order/page/order_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_deer/order/page/order_list_page.dart';
import 'package:flutter_deer/order/provider/order_page_provider.dart';
import 'package:flutter_deer/res/resources.dart';
Expand Down Expand Up @@ -124,7 +123,7 @@ class _OrderPageState extends State<OrderPage> with AutomaticKeepAliveClientMixi
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
systemOverlayStyle: SystemUiOverlayStyle.light,
systemOverlayStyle: isDark ? ThemeUtils.light : ThemeUtils.dark,
actions: <Widget>[
IconButton(
onPressed: () {
Expand Down
5 changes: 3 additions & 2 deletions lib/setting/provider/theme_provider.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/routers/web_page_transitions.dart';
import 'package:sp_util/sp_util.dart';

import '../../util/theme_utils.dart';

extension ThemeModeExtension on ThemeMode {
String get value => <String>['System', 'Light', 'Dark'][index];
}
Expand Down Expand Up @@ -73,7 +74,7 @@ class ThemeProvider extends ChangeNotifier {
appBarTheme: AppBarTheme(
elevation: 0.0,
color: isDarkMode ? Colours.dark_bg_color : Colors.white,
systemOverlayStyle: isDarkMode ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
systemOverlayStyle: isDarkMode ? ThemeUtils.light : ThemeUtils.dark,
),
dividerTheme: DividerThemeData(
color: isDarkMode ? Colours.dark_line : Colours.line,
Expand Down
3 changes: 1 addition & 2 deletions lib/statistics/page/statistics_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_deer/order/page/order_page.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
Expand Down Expand Up @@ -40,7 +39,7 @@ class _StatisticsPageState extends State<StatisticsPage> {
isDark = context.isDark;
return <Widget>[
SliverAppBar(
systemOverlayStyle: SystemUiOverlayStyle.light,
systemOverlayStyle: isDark ? ThemeUtils.light : ThemeUtils.dark,
backgroundColor: Colors.transparent,
elevation: 0.0,
centerTitle: true,
Expand Down
51 changes: 15 additions & 36 deletions lib/util/theme_utils.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/util/device_utils.dart';
import 'package:rxdart/rxdart.dart';

class ThemeUtils {

Expand Down Expand Up @@ -33,37 +28,21 @@ class ThemeUtils {
return isDark(context) ? Colours.dark_bg_color : Colors.grey[200];
}

static StreamSubscription<dynamic>? _subscription;

/// 设置NavigationBar样式,使得导航栏颜色与深色模式的设置相符。
static void setSystemNavigationBar(ThemeMode mode) {
/// 主题切换动画(AnimatedTheme)时间为200毫秒,延时设置导航栏颜色,这样过渡相对自然。
_subscription?.cancel();
_subscription = Stream.value(1).delay(const Duration(milliseconds: 200)).listen((_) {
bool isDark = false;
if (mode == ThemeMode.dark || (mode == ThemeMode.system && PlatformDispatcher.instance.platformBrightness == Brightness.dark)) {
isDark = true;
}
setSystemBarStyle(isDark: isDark);
});
}

/// 设置StatusBar、NavigationBar样式。(仅针对安卓)
/// 本项目在android MainActivity中已设置,不需要覆盖设置。
static void setSystemBarStyle({bool? isDark}) {
if (Device.isAndroid) {

final bool isDarkMode = isDark ?? PlatformDispatcher.instance.platformBrightness == Brightness.dark;
debugPrint('isDark: $isDarkMode');
final SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
/// 透明状态栏
statusBarColor: Colors.transparent,
systemNavigationBarColor: isDarkMode ? Colours.dark_bg_color : Colors.white,
systemNavigationBarIconBrightness: isDarkMode ? Brightness.light : Brightness.dark,
);
SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}
}
static const SystemUiOverlayStyle light = SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
systemNavigationBarColor: Colours.dark_bg_color,
systemNavigationBarIconBrightness: Brightness.light,
statusBarIconBrightness: Brightness.light,
statusBarBrightness: Brightness.dark,
);

static const SystemUiOverlayStyle dark = SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
systemNavigationBarColor: Colors.white,
systemNavigationBarIconBrightness: Brightness.dark,
statusBarIconBrightness: Brightness.dark,
statusBarBrightness: Brightness.light,
);
}

extension ThemeExtension on BuildContext {
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/my_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
final Color bgColor = backgroundColor ?? context.backgroundColor;

final SystemUiOverlayStyle overlayStyle = ThemeData.estimateBrightnessForColor(bgColor) == Brightness.dark
? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
? ThemeUtils.light : ThemeUtils.dark;

final Widget action = actionName.isNotEmpty ? Positioned(
right: 0.0,
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/my_search_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class _MySearchBarState extends State<MySearchBar> {
);

return AnnotatedRegion<SystemUiOverlayStyle>(
value: isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
value: isDark ? ThemeUtils.light : ThemeUtils.dark,
child: Material(
color: context.backgroundColor,
child: SafeArea(
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_deer
description: Flutter Deer

version: 1.3.1+22
version: 1.3.2+22
# 唯鹿 <[email protected]>
homepage: https://weilu.blog.csdn.net/

Expand Down Expand Up @@ -40,7 +40,7 @@ dependencies:
# 侧滑删除 https://github.com/letsar/flutter_slidable
flutter_slidable: ^3.0.1
# WebView插件 https://github.com/flutter/packages/tree/main/packages/webview_flutter
webview_flutter: 4.4.1
webview_flutter: 4.7.0
# 处理键盘事件 https://github.com/diegoveloper/flutter_keyboard_actions
keyboard_actions: ^4.2.0
# 城市选择列表 https://github.com/flutterchina/azlistview
Expand Down Expand Up @@ -76,7 +76,7 @@ dependencies:
# 刮刮卡 https://github.com/vintage/scratcher
scratcher: ^2.5.0
# 动画效果 https://github.com/xvrh/lottie-flutter
lottie: ^2.7.0
lottie: ^3.0.0

# https://github.com/simplezhli/flutter_deer/issues/187
dependency_overrides:
Expand Down

0 comments on commit 5296982

Please sign in to comment.