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

184 lines
6.7 KiB
Dart

import 'package:cupertino_ui/cupertino_ui.dart';
import 'package:flutter/gestures.dart' show kTouchSlop;
import 'package:go_router/go_router.dart';
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart'
show GlassTab, GlassTabBar;
import 'package:ttstd_family_care/core/widgets/app_page_scaffold.dart';
import '../../../core/utils/system_ui_util.dart';
import '../../../l10n/app_localizations.dart';
import 'home_tab_pager.dart';
/// 首页 Shell(含底部 Tab:首页 / 管理 / 我的)。
///
/// Tab 切换通过 GoRouter 的 StatefulShellRoute 管理,本组件仅渲染当前分支与底部栏。
/// 实际 Tab 内容由对应特性模块(home / messages / profile)提供。
class HomePage extends StatefulWidget {
const HomePage({required this.navigationShell, super.key});
final StatefulNavigationShell navigationShell;
@override
State<HomePage> createState() => _HomePageState();
}
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;
_dragPage.value = null;
}
@override
void dispose() {
_dragPage.dispose();
_visibleIndex.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
const tabBarMargin = 20.0;
final l10n = AppLocalizations.of(context);
final navigationShell = widget.navigationShell;
final index = navigationShell.currentIndex;
final tabs = [
GlassTab(icon: const Icon(CupertinoIcons.home), label: l10n.tabHome),
GlassTab(
icon: const Icon(CupertinoIcons.square_grid_2x2),
label: l10n.tabManage,
),
GlassTab(icon: const Icon(CupertinoIcons.person), label: l10n.tabProfile),
];
return PopScope(
// 拦截系统返回键:若在非「首页」Tab 则跳回首页;若在首页则弹出退出确认。
canPop: false,
onPopInvokedWithResult: (didPop, _) async {
if (didPop) return;
if (navigationShell.currentIndex == 0) {
final shouldExit = await _showExitConfirm(context);
if (shouldExit == true) {
await SystemUiUtil.moveToBack();
}
} else {
navigationShell.goBranch(0);
}
},
child: AppPageScaffold(
// The shell contains only tabs. Editors are pushed on the root navigator
// and handle the keyboard inset in their own scaffold.
resizeToAvoidBottomInset: false,
child: Stack(
children: [
HomeTabDragScope(
page: _dragPage,
visibleIndex: _visibleIndex,
child: navigationShell,
),
Align(
alignment: Alignment.bottomCenter,
child: SafeArea(
top: false,
maintainBottomViewPadding: true,
child: Padding(
padding: const EdgeInsets.only(bottom: tabBarMargin),
child: LayoutBuilder(
builder: (context, constraints) {
final tabWidth =
(constraints.maxWidth - 2 * tabBarMargin) /
tabs.length;
// 监听原始事件,玻璃栏仍负责识别手势和选定目标。
return Listener(
onPointerDown: (event) {
if (_tabPointer != null) return;
_tabPointer = event.pointer;
_tabPointerStart = event.localPosition;
},
onPointerMove: (event) {
if (event.pointer != _tabPointer || tabWidth <= 0) {
return;
}
final delta = event.localPosition - _tabPointerStart;
if (_dragPage.value == null &&
delta.dx.abs() <= (delta.dy.abs() + kTouchSlop)) {
return;
}
final x = event.localPosition.dx - tabBarMargin;
final position = x / tabWidth - 0.5;
final last = tabs.length - 1;
final page =
Directionality.of(context) == TextDirection.rtl
? last - position
: position;
_dragPage.value = page.clamp(0.0, last.toDouble());
},
onPointerUp: _finishTabDrag,
onPointerCancel: _finishTabDrag,
// GlassTabBar 仍读取 Flutter SDK 的 Cupertino 主题。
// ignore: deprecated_member_use
child: CupertinoUiCompatibilityBridge(
child: ValueListenableBuilder<int?>(
valueListenable: _visibleIndex,
builder: (context, visibleIndex, _) =>
GlassTabBar.bottom(
selectedIndex: visibleIndex ?? index,
onTabSelected: navigationShell.goBranch,
horizontalPadding: tabBarMargin,
verticalPadding: 0,
iconSize: 26,
tabs: tabs,
),
),
),
);
},
),
),
),
),
],
),
),
);
}
/// 弹出「确认退出应用」对话框。
Future<bool?> _showExitConfirm(BuildContext context) {
final l10n = AppLocalizations.of(context);
return showCupertinoDialog<bool>(
context: context,
builder: (_) => CupertinoAlertDialog(
title: Text(l10n.exitConfirmTitle),
content: Text(l10n.exitConfirmContent),
actions: [
CupertinoDialogAction(
child: Text(l10n.cancel),
onPressed: () => Navigator.of(context).pop(false),
),
CupertinoDialogAction(
isDestructiveAction: true,
child: Text(l10n.exitApp),
onPressed: () => Navigator.of(context).pop(true),
),
],
),
);
}
}