mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-04-20 03:06:59 +08:00
opt hide top/bottom bar
Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import 'package:PiliPlus/common/constants.dart' show StyleString;
|
||||
import 'package:PiliPlus/pages/common/common_controller.dart';
|
||||
import 'package:PiliPlus/pages/home/controller.dart';
|
||||
import 'package:PiliPlus/pages/main/controller.dart';
|
||||
import 'package:PiliPlus/utils/storage_pref.dart';
|
||||
import 'package:flutter/foundation.dart' show clampDouble;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
@@ -12,33 +12,17 @@ abstract class CommonPageState<
|
||||
extends State<T> {
|
||||
R get controller;
|
||||
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;
|
||||
RxDouble? _barOffset;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_showBottomBar = _mainController.showBottomBar;
|
||||
try {
|
||||
_showSearchBar = Get.find<HomeController>().showSearchBar;
|
||||
} catch (_) {}
|
||||
if (_enableScrollThreshold &&
|
||||
(_showBottomBar != null || _showSearchBar != null)) {
|
||||
_scrollController.addListener(listener);
|
||||
}
|
||||
_barOffset = _mainController.barOffset;
|
||||
}
|
||||
|
||||
Widget onBuild(Widget child) {
|
||||
if (!_enableScrollThreshold &&
|
||||
(_showBottomBar != null || _showSearchBar != null)) {
|
||||
return NotificationListener<UserScrollNotification>(
|
||||
if (_barOffset != null) {
|
||||
return NotificationListener<ScrollUpdateNotification>(
|
||||
onNotification: onNotification,
|
||||
child: child,
|
||||
);
|
||||
@@ -46,69 +30,29 @@ abstract class CommonPageState<
|
||||
return child;
|
||||
}
|
||||
|
||||
bool onNotification(UserScrollNotification notification) {
|
||||
if (notification.metrics.axis == .horizontal) return false;
|
||||
bool onNotification(ScrollUpdateNotification notification) {
|
||||
if (!_mainController.useBottomNav) return false;
|
||||
final direction = notification.direction;
|
||||
if (direction == .forward) {
|
||||
_showBottomBar?.value = true;
|
||||
_showSearchBar?.value = true;
|
||||
} else if (direction == .reverse) {
|
||||
_showBottomBar?.value = false;
|
||||
_showSearchBar?.value = false;
|
||||
|
||||
final metrics = notification.metrics;
|
||||
if (metrics.axis == .horizontal ||
|
||||
metrics.pixels < 0 ||
|
||||
notification.dragDetails == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final scrollDelta = notification.scrollDelta ?? 0.0;
|
||||
_barOffset!.value = clampDouble(
|
||||
_barOffset!.value + scrollDelta,
|
||||
0.0,
|
||||
StyleString.topBarHeight,
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void listener() {
|
||||
if (!_mainController.useBottomNav) return;
|
||||
final direction = _scrollController.position.userScrollDirection;
|
||||
|
||||
final double currentPosition = _scrollController.position.pixels;
|
||||
|
||||
// 初始化上次位置
|
||||
_lastScrollPosition ??= currentPosition;
|
||||
|
||||
// 计算滚动距离
|
||||
final double scrollDelta = currentPosition - _lastScrollPosition!;
|
||||
|
||||
if (direction == .reverse) {
|
||||
_showBottomBar?.value = false;
|
||||
_showSearchBar?.value = false; // // 向下滚动,累加向下滚动距离,重置向上滚动计数器
|
||||
_upScrollCount = 0.0; // 重置向上滚动计数器
|
||||
// if (scrollDelta > 0) {
|
||||
// _downScrollCount += scrollDelta;
|
||||
// // _upScrollCount = 0.0; // 重置向上滚动计数器
|
||||
|
||||
// // 当累计向下滚动距离超过阈值时,隐藏顶底栏
|
||||
// if (_downScrollCount >= _scrollThreshold) {
|
||||
// mainStream?.add(false);
|
||||
// searchBarStream?.add(false);
|
||||
// }
|
||||
// }
|
||||
} else if (direction == .forward) {
|
||||
// 向上滚动,累加向上滚动距离,重置向下滚动计数器
|
||||
if (scrollDelta < 0) {
|
||||
_upScrollCount -= scrollDelta; // 使用绝对值
|
||||
// _downScrollCount = 0.0; // 重置向下滚动计数器
|
||||
|
||||
// 当累计向上滚动距离超过阈值时,显示顶底栏
|
||||
if (_upScrollCount >= _scrollThreshold) {
|
||||
_showBottomBar?.value = true;
|
||||
_showSearchBar?.value = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 更新上次位置
|
||||
_lastScrollPosition = currentPosition;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_showSearchBar = null;
|
||||
_showBottomBar = null;
|
||||
_scrollController.removeListener(listener);
|
||||
_barOffset = null;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user