Skip to content

Commit ff60778

Browse files
authored
[RenderListWheelViewport] Update content dimensions to prevent scroll offset changes (#96102)
1 parent 099aa02 commit ff60778

3 files changed

Lines changed: 95 additions & 28 deletions

File tree

‎packages/flutter/lib/src/rendering/list_wheel_viewport.dart‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,10 @@ class RenderListWheelViewport
663663
/// by [childManager].
664664
@override
665665
void performLayout() {
666-
final BoxConstraints childConstraints =
667-
constraints.copyWith(
668-
minHeight: _itemExtent,
669-
maxHeight: _itemExtent,
670-
minWidth: 0.0,
671-
);
666+
// Apply the dimensions first in case it changes the scroll offset which
667+
// determines what should be shown.
668+
offset.applyViewportDimension(_viewportExtent);
669+
offset.applyContentDimensions(_minEstimatedScrollExtent, _maxEstimatedScrollExtent);
672670

673671
// The height, in pixel, that children will be visible and might be laid out
674672
// and painted.
@@ -680,7 +678,7 @@ class RenderListWheelViewport
680678
visibleHeight *= 2;
681679

682680
final double firstVisibleOffset =
683-
offset.pixels + _itemExtent / 2 - visibleHeight / 2;
681+
offset.pixels + _itemExtent / 2 - visibleHeight / 2;
684682
final double lastVisibleOffset = firstVisibleOffset + visibleHeight;
685683

686684
// The index range that we want to spawn children. We find indexes that
@@ -721,6 +719,11 @@ class RenderListWheelViewport
721719
_destroyChild(firstChild!);
722720
}
723721

722+
final BoxConstraints childConstraints = constraints.copyWith(
723+
minHeight: _itemExtent,
724+
maxHeight: _itemExtent,
725+
minWidth: 0.0,
726+
);
724727
// If there is no child at this stage, we add the first one that is in
725728
// target range.
726729
if (childCount == 0) {
@@ -759,8 +762,6 @@ class RenderListWheelViewport
759762
_layoutChild(lastChild!, childConstraints, ++currentLastIndex);
760763
}
761764

762-
offset.applyViewportDimension(_viewportExtent);
763-
764765
// Applying content dimensions bases on how the childManager builds widgets:
765766
// if it is available to provide a child just out of target range, then
766767
// we don't know whether there's a limit yet, and set the dimension to the
@@ -770,16 +771,16 @@ class RenderListWheelViewport
770771
? _minEstimatedScrollExtent
771772
: indexToScrollOffset(targetFirstIndex);
772773
final double maxScrollExtent = childManager.childExistsAt(targetLastIndex + 1)
773-
? _maxEstimatedScrollExtent
774-
: indexToScrollOffset(targetLastIndex);
774+
? _maxEstimatedScrollExtent
775+
: indexToScrollOffset(targetLastIndex);
775776
offset.applyContentDimensions(minScrollExtent, maxScrollExtent);
776777
}
777778

778779
bool _shouldClipAtCurrentOffset() {
779780
final double highestUntransformedPaintY =
780-
_getUntransformedPaintingCoordinateY(0.0);
781+
_getUntransformedPaintingCoordinateY(0.0);
781782
return highestUntransformedPaintY < 0.0
782-
|| size.height < highestUntransformedPaintY + _maxEstimatedScrollExtent + _itemExtent;
783+
|| size.height < highestUntransformedPaintY + _maxEstimatedScrollExtent + _itemExtent;
783784
}
784785

785786
@override

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -766,24 +766,26 @@ class _ListWheelScrollViewState extends State<ListWheelScrollView> {
766766
}
767767
}
768768

