Files
xianyan/lib/features/widget/providers/widget_provider.dart
Developer 85d856f0ed chore: 完成多模块迭代优化与依赖更新
本次提交包含多项更新:
1. 更新file_picker依赖到11.0.0-ohos.1版本
2. 清理SecureStorage、Catcher2配置冗余代码
3. 优化鸿蒙系统下HomeWidget调用方式
4. 重构编辑器导航栏图标与页面路由引用
5. 修复边框样式、简化空值判断逻辑
6. 移除冗余系统UI样式配置
7. 新增共享组件导出与自适应返回按钮
8. 批量替换路由引用为app_routes
9. 标记过时通知服务并补充注释
10. 新增引导页扫一扫功能卡片
11. 完善沉浸式状态栏配置逻辑
12. 为大量页面添加统一自适应返回按钮
2026-05-22 23:14:38 +08:00

150 lines
4.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// ============================================================
/// 闲言APP — 桌面小部件状态管理
/// 创建时间: 2026-05-19
/// 更新时间: 2026-05-19
/// 作用: 管理小部件安装状态、数据推送和交互
/// 上次更新: requestPinWidget返回bool修复Android/鸿蒙端添加小部件无反应
/// ============================================================
import 'dart:ui';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:home_widget/home_widget.dart';
import 'package:xianyan/core/utils/platform_utils.dart' as pu;
import '../../../core/services/data/home_widget_service.dart';
import '../../../core/utils/logger.dart';
import '../models/widget_type.dart';
class WidgetState {
final Set<WidgetType> installedWidgets;
final bool isLoading;
final String? error;
const WidgetState({
this.installedWidgets = const {},
this.isLoading = false,
this.error,
});
WidgetState copyWith({
Set<WidgetType>? installedWidgets,
bool? isLoading,
String? error,
}) => WidgetState(
installedWidgets: installedWidgets ?? this.installedWidgets,
isLoading: isLoading ?? this.isLoading,
error: error,
);
}
class WidgetNotifier extends Notifier<WidgetState> {
@override
WidgetState build() => const WidgetState();
Future<void> loadInstalledWidgets() async {
state = state.copyWith(isLoading: true);
try {
final widgets = await HomeWidget.getInstalledWidgets();
final installed = <WidgetType>{};
for (final w in widgets) {
final kind = w.iOSKind ?? w.androidClassName ?? '';
for (final type in WidgetType.values) {
if (kind.contains(type.androidProviderName) ||
kind.contains(type.iosWidgetKind) ||
kind.contains(type.ohosFormName)) {
installed.add(type);
}
}
}
state = state.copyWith(installedWidgets: installed, isLoading: false);
Log.i('WidgetNotifier: 已安装 ${installed.length} 个小部件');
} catch (e) {
Log.e('WidgetNotifier: 加载小部件失败', e);
state = state.copyWith(isLoading: false, error: e.toString());
}
}
Future<void> addWidget(WidgetType type) async {
try {
final service = HomeWidgetService.instance;
await service.init();
await service.updateWidget(type);
final isDark =
PlatformDispatcher.instance.platformBrightness == Brightness.dark;
await service.pushThemeMode(isDark);
final newInstalled = {...state.installedWidgets, type};
state = state.copyWith(installedWidgets: newInstalled);
Log.i('WidgetNotifier: 添加小部件 ${type.title}');
} catch (e) {
Log.e('WidgetNotifier: 添加小部件失败', e);
state = state.copyWith(error: e.toString());
}
}
Future<void> removeWidget(WidgetType type) async {
final newInstalled = {...state.installedWidgets}..remove(type);
state = state.copyWith(installedWidgets: newInstalled);
Log.i('WidgetNotifier: 移除小部件 ${type.title}');
}
Future<bool> requestPinWidget(WidgetType type) async {
try {
await HomeWidget.requestPinWidget(
qualifiedAndroidName: type.qualifiedAndroidName,
androidName: type.androidProviderName,
);
// 鸿蒙端:本地包版本的 HomeWidget.requestPinWidget 有 ohosName 参数
// 官方SDK下不支持使用 dynamic 调用绕过编译检查
if (pu.isOhos) {
try {
const dynamic requestPin = HomeWidget.requestPinWidget;
await requestPin(
qualifiedAndroidName: type.qualifiedAndroidName,
androidName: type.androidProviderName,
ohosName: type.ohosFormName,
);
} catch (_) {}
}
Log.i('WidgetNotifier: 请求固定小部件 ${type.title}');
return true;
} catch (e) {
Log.e('WidgetNotifier: 请求固定小部件失败', e);
return false;
}
}
Future<void> pushDataToWidget(WidgetType type) async {
try {
final service = HomeWidgetService.instance;
await service.init();
await service.updateWidget(type);
Log.i('WidgetNotifier: 推送数据到小部件 ${type.title}');
} catch (e) {
Log.e('WidgetNotifier: 推送数据失败', e);
}
}
Future<void> pushThemeToAllWidgets() async {
try {
final service = HomeWidgetService.instance;
await service.init();
final isDark =
PlatformDispatcher.instance.platformBrightness == Brightness.dark;
await service.pushThemeMode(isDark);
for (final type in WidgetType.values) {
await service.updateWidget(type);
}
Log.i('WidgetNotifier: 已推送主题到所有小部件');
} catch (e) {
Log.e('WidgetNotifier: 推送主题失败', e);
}
}
}
final widgetProvider = NotifierProvider<WidgetNotifier, WidgetState>(
WidgetNotifier.new,
);