feat(FixedExtentScrollController): Add parent class properties to the constructor. (#163190)

This request is to add configurable parameters keepScrollOffset and
debugLabel to FixedExtentScrollController.

- Fixes  https://github.com/flutter/flutter/issues/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.
This commit is contained in:
StanleyCocos
2025-03-12 01:33:03 +09:00
committed by GitHub
parent 7bf8837116
commit 9d2dcea4ab
2 changed files with 85 additions and 1 deletions

View File

@@ -219,7 +219,13 @@ class FixedExtentScrollController extends ScrollController {
/// Creates a scroll controller for scrollables whose items have the same size.
///
/// [initialItem] defaults to zero.
FixedExtentScrollController({this.initialItem = 0, super.onAttach, super.onDetach});
FixedExtentScrollController({
this.initialItem = 0,
super.keepScrollOffset,
super.debugLabel,
super.onAttach,
super.onDetach,
});
/// The page to show when first creating the scroll view.
///
@@ -294,6 +300,8 @@ class FixedExtentScrollController extends ScrollController {
context: context,
initialItem: initialItem,
oldPosition: oldPosition,
keepScrollOffset: keepScrollOffset,
debugLabel: debugLabel,
);
}
}
@@ -369,6 +377,8 @@ class _FixedExtentScrollPosition extends ScrollPositionWithSingleContext
required super.context,
required int initialItem,
super.oldPosition,
super.keepScrollOffset,
super.debugLabel,
}) : assert(
context is _FixedExtentScrollableState,
'FixedExtentScrollController can only be used with ListWheelScrollViews',

View File

@@ -117,6 +117,80 @@ void main() {
expect(detach, 1);
});
// Regression test for https://github.com/flutter/flutter/issues/162972
testWidgets('FixedExtentScrollController keepScrollOffset', (WidgetTester tester) async {
final PageStorageBucket bucket = PageStorageBucket();
Widget buildFrame(ScrollController controller) {
return Directionality(
textDirection: TextDirection.ltr,
child: PageStorage(
bucket: bucket,
child: KeyedSubtree(
key: const PageStorageKey<String>('ListWheelScrollView'),
child: ListWheelScrollView(
key: UniqueKey(),
itemExtent: 100.0,
controller: controller,
children:
List<Widget>.generate(100, (int index) {
return SizedBox(height: 100.0, width: 400.0, child: Text('Item $index'));
}).toList(),
),
),
),
);
}
FixedExtentScrollController controller = FixedExtentScrollController(initialItem: 2);
addTearDown(controller.dispose);
await tester.pumpWidget(buildFrame(controller));
expect(controller.selectedItem, 2);
expect(controller.offset, 200.0);
expect(
tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 2')),
offsetMoreOrLessEquals(const Offset(200.0, 250.0)),
);
controller.jumpToItem(20);
await tester.pump();
expect(controller.selectedItem, 20);
expect(controller.offset, 2000.0);
expect(
tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 20')),
offsetMoreOrLessEquals(const Offset(200.0, 250.0)),
);
controller = FixedExtentScrollController(initialItem: 25);
addTearDown(controller.dispose);
await tester.pumpWidget(buildFrame(controller));
expect(controller.selectedItem, 20);
expect(controller.offset, 2000.0);
expect(
tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 20')),
offsetMoreOrLessEquals(const Offset(200.0, 250.0)),
);
controller = FixedExtentScrollController(keepScrollOffset: false, initialItem: 10);
addTearDown(controller.dispose);
await tester.pumpWidget(buildFrame(controller));
expect(controller.selectedItem, 10);
expect(controller.offset, 1000.0);
expect(
tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 10')),
offsetMoreOrLessEquals(const Offset(200.0, 250.0)),
);
});
// Regression test for https://github.com/flutter/flutter/issues/162972
test('FixedExtentScrollController debugLabel', () {
final FixedExtentScrollController controller = FixedExtentScrollController(
debugLabel: 'MyCustomWidget',
);
expect(controller.debugLabel, 'MyCustomWidget');
expect(controller.toString(), contains('MyCustomWidget'));
});
testWidgets('ListWheelScrollView needs positive magnification', (WidgetTester tester) async {
expect(() {
ListWheelScrollView(