769+
bool _handleScrollNotification(ScrollNotification notification) {
770+
if (notification.depth == 0
771+
&& widget.onSelectedItemChanged != null
772+
&& notification is ScrollUpdateNotification
773+
&& notification.metrics is FixedExtentMetrics) {
774+
final FixedExtentMetrics metrics = notification.metrics as FixedExtentMetrics;
775+
final int currentItemIndex = metrics.itemIndex;
776+
if (currentItemIndex != _lastReportedItemIndex) {
777+
_lastReportedItemIndex = currentItemIndex;
778+
final int trueIndex = widget.childDelegate.trueIndexOf(currentItemIndex);
779+
widget.onSelectedItemChanged!(trueIndex);
780+
}
781+
}
782+
return false;
783+
}
784+
769785
@override
770786
Widget build(BuildContext context) {
771787
return NotificationListener<ScrollNotification>(
772-
onNotification: (ScrollNotification notification) {
773-
if (notification.depth == 0
774-
&& widget.onSelectedItemChanged != null
775-
&& notification is ScrollUpdateNotification
776-
&& notification.metrics is FixedExtentMetrics) {
777-
final FixedExtentMetrics metrics = notification.metrics as FixedExtentMetrics;
778-
final int currentItemIndex = metrics.itemIndex;
779-
if (currentItemIndex != _lastReportedItemIndex) {
780-
_lastReportedItemIndex = currentItemIndex;
781-
final int trueIndex = widget.childDelegate.trueIndexOf(currentItemIndex);
782-
widget.onSelectedItemChanged!(trueIndex);
783-
}
784-
}
785-
return false;
786-
},
788+
onNotification: _handleScrollNotification,
787789
child: _FixedExtentScrollable(
788790
controller: scrollController,
789791
physics: widget.physics,

‎packages/flutter/test/widgets/list_wheel_scroll_view_test.dart‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ void main() {
230230
});
231231

232232
testWidgets('child builder with lower and upper limits', (WidgetTester tester) async {
233+
// Adjust the content dimensions at the end of `RenderListWheelViewport.performLayout()`
233234
final List<int> paintedChildren = <int>[];
234235

235236
final FixedExtentScrollController controller =
@@ -285,6 +286,69 @@ void main() {
285286
});
286287

287288
group('layout', () {
289+
// Regression test for https://github.com/flutter/flutter/issues/90953
290+
testWidgets('ListWheelScrollView childDelegate update test 2', (WidgetTester tester) async {
291+
final FixedExtentScrollController controller = FixedExtentScrollController( initialItem: 2 );
292+
Widget buildFrame(int childCount) {
293+
return Directionality(
294+
textDirection: TextDirection.ltr,
295+
child: ListWheelScrollView.useDelegate(
296+
controller: controller,
297+
itemExtent: 400.0,
298+
onSelectedItemChanged: (_) { },
299+
childDelegate: ListWheelChildBuilderDelegate(
300+
childCount: childCount,
301+
builder: (BuildContext context, int index) {
302+
return SizedBox(
303+
width: 400.0,
304+
height: 400.0,
305+
child: Text(index.toString()),
306+
);
307+
},
308+
),
309+
),
310+
);
311+
}
312+
313+
await tester.pumpWidget(buildFrame(5));
314+
expect(find.text('0'), findsNothing);
315+
expect(tester.renderObject(find.text('1')).attached, true);
316+
expect(tester.renderObject(find.text('2')).attached, true);
317+
expect(tester.renderObject(find.text('3')).attached, true);
318+
expect(find.text('4'), findsNothing);
319+
320+
// Remove the last 3 items.
321+
await tester.pumpWidget(buildFrame(2));
322+
expect(tester.renderObject(find.text('0')).attached, true);
323+
expect(tester.renderObject(find.text('1')).attached, true);
324+
expect(find.text('3'), findsNothing);
325+
326+
// Add 3 items at the end.
327+
await tester.pumpWidget(buildFrame(5));
328+
expect(tester.renderObject(find.text('0')).attached, true);
329+
expect(tester.renderObject(find.text('1')).attached, true);
330+
expect(tester.renderObject(find.text('2')).attached, true);
331+
expect(find.text('3'), findsNothing);
332+
expect(find.text('4'), findsNothing);
333+
334+
335+
// Scroll to the last item.
336+
final TestGesture scrollGesture = await tester.startGesture(const Offset(10.0, 10.0));
337+
await scrollGesture.moveBy(const Offset(0.0, -1200.0));
338+
await tester.pump();
339+
expect(find.text('0'), findsNothing);
340+
expect(find.text('1'), findsNothing);
341+
expect(find.text('2'), findsNothing);
342+
expect(tester.renderObject(find.text('3')).attached, true);
343+
expect(tester.renderObject(find.text('4')).attached, true);
344+
345+
// Remove the last 3 items.
346+
await tester.pumpWidget(buildFrame(2));
347+
expect(tester.renderObject(find.text('0')).attached, true);
348+
expect(tester.renderObject(find.text('1')).attached, true);
349+
expect(find.text('3'), findsNothing);
350+
});
351+
288352
// Regression test for https://github.com/flutter/flutter/issues/58144
289353
testWidgets('ListWheelScrollView childDelegate update test', (WidgetTester tester) async {
290354
final FixedExtentScrollController controller = FixedExtentScrollController();

0 commit comments

Comments
 (0)