summaryrefslogtreecommitdiff
path: root/lib/widgets/common/basic/draggable_scrollbar
diff options
context:
space:
mode:
authormirror <mirror@localhost>2026-11-22 08:41:00 +0900
committermirror <mirror@localhost>2026-11-22 08:41:00 +0900
commit449d8b74bd6d9dae98a07254cdfad677b920e7b0 (patch)
treec131c3528c4d517f56e19b854a7453fedb097d40 /lib/widgets/common/basic/draggable_scrollbar
downloadaves-src-master.tar.gz
aves-src-master.zip
snapshot of aves v1.13.12 (secrets removed: android/app/google-services.json)HEADv1.13.12master
Diffstat (limited to 'lib/widgets/common/basic/draggable_scrollbar')
-rw-r--r--lib/widgets/common/basic/draggable_scrollbar/arrow_clipper.dart38
-rw-r--r--lib/widgets/common/basic/draggable_scrollbar/notifications.dart10
-rw-r--r--lib/widgets/common/basic/draggable_scrollbar/scroll_label.dart30
-rw-r--r--lib/widgets/common/basic/draggable_scrollbar/scrollbar.dart364
-rw-r--r--lib/widgets/common/basic/draggable_scrollbar/transition.dart31
5 files changed, 473 insertions, 0 deletions
diff --git a/lib/widgets/common/basic/draggable_scrollbar/arrow_clipper.dart b/lib/widgets/common/basic/draggable_scrollbar/arrow_clipper.dart
new file mode 100644
index 0000000..94deba5
--- /dev/null
+++ b/lib/widgets/common/basic/draggable_scrollbar/arrow_clipper.dart
@@ -0,0 +1,38 @@
+import 'package:flutter/rendering.dart';
+
+class ArrowClipper extends CustomClipper<Path> {
+ @override
+ Path getClip(Size size) {
+ final path = Path();
+ path.lineTo(0.0, size.height);
+ path.lineTo(size.width, size.height);
+ path.lineTo(size.width, 0.0);
+ path.lineTo(0.0, 0.0);
+ path.close();
+
+ const double arrowWidth = 8.0;
+ final startPointX = (size.width - arrowWidth) / 2;
+ var startPointY = size.height / 2 - arrowWidth / 2;
+ path.moveTo(startPointX, startPointY);
+ path.lineTo(startPointX + arrowWidth / 2, startPointY - arrowWidth / 2);
+ path.lineTo(startPointX + arrowWidth, startPointY);
+ path.lineTo(startPointX + arrowWidth, startPointY + 1.0);
+ path.lineTo(startPointX + arrowWidth / 2, startPointY - arrowWidth / 2 + 1.0);
+ path.lineTo(startPointX, startPointY + 1.0);
+ path.close();
+
+ startPointY = size.height / 2 + arrowWidth / 2;
+ path.moveTo(startPointX + arrowWidth, startPointY);
+ path.lineTo(startPointX + arrowWidth / 2, startPointY + arrowWidth / 2);
+ path.lineTo(startPointX, startPointY);
+ path.lineTo(startPointX, startPointY - 1.0);
+ path.lineTo(startPointX + arrowWidth / 2, startPointY + arrowWidth / 2 - 1.0);
+ path.lineTo(startPointX + arrowWidth, startPointY - 1.0);
+ path.close();
+
+ return path;
+ }
+
+ @override
+ bool shouldReclip(covariant CustomClipper<Path> oldClipper) => false;
+}
diff --git a/lib/widgets/common/basic/draggable_scrollbar/notifications.dart b/lib/widgets/common/basic/draggable_scrollbar/notifications.dart
new file mode 100644
index 0000000..28ca11d
--- /dev/null
+++ b/lib/widgets/common/basic/draggable_scrollbar/notifications.dart
@@ -0,0 +1,10 @@
+import 'package:flutter/widgets.dart';
+
+@immutable
+class DraggableScrollbarNotification extends Notification {
+ final DraggableScrollbarEvent event;
+
+ const DraggableScrollbarNotification(this.event);
+}
+
+enum DraggableScrollbarEvent { dragStart, dragEnd }
diff --git a/lib/widgets/common/basic/draggable_scrollbar/scroll_label.dart b/lib/widgets/common/basic/draggable_scrollbar/scroll_label.dart
new file mode 100644
index 0000000..e42c171
--- /dev/null
+++ b/lib/widgets/common/basic/draggable_scrollbar/scroll_label.dart
@@ -0,0 +1,30 @@
+import 'package:flutter/material.dart';
+
+class ScrollLabel extends StatelessWidget {
+ final Animation<double> animation;
+ final Color backgroundColor;
+ final Widget child;
+
+ const ScrollLabel({
+ super.key,
+ required this.child,
+ required this.animation,
+ required this.backgroundColor,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ return FadeTransition(
+ opacity: animation,
+ child: Container(
+ margin: const EdgeInsetsDirectional.only(end: 12.0),
+ child: Material(
+ elevation: 4.0,
+ color: backgroundColor,
+ borderRadius: const BorderRadius.all(Radius.circular(16)),
+ child: child,
+ ),
+ ),
+ );
+ }
+}
diff --git a/lib/widgets/common/basic/draggable_scrollbar/scrollbar.dart b/lib/widgets/common/basic/draggable_scrollbar/scrollbar.dart
new file mode 100644
index 0000000..245e2a6
--- /dev/null
+++ b/lib/widgets/common/basic/draggable_scrollbar/scrollbar.dart
@@ -0,0 +1,364 @@
+import 'dart:async';
+
+import 'package:aves/model/settings/settings.dart';
+import 'package:aves/widgets/common/basic/draggable_scrollbar/notifications.dart';
+import 'package:aves/widgets/common/basic/draggable_scrollbar/scroll_label.dart';
+import 'package:aves/widgets/common/basic/draggable_scrollbar/transition.dart';
+import 'package:aves/widgets/common/basic/gestures/gesture_detector.dart';
+import 'package:flutter/widgets.dart';
+
+/*
+ adapted from package `draggable_scrollbar` v0.0.4:
+ - removed default thumb builders
+ - allow any `ScrollView` as child
+ - allow any `Widget` as label content
+ - moved out constraints responsibility
+ - various extent & thumb positioning fixes
+ - null safety
+ - directionality aware
+ */
+
+/// Build the Scroll Thumb and label using the current configuration
+typedef ScrollThumbBuilder = Widget Function(
+ Color backgroundColor,
+ Animation<double> thumbAnimation,
+ Animation<double> labelAnimation,
+ double height, {
+ Widget? labelText,
+});
+
+/// Build a Text widget using the current scroll offset
+typedef OffsetLabelBuilder = Widget Function(double offsetY);
+typedef TextLabelBuilder = Widget Function(String label);
+
+/// A widget that will display a BoxScrollView with a ScrollThumb that can be dragged
+/// for quick navigation of the BoxScrollView.
+class DraggableScrollbar extends StatefulWidget {
+ /// The background color of the label and thumb
+ final Color backgroundColor;
+
+ final Map<double, String> Function()? crumbsBuilder;
+
+ final Size scrollThumbSize;
+
+ /// A function that builds a thumb using the current configuration
+ final ScrollThumbBuilder scrollThumbBuilder;
+
+ /// The amount of padding that should surround the thumb
+ final EdgeInsets padding;
+
+ /// Determines how quickly the scrollbar will animate in and out
+ final Duration scrollbarAnimationDuration;
+
+ /// How long should the thumb be visible before fading out
+ final Duration scrollbarTimeToFade;
+
+ /// Build a Text widget from the current offset in the BoxScrollView
+ final OffsetLabelBuilder labelTextBuilder;
+
+ final TextLabelBuilder crumbTextBuilder;
+
+ /// The ScrollController for the BoxScrollView
+ final ScrollController controller;
+
+ final double Function(double scrollOffset, double offsetIncrement)? dragOffsetSnapper;
+
+ /// The view that will be scrolled with the scroll thumb
+ final Widget child;
+
+ const DraggableScrollbar({
+ super.key,
+ required this.backgroundColor,
+ required this.scrollThumbSize,
+ required this.scrollThumbBuilder,
+ required this.controller,
+ this.dragOffsetSnapper,
+ this.crumbsBuilder,
+ this.padding = EdgeInsets.zero,
+ this.scrollbarAnimationDuration = const Duration(milliseconds: 300),
+ this.scrollbarTimeToFade = const Duration(milliseconds: 1000),
+ required this.labelTextBuilder,
+ required this.crumbTextBuilder,
+ required this.child,
+ });
+
+ @override
+ State<DraggableScrollbar> createState() => _DraggableScrollbarState();
+
+ static const double labelThumbPadding = 16;
+
+ static Widget buildScrollThumbAndLabel({
+ required Widget scrollThumb,
+ required Color backgroundColor,
+ required Animation<double> thumbAnimation,
+ required Animation<double> labelAnimation,
+ required Widget? labelText,
+ }) {
+ final scrollThumbAndLabel = labelText == null
+ ? scrollThumb
+ : Row(
+ mainAxisSize: MainAxisSize.min,
+ mainAxisAlignment: MainAxisAlignment.end,
+ children: [
+ ScrollLabel(
+ animation: labelAnimation,
+ backgroundColor: backgroundColor,
+ child: labelText,
+ ),
+ const SizedBox(width: labelThumbPadding),
+ scrollThumb,
+ ],
+ );
+ return SlideFadeTransition(
+ animation: thumbAnimation,
+ child: scrollThumbAndLabel,
+ );
+ }
+}
+
+class _DraggableScrollbarState extends State<DraggableScrollbar> with TickerProviderStateMixin {
+ final ValueNotifier<double> _thumbOffsetNotifier = ValueNotifier(0), _viewOffsetNotifier = ValueNotifier(0);
+ bool _isDragInProcess = false;
+ double _boundlessThumbOffset = 0, _offsetIncrement = 0;
+ late Offset _longPressLastGlobalPosition;
+
+ late AnimationController _thumbAnimationController;
+ late CurvedAnimation _thumbAnimation;
+ late AnimationController _labelAnimationController;
+ late CurvedAnimation _labelAnimation;
+ Timer? _fadeoutTimer;
+ Map<double, String>? _percentCrumbs;
+ final Map<double, String> _viewportCrumbs = {};
+
+ static const double crumbPadding = 30;
+ static const double crumbMinViewportRatio = 4;
+
+ @override
+ void initState() {
+ super.initState();
+
+ _thumbAnimationController = AnimationController(
+ vsync: this,
+ duration: widget.scrollbarAnimationDuration,
+ );
+
+ _thumbAnimation = CurvedAnimation(
+ parent: _thumbAnimationController,
+ curve: Curves.fastOutSlowIn,
+ );
+
+ _labelAnimationController = AnimationController(
+ vsync: this,
+ duration: widget.scrollbarAnimationDuration,
+ );
+
+ _labelAnimation = CurvedAnimation(
+ parent: _labelAnimationController,
+ curve: Curves.fastOutSlowIn,
+ );
+ }
+
+ @override
+ void didUpdateWidget(covariant DraggableScrollbar oldWidget) {
+ super.didUpdateWidget(oldWidget);
+
+ if (oldWidget.crumbsBuilder != widget.crumbsBuilder) {
+ _percentCrumbs = null;
+ }
+ }
+
+ @override
+ void dispose() {
+ _thumbAnimation.dispose();
+ _thumbAnimationController.dispose();
+ _labelAnimation.dispose();
+ _labelAnimationController.dispose();
+ _fadeoutTimer?.cancel();
+ super.dispose();
+ }
+
+ ScrollController get controller => widget.controller;
+
+ double get scrollBarHeight => context.size!.height - widget.padding.vertical;
+
+ double get thumbMaxScrollExtent => scrollBarHeight - widget.scrollThumbSize.height;
+
+ double get thumbMinScrollExtent => 0.0;
+
+ @override
+ Widget build(BuildContext context) {
+ return NotificationListener<ScrollNotification>(
+ onNotification: (notification) {
+ _onScrollNotification(notification);
+ return false;
+ },
+ child: Stack(
+ children: [
+ RepaintBoundary(
+ child: widget.child,
+ ),
+ if (_isDragInProcess)
+ ..._viewportCrumbs.entries.map((kv) {
+ final offset = kv.key;
+ final label = kv.value;
+ return Positioned.directional(
+ textDirection: Directionality.of(context),
+ top: offset,
+ end: DraggableScrollbar.labelThumbPadding + widget.scrollThumbSize.width,
+ child: Padding(
+ padding: widget.padding,
+ child: ConstrainedBox(
+ constraints: BoxConstraints(minHeight: widget.scrollThumbSize.height),
+ child: Center(
+ child: ScrollLabel(
+ animation: kAlwaysCompleteAnimation,
+ backgroundColor: widget.backgroundColor,
+ child: widget.crumbTextBuilder(label),
+ ),
+ ),
+ ),
+ ),
+ );
+ }),
+ // exclude semantics, otherwise this layer will block access to content layers below when using TalkBack
+ ExcludeSemantics(
+ child: RepaintBoundary(
+ child: AGestureDetector(
+ onLongPressStart: (details) {
+ _longPressLastGlobalPosition = details.globalPosition;
+ _onVerticalDragStart();
+ },
+ onLongPressMoveUpdate: (details) {
+ final dy = (details.globalPosition - _longPressLastGlobalPosition).dy;
+ _longPressLastGlobalPosition = details.globalPosition;
+ _onVerticalDragUpdate(dy);
+ },
+ onLongPressEnd: (_) => _onVerticalDragEnd(),
+ onVerticalDragStart: (_) => _onVerticalDragStart(),
+ onVerticalDragUpdate: (details) => _onVerticalDragUpdate(details.delta.dy),
+ onVerticalDragEnd: (_) => _onVerticalDragEnd(),
+ longPressTimeout: settings.longPressTimeout,
+ child: ValueListenableBuilder<double>(
+ valueListenable: _thumbOffsetNotifier,
+ builder: (context, thumbOffset, child) => Container(
+ alignment: AlignmentDirectional.topEnd,
+ padding: EdgeInsets.only(top: thumbOffset) + widget.padding,
+ child: widget.scrollThumbBuilder(
+ widget.backgroundColor,
+ _thumbAnimation,
+ _labelAnimation,
+ widget.scrollThumbSize.height,
+ labelText: _isDragInProcess
+ ? ValueListenableBuilder<double>(
+ valueListenable: _viewOffsetNotifier,
+ builder: (context, viewOffset, child) => widget.labelTextBuilder(viewOffset + thumbOffset),
+ )
+ : null,
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+
+ void _onScrollNotification(ScrollNotification notification) {
+ final scrollMetrics = notification.metrics;
+
+ // do not update the thumb if we cannot actually scroll
+ if (scrollMetrics.minScrollExtent >= scrollMetrics.maxScrollExtent) return;
+
+ _viewOffsetNotifier.value = scrollMetrics.pixels;
+
+ // we update the thumb position from the scrolled offset
+ // when the user is not dragging the thumb
+ if (!_isDragInProcess) {
+ if (notification is ScrollUpdateNotification) {
+ final scrollExtent = (scrollMetrics.pixels / scrollMetrics.maxScrollExtent * thumbMaxScrollExtent);
+ _thumbOffsetNotifier.value = thumbMaxScrollExtent > thumbMinScrollExtent ? scrollExtent.clamp(thumbMinScrollExtent, thumbMaxScrollExtent) : thumbMinScrollExtent;
+ }
+
+ if (notification is ScrollUpdateNotification || notification is OverscrollNotification) {
+ _showThumb();
+ _scheduleFadeout();
+ }
+ }
+ }
+
+ void _onVerticalDragStart() {
+ const DraggableScrollbarNotification(DraggableScrollbarEvent.dragStart).dispatch(context);
+ _boundlessThumbOffset = _thumbOffsetNotifier.value;
+ _offsetIncrement = 1 / thumbMaxScrollExtent * controller.position.maxScrollExtent;
+ _labelAnimationController.forward();
+ _fadeoutTimer?.cancel();
+ _showThumb();
+ _updateViewportCrumbs();
+ setState(() => _isDragInProcess = true);
+ }
+
+ void _onVerticalDragUpdate(double deltaY) {
+ _showThumb();
+ if (_isDragInProcess && thumbMinScrollExtent < thumbMaxScrollExtent) {
+ // thumb offset
+ _boundlessThumbOffset += deltaY;
+ _thumbOffsetNotifier.value = _boundlessThumbOffset.clamp(thumbMinScrollExtent, thumbMaxScrollExtent);
+
+ // scroll offset
+ final min = controller.position.minScrollExtent;
+ final max = controller.position.maxScrollExtent;
+ final scrollOffset = _thumbOffsetNotifier.value / thumbMaxScrollExtent * max;
+ controller.jumpTo((widget.dragOffsetSnapper?.call(scrollOffset, _offsetIncrement) ?? scrollOffset).clamp(min, max));
+ }
+ }
+
+ void _onVerticalDragEnd() {
+ const DraggableScrollbarNotification(DraggableScrollbarEvent.dragEnd).dispatch(context);
+ _scheduleFadeout();
+ setState(() => _isDragInProcess = false);
+ }
+
+ void _showThumb() {
+ if (_thumbAnimationController.status != AnimationStatus.forward) {
+ _thumbAnimationController.forward();
+ }
+ }
+
+ void _scheduleFadeout() {
+ _fadeoutTimer?.cancel();
+ _fadeoutTimer = Timer(widget.scrollbarTimeToFade, () {
+ _thumbAnimationController.reverse();
+ _labelAnimationController.reverse();
+ _fadeoutTimer = null;
+ });
+ }
+
+ void _updateViewportCrumbs() {
+ _viewportCrumbs.clear();
+ final crumbsBuilder = widget.crumbsBuilder;
+ if (crumbsBuilder != null) {
+ final maxOffset = thumbMaxScrollExtent;
+ final position = controller.position;
+ if (position.maxScrollExtent / position.viewportDimension > crumbMinViewportRatio) {
+ double lastLabelOffset = -crumbPadding;
+ _percentCrumbs ??= crumbsBuilder();
+ _percentCrumbs!.entries.forEach((kv) {
+ final percent = kv.key;
+ final label = kv.value;
+ final labelOffset = percent * maxOffset;
+ if (labelOffset >= lastLabelOffset + crumbPadding) {
+ lastLabelOffset = labelOffset;
+ _viewportCrumbs[labelOffset] = label;
+ }
+ });
+ // hide lonesome crumb, whether it is because of a single section,
+ // or because multiple sections collapsed to a single crumb
+ if (_viewportCrumbs.length == 1) {
+ _viewportCrumbs.clear();
+ }
+ }
+ }
+ }
+}
diff --git a/lib/widgets/common/basic/draggable_scrollbar/transition.dart b/lib/widgets/common/basic/draggable_scrollbar/transition.dart
new file mode 100644
index 0000000..2eeb3a3
--- /dev/null
+++ b/lib/widgets/common/basic/draggable_scrollbar/transition.dart
@@ -0,0 +1,31 @@
+import 'package:aves/widgets/common/extensions/build_context.dart';
+import 'package:flutter/widgets.dart';
+
+class SlideFadeTransition extends StatelessWidget {
+ final Animation<double> animation;
+ final Widget child;
+
+ const SlideFadeTransition({
+ super.key,
+ required this.animation,
+ required this.child,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ return AnimatedBuilder(
+ animation: animation,
+ builder: (context, child) => animation.value == 0.0 ? Container() : child!,
+ child: SlideTransition(
+ position: Tween(
+ begin: Offset((context.isRtl ? -1 : 1) * .3, 0),
+ end: Offset.zero,
+ ).animate(animation),
+ child: FadeTransition(
+ opacity: animation,
+ child: child,
+ ),
+ ),
+ );
+ }
+}