11import 'package:flutter/foundation.dart' ;
22import 'package:flutter/material.dart' ;
3+ import 'package:flutter_hooks/flutter_hooks.dart' ;
34
45import '../theme/theme.dart' ;
56import 'ios_glass_theme_pagination.dart' ;
@@ -92,7 +93,14 @@ class PageIndicator extends StatelessWidget {
9293 }
9394}
9495
95- class _WindowedPagination extends StatelessWidget {
96+ typedef _PaginationScrubGeometry = ({
97+ int windowStart,
98+ int visibleCount,
99+ double width,
100+ bool isRtl,
101+ });
102+
103+ class _WindowedPagination extends HookWidget {
96104 const _WindowedPagination ({
97105 required this .containerHeight,
98106 required this .dotKeyPrefix,
@@ -117,28 +125,37 @@ class _WindowedPagination extends StatelessWidget {
117125
118126 @override
119127 Widget build (BuildContext context) {
128+ final scrubGeometry = useState <_PaginationScrubGeometry ?>(null );
120129 final reduceMotion = MediaQuery .disableAnimationsOf (context);
121130 final duration = reduceMotion || ! animateChanges
122131 ? Duration .zero
123132 : const Duration (milliseconds: 150 );
124- final visibleCount = count.clamp (1 , _maximumVisibleDots);
125- final maximumStart = (count - visibleCount).clamp (0 , count);
126- final centerSlot = visibleCount ~ / 2 ;
127- final windowStart = (selected - centerSlot).clamp (0 , maximumStart);
128- final windowEnd = windowStart + visibleCount - 1 ;
129- final hasEarlierDots = windowStart > 0 ;
130- final hasLaterDots = windowEnd < count - 1 ;
131-
132- final pitch = _dotSize + _spacing;
133- final trackWidth = visibleCount * _dotSize + (visibleCount - 1 ) * _spacing;
134- final isRtl = Directionality .of (context) == TextDirection .rtl;
135-
133+ final currentVisibleCount = count.clamp (1 , _maximumVisibleDots);
134+ final maximumStart = (count - currentVisibleCount).clamp (0 , count);
135+ final centerSlot = currentVisibleCount ~ / 2 ;
136136 return LayoutBuilder (
137137 builder: (context, constraints) {
138+ final geometry =
139+ scrubGeometry.value ??
140+ (
141+ windowStart: (selected - centerSlot).clamp (0 , maximumStart),
142+ visibleCount: currentVisibleCount,
143+ width: constraints.maxWidth,
144+ isRtl: Directionality .of (context) == TextDirection .rtl,
145+ );
146+ final windowStart = geometry.windowStart;
147+ final visibleCount = geometry.visibleCount;
148+ final isRtl = geometry.isRtl;
149+ final windowEnd = windowStart + visibleCount - 1 ;
150+ final hasEarlierDots = windowStart > 0 ;
151+ final hasLaterDots = windowEnd < count - 1 ;
152+ final pitch = _dotSize + _spacing;
153+ final trackWidth =
154+ visibleCount * _dotSize + (visibleCount - 1 ) * _spacing;
155+
138156 void selectFromPosition (double dx) {
139157 if (count <= 1 ) return ;
140- final firstCenter =
141- (constraints.maxWidth - trackWidth) / 2 + _dotSize / 2 ;
158+ final firstCenter = (geometry.width - trackWidth) / 2 + _dotSize / 2 ;
142159 final slot = ((dx - firstCenter) / pitch).round ().clamp (
143160 0 ,
144161 visibleCount - 1 ,
@@ -150,11 +167,17 @@ class _WindowedPagination extends StatelessWidget {
150167 return GestureDetector (
151168 behavior: HitTestBehavior .opaque,
152169 excludeFromSemantics: true ,
153- onTapDown: (details) => selectFromPosition (details.localPosition.dx),
154- onHorizontalDragStart: (details) =>
155- selectFromPosition (details.localPosition.dx),
170+ // A tap must not select on pointer-down before a drag can snapshot
171+ // the window. Otherwise the first drag callback already sees a shift.
172+ onTapUp: (details) => selectFromPosition (details.localPosition.dx),
173+ onHorizontalDragStart: (details) {
174+ scrubGeometry.value = geometry;
175+ selectFromPosition (details.localPosition.dx);
176+ },
156177 onHorizontalDragUpdate: (details) =>
157178 selectFromPosition (details.localPosition.dx),
179+ onHorizontalDragEnd: (_) => scrubGeometry.value = null ,
180+ onHorizontalDragCancel: () => scrubGeometry.value = null ,
158181 child: SizedBox (
159182 height: 54 ,
160183 child: Center (
@@ -167,7 +190,8 @@ class _WindowedPagination extends StatelessWidget {
167190 ),
168191 child: LayoutBuilder (
169192 builder: (context, constraints) {
170- final trackOrigin = (constraints.maxWidth - trackWidth) / 2 ;
193+ final trackOrigin =
194+ (geometry.width - Grid .twelve * 2 - trackWidth) / 2 ;
171195 return ClipRect (
172196 child: Stack (
173197 children: [
0 commit comments