Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-01-23 19:50:11 +08:00
parent 7eaf05839a
commit 0ab07a713e
22 changed files with 443 additions and 470 deletions

View File

@@ -3,7 +3,6 @@ import 'package:PiliPlus/pages/home/controller.dart';
import 'package:PiliPlus/pages/main/controller.dart';
import 'package:PiliPlus/utils/storage_pref.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
abstract class CommonPageState<
@@ -12,31 +11,33 @@ abstract class CommonPageState<
>
extends State<T> {
R get controller;
RxBool? showBottomBar;
RxBool? showSearchBar;
final _mainController = Get.find<MainController>();
RxBool? _showBottomBar;
RxBool? _showSearchBar;
// late double _downScrollCount = 0.0; // 向下滚动计数器
late double _upScrollCount = 0.0; // 向上滚动计数器
double? _lastScrollPosition; // 记录上次滚动位置
final _enableScrollThreshold = Pref.enableScrollThreshold;
late final double _scrollThreshold = Pref.scrollThreshold; // 滚动阈值
late final scrollController = controller.scrollController;
late final _scrollController = controller.scrollController;
@override
void initState() {
super.initState();
_showBottomBar = _mainController.showBottomBar;
try {
showBottomBar = Get.find<MainController>().bottomBar;
showSearchBar = Get.find<HomeController>().searchBar;
_showSearchBar = Get.find<HomeController>().showSearchBar;
} catch (_) {}
if (_enableScrollThreshold &&
(showBottomBar != null || showSearchBar != null)) {
controller.scrollController.addListener(listener);
(_showBottomBar != null || _showSearchBar != null)) {
_scrollController.addListener(listener);
}
}
Widget onBuild(Widget child) {
if (!_enableScrollThreshold &&
(showBottomBar != null || showSearchBar != null)) {
(_showBottomBar != null || _showSearchBar != null)) {
return NotificationListener<UserScrollNotification>(
onNotification: onNotification,
child: child,
@@ -46,22 +47,24 @@ abstract class CommonPageState<
}
bool onNotification(UserScrollNotification notification) {
if (notification.metrics.axis == Axis.horizontal) return false;
if (notification.metrics.axis == .horizontal) return false;
if (!_mainController.useBottomNav) return false;
final direction = notification.direction;
if (direction == ScrollDirection.forward) {
showBottomBar?.value = true;
showSearchBar?.value = true;
} else if (direction == ScrollDirection.reverse) {
showBottomBar?.value = false;
showSearchBar?.value = false;
if (direction == .forward) {
_showBottomBar?.value = true;
_showSearchBar?.value = true;
} else if (direction == .reverse) {
_showBottomBar?.value = false;
_showSearchBar?.value = false;
}
return false;
}
void listener() {
final direction = scrollController.position.userScrollDirection;
if (!_mainController.useBottomNav) return;
final direction = _scrollController.position.userScrollDirection;
final double currentPosition = scrollController.position.pixels;
final double currentPosition = _scrollController.position.pixels;
// 初始化上次位置
_lastScrollPosition ??= currentPosition;
@@ -69,9 +72,9 @@ abstract class CommonPageState<
// 计算滚动距离
final double scrollDelta = currentPosition - _lastScrollPosition!;
if (direction == ScrollDirection.reverse) {
showBottomBar?.value = false;
showSearchBar?.value = false; // // 向下滚动,累加向下滚动距离,重置向上滚动计数器
if (direction == .reverse) {
_showBottomBar?.value = false;
_showSearchBar?.value = false; // // 向下滚动,累加向下滚动距离,重置向上滚动计数器
_upScrollCount = 0.0; // 重置向上滚动计数器
// if (scrollDelta > 0) {
// _downScrollCount += scrollDelta;
@@ -83,16 +86,16 @@ abstract class CommonPageState<
// searchBarStream?.add(false);
// }
// }
} else if (direction == ScrollDirection.forward) {
} else if (direction == .forward) {
// 向上滚动,累加向上滚动距离,重置向下滚动计数器
if (scrollDelta < 0) {
_upScrollCount += (-scrollDelta); // 使用绝对值
_upScrollCount -= scrollDelta; // 使用绝对值
// _downScrollCount = 0.0; // 重置向下滚动计数器
// 当累计向上滚动距离超过阈值时,显示顶底栏
if (_upScrollCount >= _scrollThreshold) {
showBottomBar?.value = true;
showSearchBar?.value = true;
_showBottomBar?.value = true;
_showSearchBar?.value = true;
}
}
}
@@ -103,9 +106,9 @@ abstract class CommonPageState<
@override
void dispose() {
showSearchBar = null;
showBottomBar = null;
controller.scrollController.removeListener(listener);
_showSearchBar = null;
_showBottomBar = null;
_scrollController.removeListener(listener);
super.dispose();
}
}