and so on
This commit is contained in:
@@ -1,52 +1,3 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 工单模块颜色常量
|
||||
class WorkOrderColors {
|
||||
WorkOrderColors._();
|
||||
|
||||
/// 主色调
|
||||
static const Color primary = Color(0xFF165DFF);
|
||||
|
||||
/// 待处理色(橙色)
|
||||
static const Color pending = Color(0xFFFF7D00);
|
||||
|
||||
/// 执行中色(蓝色)
|
||||
static const Color executing = Color(0xFF165DFF);
|
||||
|
||||
/// 已完成色(绿色)
|
||||
static const Color completed = Color(0xFF00B42A);
|
||||
|
||||
/// 背景色
|
||||
static const Color background = Color(0xFFFFFFFF);
|
||||
|
||||
/// 页面背景色
|
||||
static const Color pageBackground = Color(0xFFF7F8FA);
|
||||
|
||||
/// 主要文字色
|
||||
static const Color primaryText = Color(0xFF1D2129);
|
||||
|
||||
/// 次要文字色
|
||||
static const Color secondaryText = Color(0xFF4E5969);
|
||||
|
||||
/// 辅助文字色
|
||||
static const Color auxiliaryText = Color(0xFF86909C);
|
||||
|
||||
/// 分割线颜色
|
||||
static const Color divider = Color(0xFFF0F0F0);
|
||||
|
||||
/// 进度条背景色
|
||||
static const Color progressBackground = Color(0xFFF0F0F0);
|
||||
|
||||
/// 卡片阴影
|
||||
static List<BoxShadow> get cardShadow => [
|
||||
const BoxShadow(
|
||||
color: Color(0x0D000000),
|
||||
blurRadius: 4,
|
||||
offset: Offset(0, 2),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/// 工单模块尺寸常量
|
||||
class WorkOrderDimensions {
|
||||
WorkOrderDimensions._();
|
||||
@@ -71,21 +22,10 @@ class WorkOrderDimensions {
|
||||
class AppConstants {
|
||||
AppConstants._();
|
||||
|
||||
static const Color primaryColor = WorkOrderColors.primary;
|
||||
static const Color backgroundColor = WorkOrderColors.background;
|
||||
static const Color primaryTextColor = WorkOrderColors.primaryText;
|
||||
static const Color secondaryTextColor = WorkOrderColors.secondaryText;
|
||||
static const Color auxiliaryTextColor = WorkOrderColors.auxiliaryText;
|
||||
static const Color pendingColor = WorkOrderColors.pending;
|
||||
static const Color executingColor = WorkOrderColors.executing;
|
||||
static const Color completedColor = WorkOrderColors.completed;
|
||||
|
||||
static const double borderRadius = WorkOrderDimensions.borderRadius;
|
||||
static const double horizontalPadding = WorkOrderDimensions.horizontalPadding;
|
||||
static const double moduleSpacing = WorkOrderDimensions.moduleSpacing;
|
||||
static const double navigationBarHeight = WorkOrderDimensions.navigationBarHeight;
|
||||
|
||||
static List<BoxShadow> get cardShadow => WorkOrderColors.cardShadow;
|
||||
}
|
||||
|
||||
/// 工单状态枚举
|
||||
|
||||
@@ -141,6 +141,7 @@ import '../../features/devices/domain/usecases/recovery_task_usecase.dart';
|
||||
import '../../features/devices/presentation/bloc/device_task_cubit.dart';
|
||||
import '../app/app_user_cubit.dart';
|
||||
import '../localization/locale_cubit.dart';
|
||||
import '../theme/theme_cubit.dart';
|
||||
import '../../features/home/presentation/bloc/permission_request_bloc.dart';
|
||||
import '../network/dio_client.dart';
|
||||
import '../network/net_message_dispatcher.dart';
|
||||
@@ -216,7 +217,8 @@ Future<void> init() async {
|
||||
);
|
||||
|
||||
/// 1.5 --- MQTT Data Sources ---
|
||||
sl.registerFactory<DroneOsdDataSource>(
|
||||
/// 改为 LazySingleton:列表页预启动订阅和详情页共享同一实例,stream 互通
|
||||
sl.registerLazySingleton<DroneOsdDataSource>(
|
||||
() =>
|
||||
DroneOsdDataSourceImpl(sl<MqttClient>(instanceName: 'droneOsdClient')),
|
||||
);
|
||||
@@ -451,6 +453,9 @@ Future<void> init() async {
|
||||
// 🔥 语言管理 Cubit (单例)
|
||||
sl.registerLazySingleton(() => LocaleCubit());
|
||||
|
||||
// 🔥 主题管理 Cubit (单例,管理亮/暗/跟随系统)
|
||||
sl.registerLazySingleton(() => ThemeCubit());
|
||||
|
||||
// Tab 配置 Cubit (单例)
|
||||
sl.registerLazySingleton(() => TabConfigCubit(sl<SharedPreferences>()));
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
|
||||
}) async {
|
||||
if (_isDisposed) return;
|
||||
|
||||
// 同一设备(SN 相同):只增加引用计数,共享订阅
|
||||
if (_listenerCount > 0 &&
|
||||
_deviceSn == deviceSn &&
|
||||
_gatewaySn == gatewaySn &&
|
||||
@@ -55,7 +56,11 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
|
||||
return;
|
||||
}
|
||||
|
||||
// 切换到不同设备:强制清理旧订阅
|
||||
// 不管引用计数多少,SN 变了就必须先取消旧订阅再订阅新的
|
||||
if (_deviceSn != null || _gatewaySn != null) {
|
||||
// 重置引用计数为 1,让 stopListening 能真正取消 MQTT 订阅
|
||||
_listenerCount = 1;
|
||||
await stopListening();
|
||||
}
|
||||
|
||||
@@ -113,15 +118,19 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
|
||||
if (_isDisposed) return;
|
||||
try {
|
||||
final jsonData = jsonDecode(message.payload) as Map<String, dynamic>;
|
||||
final osdData = DroneOsdEntity.fromJson(jsonData);
|
||||
|
||||
if (_gatewaySn != null &&
|
||||
_gatewaySn!.isNotEmpty &&
|
||||
message.topic.contains(_gatewaySn!)) {
|
||||
// 注入来源 SN,供下游解析层验证消息归属
|
||||
jsonData['_sourceSn'] = _gatewaySn;
|
||||
final osdData = DroneOsdEntity.fromJson(jsonData);
|
||||
_stationOsdController.add(osdData);
|
||||
} else if (_deviceSn != null &&
|
||||
_deviceSn!.isNotEmpty &&
|
||||
message.topic.contains(_deviceSn!)) {
|
||||
jsonData['_sourceSn'] = _deviceSn;
|
||||
final osdData = DroneOsdEntity.fromJson(jsonData);
|
||||
_droneOsdController.add(osdData);
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -130,9 +139,28 @@ class DroneOsdDataSourceImpl implements DroneOsdDataSource {
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
if (_isDisposed) return;
|
||||
_isDisposed = true;
|
||||
stopListening();
|
||||
|
||||
// 强制清理:不受引用计数限制,确保 MQTT 订阅被真正取消
|
||||
// 避免切换机场时旧订阅残留导致数据串台
|
||||
_listenerCount = 0;
|
||||
_subscription?.cancel();
|
||||
_subscription = null;
|
||||
|
||||
if (_deviceSn != null && _deviceSn!.isNotEmpty) {
|
||||
final deviceSn = _deviceSn!;
|
||||
mqttClient.unsubscribe('thing/product/$deviceSn/osd');
|
||||
}
|
||||
if (_gatewaySn != null && _gatewaySn!.isNotEmpty) {
|
||||
final gatewaySn = _gatewaySn!;
|
||||
mqttClient.unsubscribe('thing/product/$gatewaySn/osd');
|
||||
}
|
||||
|
||||
_deviceSn = null;
|
||||
_gatewaySn = null;
|
||||
|
||||
_droneOsdController.close();
|
||||
_stationOsdController.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,159 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 应用级语义色扩展(浅色/深色各一套,随主题自动切换)
|
||||
///
|
||||
/// 用法:`context.appColors.cardBackground`
|
||||
/// 原理:颜色挂载在 ThemeData.extensions 上,主题切换时组件树自动重建,
|
||||
/// 页面无需写 `isDark ? A : B` 判断
|
||||
@immutable
|
||||
class AppColors extends ThemeExtension<AppColors> {
|
||||
const AppColors({
|
||||
required this.pageBackground,
|
||||
required this.cardBackground,
|
||||
required this.fillBackground,
|
||||
required this.textPrimary,
|
||||
required this.textSecondary,
|
||||
required this.textTertiary,
|
||||
required this.divider,
|
||||
required this.cardShadow,
|
||||
required this.primary,
|
||||
required this.danger,
|
||||
required this.warning,
|
||||
required this.success,
|
||||
});
|
||||
|
||||
/// 页面背景色
|
||||
final Color pageBackground;
|
||||
|
||||
/// 卡片背景色
|
||||
final Color cardBackground;
|
||||
|
||||
/// 卡片内嵌块填充色(输入框、上传按钮等)
|
||||
final Color fillBackground;
|
||||
|
||||
/// 主要文字色
|
||||
final Color textPrimary;
|
||||
|
||||
/// 次要文字色
|
||||
final Color textSecondary;
|
||||
|
||||
/// 辅助文字色(占位、说明)
|
||||
final Color textTertiary;
|
||||
|
||||
/// 分割线/浅色边框
|
||||
final Color divider;
|
||||
|
||||
/// 卡片阴影色
|
||||
final Color cardShadow;
|
||||
|
||||
/// 品牌主色(两种模式下保持一致)
|
||||
final Color primary;
|
||||
|
||||
/// 危险/错误色(两种模式下保持一致)
|
||||
final Color danger;
|
||||
|
||||
/// 警告色(两种模式下保持一致)
|
||||
final Color warning;
|
||||
|
||||
/// 成功/完成色(两种模式下保持一致)
|
||||
final Color success;
|
||||
|
||||
/// 浅色语义色(取自设计稿)
|
||||
static const AppColors light = AppColors(
|
||||
pageBackground: Color(0xFFF5F7FA),
|
||||
cardBackground: Color(0xFFFFFFFF),
|
||||
fillBackground: Color(0xFFF8F9FA),
|
||||
textPrimary: Color(0xFF1D2129),
|
||||
textSecondary: Color(0xFF4E5969),
|
||||
textTertiary: Color(0xFF86909C),
|
||||
divider: Color(0xFFF0F0F0),
|
||||
cardShadow: Color(0x0D000000),
|
||||
primary: Color(0xFF165DFF),
|
||||
danger: Color(0xFFF53F3F),
|
||||
warning: Color(0xFFFF7D00),
|
||||
success: Color(0xFF00B42A),
|
||||
);
|
||||
|
||||
/// 深色语义色(与 AppTheme.darkTheme 的 surface 色保持一致)
|
||||
static const AppColors dark = AppColors(
|
||||
pageBackground: Color(0xFF1C1C1E),
|
||||
cardBackground: Color(0xFF2C2C2E),
|
||||
fillBackground: Color(0xFF3A3A3C),
|
||||
textPrimary: Color(0xFFF2F3F5),
|
||||
textSecondary: Color(0xFFC9CDD4),
|
||||
textTertiary: Color(0xFF86909C),
|
||||
divider: Color(0xFF3A3A3C),
|
||||
cardShadow: Color(0x00000000), // 深色下无阴影,靠色差分层级
|
||||
primary: Color(0xFF165DFF),
|
||||
danger: Color(0xFFF53F3F),
|
||||
warning: Color(0xFFFF7D00),
|
||||
success: Color(0xFF00B42A),
|
||||
);
|
||||
|
||||
@override
|
||||
AppColors copyWith({
|
||||
Color? pageBackground,
|
||||
Color? cardBackground,
|
||||
Color? fillBackground,
|
||||
Color? textPrimary,
|
||||
Color? textSecondary,
|
||||
Color? textTertiary,
|
||||
Color? divider,
|
||||
Color? cardShadow,
|
||||
Color? primary,
|
||||
Color? danger,
|
||||
Color? warning,
|
||||
Color? success,
|
||||
}) {
|
||||
return AppColors(
|
||||
pageBackground: pageBackground ?? this.pageBackground,
|
||||
cardBackground: cardBackground ?? this.cardBackground,
|
||||
fillBackground: fillBackground ?? this.fillBackground,
|
||||
textPrimary: textPrimary ?? this.textPrimary,
|
||||
textSecondary: textSecondary ?? this.textSecondary,
|
||||
textTertiary: textTertiary ?? this.textTertiary,
|
||||
divider: divider ?? this.divider,
|
||||
cardShadow: cardShadow ?? this.cardShadow,
|
||||
primary: primary ?? this.primary,
|
||||
danger: danger ?? this.danger,
|
||||
warning: warning ?? this.warning,
|
||||
success: success ?? this.success,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
AppColors lerp(AppColors? other, double t) {
|
||||
if (other == null) return this;
|
||||
return AppColors(
|
||||
pageBackground: Color.lerp(pageBackground, other.pageBackground, t)!,
|
||||
cardBackground: Color.lerp(cardBackground, other.cardBackground, t)!,
|
||||
fillBackground: Color.lerp(fillBackground, other.fillBackground, t)!,
|
||||
textPrimary: Color.lerp(textPrimary, other.textPrimary, t)!,
|
||||
textSecondary: Color.lerp(textSecondary, other.textSecondary, t)!,
|
||||
textTertiary: Color.lerp(textTertiary, other.textTertiary, t)!,
|
||||
divider: Color.lerp(divider, other.divider, t)!,
|
||||
cardShadow: Color.lerp(cardShadow, other.cardShadow, t)!,
|
||||
primary: Color.lerp(primary, other.primary, t)!,
|
||||
danger: Color.lerp(danger, other.danger, t)!,
|
||||
warning: Color.lerp(warning, other.warning, t)!,
|
||||
success: Color.lerp(success, other.success, t)!,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 便捷访问扩展:`context.appColors`
|
||||
extension AppColorsX on BuildContext {
|
||||
AppColors get appColors => Theme.of(this).extension<AppColors>()!;
|
||||
}
|
||||
|
||||
class AppTheme {
|
||||
// 基础色值定义
|
||||
static const Color _pureWhite = Color(0xFFFFFFFF);
|
||||
static const Color _offWhite = Color(0xFFfafafc); // 稍微带点灰的白,用于背景增加层次感
|
||||
static const Color _pureBlack = Color(0xFF000000);
|
||||
static const Color _darkGrey = Color(0xFF1C1C1E); // iOS 风格的深灰黑色
|
||||
static const Color _surfaceDark = Color(0xFF2C2C2E); // 深色模式下卡片、弹窗表面色
|
||||
|
||||
static ThemeData get lightTheme {
|
||||
return ThemeData(
|
||||
@@ -15,6 +162,9 @@ class AppTheme {
|
||||
brightness: Brightness.light,
|
||||
fontFamily: (Platform.isWindows) ? 'Microsoft YaHei' : null,
|
||||
|
||||
// 语义色扩展
|
||||
extensions: const [AppColors.light],
|
||||
|
||||
// 1. 整体背景色
|
||||
scaffoldBackgroundColor: _offWhite,
|
||||
|
||||
@@ -85,4 +235,84 @@ class AppTheme {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 深色主题 (与浅色主题对称的黑白简约风格)
|
||||
static ThemeData get darkTheme {
|
||||
return ThemeData(
|
||||
useMaterial3: true,
|
||||
brightness: Brightness.dark,
|
||||
fontFamily: (Platform.isWindows) ? 'Microsoft YaHei' : null,
|
||||
|
||||
// 语义色扩展
|
||||
extensions: const [AppColors.dark],
|
||||
|
||||
// 1. 整体背景色 (iOS 风格深灰黑)
|
||||
scaffoldBackgroundColor: _darkGrey,
|
||||
|
||||
// 2. 核心颜色方案 (与浅色主题反色:主色白色)
|
||||
colorScheme: const ColorScheme.dark(
|
||||
primary: _pureWhite, // 主色设为白色
|
||||
onPrimary: _pureBlack, // 白色背景上的文字设为黑色
|
||||
surface: _surfaceDark, // 卡片、弹窗等表面颜色
|
||||
onSurface: _pureWhite, // 表面上的文字色
|
||||
),
|
||||
|
||||
// 3. 全局按钮主题 (白底黑字)
|
||||
elevatedButtonTheme: ElevatedButtonThemeData(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: _pureWhite,
|
||||
foregroundColor: _pureBlack,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
|
||||
),
|
||||
),
|
||||
|
||||
// 4. 填充按钮主题 (常用语 Material 3)
|
||||
filledButtonTheme: FilledButtonThemeData(
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: _pureWhite,
|
||||
foregroundColor: _pureBlack,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 5. AppBar 主题 (深灰黑背景,白色文字)
|
||||
appBarTheme: const AppBarTheme(
|
||||
backgroundColor: _darkGrey,
|
||||
surfaceTintColor: Colors.transparent,
|
||||
elevation: 0,
|
||||
centerTitle: true,
|
||||
iconTheme: IconThemeData(color: _pureWhite),
|
||||
titleTextStyle: TextStyle(
|
||||
color: _pureWhite,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w700,
|
||||
letterSpacing: -0.5,
|
||||
),
|
||||
),
|
||||
|
||||
// 6. 输入框主题 (深色填充,白色边框)
|
||||
inputDecorationTheme: InputDecorationTheme(
|
||||
filled: true,
|
||||
fillColor: _surfaceDark,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: const BorderSide(color: Colors.white12),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: const BorderSide(color: Colors.white12),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: const BorderSide(color: _pureWhite, width: 1.5),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
54
lib/core/theme/theme_cubit.dart
Normal file
54
lib/core/theme/theme_cubit.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// 全局主题管理 Cubit(仿 LocaleCubit 模式)
|
||||
/// 支持 light / dark / system 三态,持久化到 SharedPreferences
|
||||
class ThemeCubit extends Cubit<ThemeMode> {
|
||||
static const String _themeKey = 'app_theme_mode';
|
||||
|
||||
ThemeCubit() : super(ThemeMode.light) {
|
||||
_loadSavedThemeMode();
|
||||
}
|
||||
|
||||
/// 从本地存储恢复上次保存的主题模式
|
||||
Future<void> _loadSavedThemeMode() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final saved = prefs.getString(_themeKey);
|
||||
emit(_parseThemeMode(saved));
|
||||
} catch (e) {
|
||||
emit(ThemeMode.light);
|
||||
}
|
||||
}
|
||||
|
||||
/// 设置主题模式并持久化
|
||||
Future<void> setThemeMode(ThemeMode mode) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_themeKey, mode.name);
|
||||
emit(mode);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [ThemeCubit] 保存主题设置失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 夜间模式开关(Switch 二态切换,不涉及跟随系统)
|
||||
Future<void> toggleDarkMode(bool enabled) =>
|
||||
setThemeMode(enabled ? ThemeMode.dark : ThemeMode.light);
|
||||
|
||||
/// 当前是否为深色模式
|
||||
bool get isDarkMode => state == ThemeMode.dark;
|
||||
|
||||
/// 将持久化的字符串解析为 ThemeMode
|
||||
ThemeMode _parseThemeMode(String? saved) {
|
||||
switch (saved) {
|
||||
case 'dark':
|
||||
return ThemeMode.dark;
|
||||
case 'system':
|
||||
return ThemeMode.system;
|
||||
default:
|
||||
return ThemeMode.light;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user