Skip to content

Commit 9d2dcea

Browse files
authored
feat(FixedExtentScrollController): Add parent class properties to the constructor. (#163190)
This request is to add configurable parameters keepScrollOffset and debugLabel to FixedExtentScrollController. - Fixes flutter/flutter#162972 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
1 parent 7bf8837 commit 9d2dcea

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

packages/flutter/lib/src/widgets/list_wheel_scroll_view.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,13 @@ class FixedExtentScrollController extends ScrollController {
219219
/// Creates a scroll controller for scrollables whose items have the same size.
220220
///
221221
/// [initialItem] defaults to zero.
222-
FixedExtentScrollController({this.initialItem = 0, super.onAttach, super.onDetach});
222+
FixedExtentScrollController({
223+
this.initialItem = 0,
224+
super.keepScrollOffset,
225+
super.debugLabel,
226+
super.onAttach,
227+
super.onDetach,
228+
});
223229

224230
/// The page to show when first creating the scroll view.
225231
///
@@ -294,6 +300,8 @@ class FixedExtentScrollController extends ScrollController {
294300
context: context,
295301
initialItem: initialItem,
296302
oldPosition: oldPosition,
303+
keepScrollOffset: keepScrollOffset,
304+
debugLabel: debugLabel,
297305
);
298306
}
299307
}
@@ -369,6 +377,8 @@ class _FixedExtentScrollPosition extends ScrollPositionWithSingleContext
369377
required super.context,
370378
required int initialItem,
371379
super.oldPosition,
380+
super.keepScrollOffset,
381+
super.debugLabel,
372382
}) : assert(
373383
context is _FixedExtentScrollableState,
374384
'FixedExtentScrollController can only be used with ListWheelScrollViews',

packages/flutter/test/widgets/list_wheel_scroll_view_test.dart

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,80 @@ void main() {
117117
expect(detach, 1);
118118
});
119119

120+
// Regression test for https://github.com/flutter/flutter/issues/162972
121+
testWidgets('FixedExtentScrollController keepScrollOffset', (WidgetTester tester) async {
122+
final PageStorageBucket bucket = PageStorageBucket();
123+
124+
Widget buildFrame(ScrollController controller) {
125+
return Directionality(
126+
textDirection: TextDirection.ltr,
127+
child: PageStorage(
128+
bucket: bucket,
129+
child: KeyedSubtree(
130+
key: const PageStorageKey<String>('ListWheelScrollView'),
131+
child: ListWheelScrollView(
132+
key: UniqueKey(),
133+
itemExtent: 100.0,
134+
controller: controller,
135+
children:
136+
List<Widget>.generate(100, (int index) {
137+
return SizedBox(height: 100.0, width: 400.0, child: Text('Item $index'));
138+
}).toList(),
139+
),
140+
),
141+
),
142+
);
143+
}
144+
145+
FixedExtentScrollController controller = FixedExtentScrollController(initialItem: 2);
146+
addTearDown(controller.dispose);
147+
await tester.pumpWidget(buildFrame(controller));
148+
expect(controller.selectedItem, 2);
149+
expect(controller.offset, 200.0);
150+
expect(
151+
tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 2')),
152+
offsetMoreOrLessEquals(const Offset(200.0, 250.0)),
153+
);
154+
155+
controller.jumpToItem(20);
156+
await tester.pump();
157+
expect(controller.selectedItem, 20);
158+
expect(controller.offset, 2000.0);
159+
expect(
160+
tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 20')),
161+
offsetMoreOrLessEquals(const Offset(200.0, 250.0)),
162+
);
163+
164+
controller = FixedExtentScrollController(initialItem: 25);
165+
addTearDown(controller.dispose);
166+
await tester.pumpWidget(buildFrame(controller));
167+
expect(controller.selectedItem, 20);
168+
expect(controller.offset, 2000.0);
169+
expect(
170+
tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 20')),
171+
offsetMoreOrLessEquals(const Offset(200.0, 250.0)),
172+
);
173+
174+
controller = FixedExtentScrollController(keepScrollOffset: false, initialItem: 10);
175+
addTearDown(controller.dispose);
176+
await tester.pumpWidget(buildFrame(controller));
177+
expect(controller.selectedItem, 10);
178+
expect(controller.offset, 1000.0);
179+
expect(
180+
tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 10')),
181+
offsetMoreOrLessEquals(const Offset(200.0, 250.0)),
182+
);
183+
});
184+
185+
// Regression test for https://github.com/flutter/flutter/issues/162972
186+
test('FixedExtentScrollController debugLabel', () {
187+
final FixedExtentScrollController controller = FixedExtentScrollController(
188+
debugLabel: 'MyCustomWidget',
189+
);
190+
expect(controller.debugLabel, 'MyCustomWidget');
191+
expect(controller.toString(), contains('MyCustomWidget'));
192+
});
193+
120194
testWidgets('ListWheelScrollView needs positive magnification', (WidgetTester tester) async {
121195
expect(() {
122196
ListWheelScrollView(

0 commit comments

Comments
 (0)