Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/flutter/lib/src/widgets/list_wheel_scroll_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -294,6 +300,8 @@ class FixedExtentScrollController extends ScrollController {
context: context,
initialItem: initialItem,
oldPosition: oldPosition,
keepScrollOffset: keepScrollOffset,
debugLabel: debugLabel,
);
}
}
Expand Down Expand Up @@ -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',
Expand Down
74 changes: 74 additions & 0 deletions packages/flutter/test/widgets/list_wheel_scroll_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test, to check that keep scroll offset is working, can you check the actual scroll offset, controller.offset? I might check the existing tests for ScrollController.keepScrollOffset for comparison.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, I have updated it.

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(
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy