@@ -55,6 +55,11 @@ class _ScrollablePositionedListPageState
55
55
/// Controller to scroll or jump to a particular item.
56
56
final ItemScrollController itemScrollController = ItemScrollController ();
57
57
58
+ /// Controller to scroll a certain number of pixels relative to the current
59
+ /// scroll offset.
60
+ final ScrollOffsetController scrollOffsetController =
61
+ ScrollOffsetController ();
62
+
58
63
/// Listener that reports the position of items when the list is scrolled.
59
64
final ItemPositionsListener itemPositionsListener =
60
65
ItemPositionsListener .create ();
@@ -89,10 +94,12 @@ class _ScrollablePositionedListPageState
89
94
),
90
95
positionsView,
91
96
Row (
97
+ mainAxisSize: MainAxisSize .min,
92
98
children: < Widget > [
93
99
Column (
94
100
children: < Widget > [
95
101
scrollControlButtons,
102
+ scrollOffsetControlButtons,
96
103
const SizedBox (height: 10 ),
97
104
jumpControlButtons,
98
105
alignmentControl,
@@ -130,6 +137,7 @@ class _ScrollablePositionedListPageState
130
137
itemBuilder: (context, index) => item (index, orientation),
131
138
itemScrollController: itemScrollController,
132
139
itemPositionsListener: itemPositionsListener,
140
+ scrollOffsetController: scrollOffsetController,
133
141
reverse: reversed,
134
142
scrollDirection: orientation == Orientation .portrait
135
143
? Axis .vertical
@@ -181,12 +189,24 @@ class _ScrollablePositionedListPageState
181
189
Widget get scrollControlButtons => Row (
182
190
children: < Widget > [
183
191
const Text ('scroll to' ),
184
- scrollButton (0 ),
185
- scrollButton (5 ),
186
- scrollButton (10 ),
187
- scrollButton (100 ),
188
- scrollButton (1000 ),
189
- scrollButton (5000 ),
192
+ scrollItemButton (0 ),
193
+ scrollItemButton (5 ),
194
+ scrollItemButton (10 ),
195
+ scrollItemButton (100 ),
196
+ scrollItemButton (1000 ),
197
+ scrollItemButton (5000 ),
198
+ ],
199
+ );
200
+
201
+ Widget get scrollOffsetControlButtons => Row (
202
+ children: < Widget > [
203
+ const Text ('scroll by' ),
204
+ scrollOffsetButton (- 1000 ),
205
+ scrollOffsetButton (- 100 ),
206
+ scrollOffsetButton (- 10 ),
207
+ scrollOffsetButton (10 ),
208
+ scrollOffsetButton (100 ),
209
+ scrollOffsetButton (1000 ),
190
210
],
191
211
);
192
212
@@ -202,26 +222,41 @@ class _ScrollablePositionedListPageState
202
222
],
203
223
);
204
224
205
- final _scrollButtonStyle = ButtonStyle (
206
- padding: MaterialStateProperty .all (
207
- const EdgeInsets .symmetric (horizontal: 20 , vertical: 0 ),
208
- ),
209
- minimumSize: MaterialStateProperty .all (Size .zero),
210
- tapTargetSize: MaterialTapTargetSize .shrinkWrap,
211
- );
225
+ ButtonStyle _scrollButtonStyle ({required double horizonalPadding}) =>
226
+ ButtonStyle (
227
+ padding: MaterialStateProperty .all (
228
+ EdgeInsets .symmetric (horizontal: horizonalPadding, vertical: 0 ),
229
+ ),
230
+ minimumSize: MaterialStateProperty .all (Size .zero),
231
+ tapTargetSize: MaterialTapTargetSize .shrinkWrap,
232
+ );
212
233
213
- Widget scrollButton (int value) => TextButton (
234
+ Widget scrollItemButton (int value) => TextButton (
214
235
key: ValueKey <String >('Scroll$value ' ),
215
236
onPressed: () => scrollTo (value),
216
237
child: Text ('$value ' ),
217
- style: _scrollButtonStyle,
238
+ style: _scrollButtonStyle (horizonalPadding: 20 ),
239
+ );
240
+
241
+ Widget scrollOffsetButton (int value) => TextButton (
242
+ key: ValueKey <String >('Scroll$value ' ),
243
+ onPressed: () => scrollBy (value.toDouble ()),
244
+ child: Text ('$value ' ),
245
+ style: _scrollButtonStyle (horizonalPadding: 10 ),
246
+ );
247
+
248
+ Widget scrollPixelButton (int value) => TextButton (
249
+ key: ValueKey <String >('Scroll$value ' ),
250
+ onPressed: () => scrollTo (value),
251
+ child: Text ('$value ' ),
252
+ style: _scrollButtonStyle (horizonalPadding: 20 ),
218
253
);
219
254
220
255
Widget jumpButton (int value) => TextButton (
221
256
key: ValueKey <String >('Jump$value ' ),
222
257
onPressed: () => jumpTo (value),
223
258
child: Text ('$value ' ),
224
- style: _scrollButtonStyle,
259
+ style: _scrollButtonStyle (horizonalPadding : 20 ) ,
225
260
);
226
261
227
262
void scrollTo (int index) => itemScrollController.scrollTo (
@@ -230,6 +265,9 @@ class _ScrollablePositionedListPageState
230
265
curve: Curves .easeInOutCubic,
231
266
alignment: alignment);
232
267
268
+ void scrollBy (double offset) => scrollOffsetController.animateScroll (
269
+ offset: offset, duration: scrollDuration, curve: Curves .easeInOutCubic);
270
+
233
271
void jumpTo (int index) =>
234
272
itemScrollController.jumpTo (index: index, alignment: alignment);
235
273
0 commit comments