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

166 lines
4.6 KiB
Dart

import 'package:flutter/gestures.dart' show Drag;
import 'package:flutter/widgets.dart';
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 this.visibleIndex,
required super.child,
super.key,
});
final ValueNotifier<double?> page;
final ValueNotifier<int?> visibleIndex;
@override
bool updateShouldNotify(HomeTabDragScope oldWidget) =>
page != oldWidget.page || visibleIndex != oldWidget.visibleIndex;
}
/// Displays the shell's existing branch navigators as swipeable pages.
class HomeTabPager extends StatefulWidget {
const HomeTabPager({
required this.navigationShell,
required this.children,
super.key,
});
final StatefulNavigationShell navigationShell;
final List<Widget> children;
@override
State<HomeTabPager> createState() => _HomeTabPagerState();
}
class _HomeTabPagerState extends State<HomeTabPager> {
late final _controller = PageController(
initialPage: widget.navigationShell.currentIndex,
keepPage: false,
);
ValueNotifier<double?>? _dragPage;
Drag? _tabDrag;
ValueNotifier<int?>? _visibleIndex;
@override
void didChangeDependencies() {
super.didChangeDependencies();
final scope = context
.dependOnInheritedWidgetOfExactType<HomeTabDragScope>();
_visibleIndex = scope?.visibleIndex;
final page = scope?.page;
if (page == _dragPage) return;
_dragPage?.removeListener(_updateTabDrag);
_dragPage = page;
page?.addListener(_updateTabDrag);
}
void _updateTabDrag() {
if (!_controller.hasClients) return;
final page = _dragPage?.value;
if (page == null) {
if (_tabDrag != null) {
_tabDrag!.cancel();
_animateToIndex(widget.navigationShell.currentIndex);
}
return;
}
final position = _controller.position;
if (!position.hasContentDimensions) return;
_tabDrag ??= position.drag(
DragStartDetails(globalPosition: Offset.zero),
() => _tabDrag = null,
);
final pixels = position.minScrollExtent + page * position.viewportDimension;
final delta = position.axisDirection == AxisDirection.right
? position.pixels - pixels
: pixels - position.pixels;
_tabDrag!.update(
DragUpdateDetails(
globalPosition: Offset.zero,
delta: Offset(delta, 0),
primaryDelta: delta,
),
);
}
@override
void didUpdateWidget(covariant HomeTabPager oldWidget) {
super.didUpdateWidget(oldWidget);
final index = widget.navigationShell.currentIndex;
if (index != oldWidget.navigationShell.currentIndex) {
_animateToIndex(index);
}
}
void _animateToIndex(int index) {
if (!_controller.hasClients || _controller.page == index) return;
_controller.animateToPage(
index,
duration: const Duration(milliseconds: 260),
curve: Curves.easeOutCubic,
);
}
@override
void dispose() {
_dragPage?.removeListener(_updateTabDrag);
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final activeIndex = widget.navigationShell.currentIndex;
return NotificationListener<ScrollEndNotification>(
onNotification: (notification) {
if (notification.depth != 0 ||
_tabDrag != null ||
_dragPage?.value != null) {
return false;
}
final index = _controller.page?.round();
if (index != null && index != activeIndex) {
widget.navigationShell.goBranch(index);
}
return false;
},
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(
child: TickerMode(enabled: index == activeIndex, child: child),
),
],
),
);
}
}
class _KeepAlivePage extends StatefulWidget {
const _KeepAlivePage({required this.child});
final Widget child;
@override
State<_KeepAlivePage> createState() => _KeepAlivePageState();
}
class _KeepAlivePageState extends State<_KeepAlivePage>
with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
super.build(context);
return widget.child;
}
}