feat(profile): 实现个人资料编辑与策略本地缓存

- 将资料编辑页重构为可交互表单,支持昵称、签名编辑与头像更换
- 新增 DioClient 本地缓存读取与写入方法,支持离线读取策略快照
- 设备策略切换后自动使本地缓存失效,避免数据不一致
- Home Tab 切换时选中态跟随可见页面,滑动过程中实时更新底栏高亮
- 系统设置页引入 loading 遮罩层,防止切换开关时重复操作
- 个人资料页接入 profileController 展示真实用户信息与加载状态
- 适配深色模式,修正 home_tab 中头像与文字颜色硬编码问题
This commit is contained in:
tongtong committed 2026-11-09 09:24:51 +08:00
1 parent 5f84a64c3e
commit e06e12fbbc
30 files changed
+2059 -336

No files matched your search

+27 -8
View File
@@ -24,9 +24,19 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
final _dragPage = ValueNotifier<double?>(null);
final _visibleIndex = ValueNotifier<int?>(null);
int? _tabPointer;
Offset _tabPointerStart = Offset.zero;
@override
void didUpdateWidget(covariant HomePage oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.navigationShell.currentIndex !=
oldWidget.navigationShell.currentIndex) {
_visibleIndex.value = null;
}
}
void _finishTabDrag(PointerEvent event) {
if (event.pointer != _tabPointer) return;
_tabPointer = null;
@@ -36,6 +46,7 @@ class _HomePageState extends State<HomePage> {
@override
void dispose() {
_dragPage.dispose();
_visibleIndex.dispose();
super.dispose();
}
@@ -75,7 +86,11 @@ class _HomePageState extends State<HomePage> {
resizeToAvoidBottomInset: false,
child: Stack(
children: [
HomeTabDragScope(page: _dragPage, child: navigationShell),
HomeTabDragScope(
page: _dragPage,
visibleIndex: _visibleIndex,
child: navigationShell,
),
Align(
alignment: Alignment.bottomCenter,
child: SafeArea(
@@ -118,13 +133,17 @@ class _HomePageState extends State<HomePage> {
// GlassTabBar 仍读取 Flutter SDK 的 Cupertino 主题。
// ignore: deprecated_member_use
child: CupertinoUiCompatibilityBridge(
child: GlassTabBar.bottom(
selectedIndex: index,
onTabSelected: navigationShell.goBranch,
horizontalPadding: tabBarMargin,
verticalPadding: 0,
iconSize: 26,
tabs: tabs,
child: ValueListenableBuilder<int?>(
valueListenable: _visibleIndex,
builder: (context, visibleIndex, _) =>
GlassTabBar.bottom(
selectedIndex: visibleIndex ?? index,
onTabSelected: navigationShell.goBranch,
horizontalPadding: tabBarMargin,
verticalPadding: 0,
iconSize: 26,
tabs: tabs,
),
),
),
);
+18 -3
View File
@@ -239,8 +239,10 @@ class HomeTab extends ConsumerWidget {
Container(
width: 64,
height: 64,
decoration: const BoxDecoration(
color: CupertinoColors.white, shape: BoxShape.circle),
decoration: BoxDecoration(
color: DarkMode.color(context, AppColors.card),
shape: BoxShape.circle,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
@@ -274,7 +276,20 @@ class HomeTab extends ConsumerWidget {
padding: const EdgeInsets.only(bottom: 8),
child: Row(
children: [
SizedBox(width: 42, child: Text(name, style: const TextStyle(fontSize: 12, color: Color(0xFF4B5563)))),
SizedBox(
width: 42,
child: Text(
name,
style: TextStyle(
fontSize: 12,
color: DarkMode.tone(
context,
const Color(0xFF4B5563),
DarkMode.color(context, AppColors.ink),
),
),
),
),
Expanded(
child: Container(
height: 7,
@@ -4,12 +4,19 @@ import 'package:go_router/go_router.dart';
/// Fractional page selected by the bottom bar while a pointer is dragging.
class HomeTabDragScope extends InheritedWidget {
const HomeTabDragScope({required this.page, required super.child, super.key});
const HomeTabDragScope({
required this.page,
required this.visibleIndex,
required super.child,
super.key,
});
final ValueNotifier<double?> page;
final ValueNotifier<int?> visibleIndex;
@override
bool updateShouldNotify(HomeTabDragScope oldWidget) => page != oldWidget.page;
bool updateShouldNotify(HomeTabDragScope oldWidget) =>
page != oldWidget.page || visibleIndex != oldWidget.visibleIndex;
}
/// Displays the shell's existing branch navigators as swipeable pages.
@@ -34,13 +41,15 @@ class _HomeTabPagerState extends State<HomeTabPager> {
);
ValueNotifier<double?>? _dragPage;
Drag? _tabDrag;
ValueNotifier<int?>? _visibleIndex;
@override
void didChangeDependencies() {
super.didChangeDependencies();
final page = context
.dependOnInheritedWidgetOfExactType<HomeTabDragScope>()
?.page;
final scope = context
.dependOnInheritedWidgetOfExactType<HomeTabDragScope>();
_visibleIndex = scope?.visibleIndex;
final page = scope?.page;
if (page == _dragPage) return;
_dragPage?.removeListener(_updateTabDrag);
_dragPage = page;
@@ -81,7 +90,9 @@ class _HomeTabPagerState extends State<HomeTabPager> {
void didUpdateWidget(covariant HomeTabPager oldWidget) {
super.didUpdateWidget(oldWidget);
final index = widget.navigationShell.currentIndex;
if (index != oldWidget.navigationShell.currentIndex) _animateToIndex(index);
if (index != oldWidget.navigationShell.currentIndex) {
_animateToIndex(index);
}
}
void _animateToIndex(int index) {
@@ -118,6 +129,9 @@ class _HomeTabPagerState extends State<HomeTabPager> {
},
child: PageView(
controller: _controller,
// Update the bar at the page midpoint, without waiting for settling
// or changing the route while the user's gesture is still active.
onPageChanged: (index) => _visibleIndex?.value = index,
children: [
for (final (index, child) in widget.children.indexed)
_KeepAlivePage(
@@ -0,0 +1,72 @@
import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/network/api_exception.dart';
import '../../device/data/device_providers.dart';
import '../../device/domain/device_models.dart';
// Keep this declaration explicit, as in device_providers.dart: the pinned
// Riverpod generator cannot parse the current Flutter SDK's dot shorthand.
final systemSettingsControllerProvider = NotifierProvider.autoDispose
.family<SystemSettingsController, AsyncValue<DevicePolicy?>, String>(
SystemSettingsController.new,
);
/// 页面级策略状态:先展示缓存,请求期间保留数据并锁定操作。
class SystemSettingsController
extends AutoDisposeFamilyNotifier<AsyncValue<DevicePolicy?>, String> {
late String _sn;
bool _active = true;
@override
AsyncValue<DevicePolicy?> build(String sn) {
_sn = sn;
_active = true;
ref.onDispose(() => _active = false);
unawaited(Future<void>.microtask(_load));
return const AsyncLoading();
}
Future<void> _load() async {
final repo = ref.read(deviceRepositoryProvider);
try {
final cached = await repo.getCachedPolicy(_sn);
if (!_active) return;
if (cached != null) {
state = const AsyncLoading<DevicePolicy?>().copyWithPrevious(
AsyncData(cached),
);
}
final policy = await repo.getPolicy(_sn);
if (_active) state = AsyncData(policy);
} catch (error, stack) {
if (_active) state = AsyncError(error, stack);
}
}
Future<void> toggle(String field, bool enabled) async {
if (state.isLoading || state.valueOrNull == null) return;
final previous = state;
state = const AsyncLoading<DevicePolicy?>().copyWithPrevious(previous);
try {
final repo = ref.read(deviceRepositoryProvider);
final error = await repo.togglePolicy(_sn, field, enabled ? 1 : 0);
if (!_active) return;
if (error != null) {
throw ApiException(code: 'policy_toggle', message: error);
}
final policy = await repo.getPolicy(_sn);
if (!_active) return;
state = AsyncData(policy);
ref.invalidate(devicePolicyProvider(_sn));
} catch (error, stack) {
if (_active) {
state = AsyncError<DevicePolicy?>(
error,
stack,
).copyWithPrevious(previous);
}
}
}
}
@@ -6,6 +6,8 @@ import 'package:ttstd_family_care/app/theme/dark_mode.dart';
import 'package:ttstd_family_care/core/widgets/app_page_scaffold.dart';
import '../../../core/widgets/feature_ui.dart';
import '../../../core/network/error_message.dart';
import 'system_settings_controller.dart';
import '../../../features/device/data/device_providers.dart';
import '../../../features/device/domain/device_models.dart';
import '../../../features/device/presentation/device_switcher.dart';
@@ -28,46 +30,67 @@ class SystemSettingsPage extends ConsumerWidget {
final sn = ref.watch(selectedDeviceSnProvider);
final policyAsync = sn == null
? const AsyncValue<DevicePolicy?>.data(null)
: ref.watch(devicePolicyProvider(sn));
: ref.watch(systemSettingsControllerProvider(sn));
final policy = policyAsync.valueOrNull;
final busy = policyAsync.isLoading;
if (sn != null) {
ref.listen(systemSettingsControllerProvider(sn), (previous, next) {
if (next.hasError && previous?.error != next.error) {
_showToast(context, userMessageOf(next.error!));
}
});
}
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, _) {
if (didPop) return;
if (didPop || busy) return;
if (context.canPop()) {
context.pop();
} else {
context.go('/home');
}
},
child: AppPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: DeviceContextTitle(l10n.ssTitle),
leading: CupertinoButton(
padding: EdgeInsets.zero,
child: const Icon(CupertinoIcons.back),
onPressed: () => context.pop(),
),
),
child: SafeArea(
child: policyAsync.when(
loading: () => const Center(child: CupertinoActivityIndicator()),
error: (e, _) => Center(
child: Text(l10n.commonLoadFailed,
style: TextStyle(color: DarkMode.color(context, AppColors.sub))),
child: Stack(
children: [
AppPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: DeviceContextTitle(l10n.ssTitle),
leading: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: busy ? null : () => context.pop(),
child: const Icon(CupertinoIcons.back),
),
),
child: SafeArea(
child: policy != null
? _buildGroups(context, ref, l10n, sn, policy)
: Center(
child: busy
? const SizedBox.shrink()
: Text(
l10n.ssLoadFailed,
style: TextStyle(
color: DarkMode.color(context, AppColors.sub),
),
),
),
),
data: (data) {
if (data == null) {
return Center(
child: Text(l10n.ssLoadFailed,
style: TextStyle(color: DarkMode.color(context, AppColors.sub))),
);
}
return _buildGroups(context, ref, l10n, sn, data);
},
),
),
if (busy) ...[
const Positioned.fill(
child: ModalBarrier(dismissible: false, color: Color(0x55000000)),
),
const Center(
child: CupertinoPopupSurface(
child: Padding(
padding: EdgeInsets.all(28),
child: CupertinoActivityIndicator(radius: 14),
),
),
),
],
],
),
);
}
@@ -181,54 +204,39 @@ class SystemSettingsPage extends ConsumerWidget {
Expanded(
child: Text(
item.label,
style: TextStyle(fontSize: 14.5, color: DarkMode.color(context, AppColors.ink)),
style: TextStyle(
fontSize: 14.5,
color: DarkMode.color(context, AppColors.ink),
),
),
),
CupertinoSwitch(
value: enabled,
activeColor: AppColors.green,
onChanged: (value) =>
_onToggle(context, ref, l10n, sn, item.field, value ? 1 : 0),
activeTrackColor: AppColors.green,
onChanged:
sn == null ||
ref.watch(systemSettingsControllerProvider(sn)).isLoading
? null
: (value) => ref
.read(systemSettingsControllerProvider(sn).notifier)
.toggle(item.field, value),
),
],
),
);
}
/// 调用 toggle 接口并刷新策略状态。
Future<void> _onToggle(
BuildContext context,
WidgetRef ref,
AppLocalizations l10n,
String? sn,
String field,
int value,
) async {
if (sn == null || sn.isEmpty) {
_showToast(context, l10n.opCmdFailed);
return;
}
final error = await ref
.read(deviceRepositoryProvider)
.togglePolicy(sn, field, value);
if (!context.mounted) return;
if (error == null) {
// 成功后仅刷新该策略数据,保证开关状态与后台一致。
ref.invalidate(devicePolicyProvider(sn));
} else {
_showToast(context, error);
}
}
/// 轻提示:以 SnackBar 展示结果,避免连续切换时被弹窗打断。
void _showToast(BuildContext context, String message) {
ScaffoldMessenger.of(context)
..clearSnackBars()
..showSnackBar(SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
behavior: SnackBarBehavior.floating,
));
..showSnackBar(
SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
behavior: SnackBarBehavior.floating,
),
);
}
}