Skip to content

Commit 5f6e3ed

Browse files
authored
Add itemExtentBuilder support to ListView.separated (flutter#191157)
This PR adds `itemExtentBuilder` support to `ListView.separated`, allowing optimal scrolling performance when item and separator extents are known. Closes flutter#138758 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 8698e4d commit 5f6e3ed

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ class ListView extends BoxScrollView {
15241524
super.physics,
15251525
super.shrinkWrap,
15261526
super.padding,
1527+
this.itemExtentBuilder,
15271528
required NullableIndexedWidgetBuilder itemBuilder,
15281529
@Deprecated(
15291530
'Use findItemIndexCallback instead. '
@@ -1558,7 +1559,6 @@ class ListView extends BoxScrollView {
15581559
'Use findItemIndexCallback as findChildIndexCallback is deprecated.',
15591560
),
15601561
itemExtent = null,
1561-
itemExtentBuilder = null,
15621562
prototypeItem = null,
15631563
childrenDelegate = SliverChildBuilderDelegate(
15641564
(BuildContext context, int index) {
@@ -1666,6 +1666,14 @@ class ListView extends BoxScrollView {
16661666
/// Unlike [itemExtent] or [prototypeItem], this allows children to have
16671667
/// different extents.
16681668
///
1669+
/// For [ListView.separated], the `index` argument passed to the callback
1670+
/// represents the interleaved child index, which includes both items and separators.
1671+
/// Even indices correspond to the items, and odd indices correspond to the
1672+
/// separators.
1673+
///
1674+
/// To map the interleaved child index to the semantic item index (for example,
1675+
/// to look up the extent in a data list), divide it by 2 (`index ~/ 2`).
1676+
///
16691677
/// See also:
16701678
///
16711679
/// * [SliverVariedExtentList], the sliver used internally when this property

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,4 +1186,40 @@ void main() {
11861186
);
11871187
expect(tester.getSize(find.byType(ListView)), Size.zero);
11881188
});
1189+
1190+
testWidgets(
1191+
'ListView.separated itemExtentBuilder works with different extents for items and separators',
1192+
(WidgetTester tester) async {
1193+
final controller = ScrollController();
1194+
addTearDown(controller.dispose);
1195+
await tester.pumpWidget(
1196+
Directionality(
1197+
textDirection: TextDirection.ltr,
1198+
child: ListView.separated(
1199+
controller: controller,
1200+
itemCount: 10,
1201+
itemExtentBuilder: (int index, SliverLayoutDimensions dimensions) {
1202+
return index.isEven ? 100.0 : 10.0; // 100 for items, 10 for separators
1203+
},
1204+
itemBuilder: (BuildContext context, int index) {
1205+
return SizedBox(height: 100.0, child: Text('Item $index'));
1206+
},
1207+
separatorBuilder: (BuildContext context, int index) {
1208+
return SizedBox(height: 10.0, child: Text('Separator $index'));
1209+
},
1210+
),
1211+
),
1212+
);
1213+
1214+
expect(find.text('Item 0'), findsOneWidget);
1215+
expect(find.text('Separator 0'), findsOneWidget);
1216+
expect(find.text('Item 5'), findsOneWidget);
1217+
expect(find.text('Item 6'), findsNothing);
1218+
1219+
controller.jumpTo(200.0);
1220+
await tester.pump();
1221+
expect(find.text('Item 0'), findsNothing);
1222+
expect(find.text('Item 1'), findsOneWidget);
1223+
},
1224+
);
11891225
}

0 commit comments

Comments
 (0)