Skip to content

Commit 6b4b31e

Browse files
authored
feat\!: add index to separator builder (#47)
1 parent 4418d97 commit 6b4b31e

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

example/lib/advanced/advanced_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AdvancedExample extends StatelessWidget {
3535
hasError: state.error != null,
3636
hasReachedMax: state.hasReachedMax,
3737
onFetchData: () => context.read<PeopleCubit>().loadData(),
38-
separatorBuilder: (context) => const Divider(),
38+
separatorBuilder: (context, _) => const Divider(),
3939
itemBuilder: (context, index) {
4040
return ListTile(
4141
dense: true,

example/lib/simple/simple_example.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ class SimpleExampleState extends State<SimpleExample> {
4747
itemCount: _items.length,
4848
isLoading: _isLoading,
4949
onFetchData: _fetchData,
50-
separatorBuilder: (context) => const Divider(),
50+
separatorBuilder: (context, index) {
51+
return Divider(
52+
color: index.isOdd ? Colors.black : Colors.blue,
53+
);
54+
},
5155
itemBuilder: (context, index) {
5256
return ListTile(
5357
dense: true,

example/lib/sliver/sliver_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SliverExampleState extends State<SliverExample> {
5353
itemCount: _items.length,
5454
isLoading: _isLoading,
5555
onFetchData: _fetchData,
56-
separatorBuilder: (context) => const Divider(),
56+
separatorBuilder: (context, _) => const Divider(),
5757
itemBuilder: (context, index) {
5858
return ListTile(
5959
dense: true,

lib/src/infinite_list.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class InfiniteList extends StatelessWidget {
165165
///
166166
/// Is optional and can be `null`.
167167
/// {@endtemplate}
168-
final WidgetBuilder? separatorBuilder;
168+
final IndexedWidgetBuilder? separatorBuilder;
169169

170170
/// {@template item_builder}
171171
/// The builder used to build a widget for every index of the `itemCount`.

lib/src/sliver_infinite_list.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SliverInfiniteList extends StatefulWidget {
5454
final WidgetBuilder? errorBuilder;
5555

5656
/// {@macro separator_builder}
57-
final WidgetBuilder? separatorBuilder;
57+
final IndexedWidgetBuilder? separatorBuilder;
5858

5959
/// {@macro item_builder}
6060
final ItemBuilder itemBuilder;
@@ -142,10 +142,10 @@ class _SliverInfiniteListState extends State<SliverInfiniteList> {
142142
return widget.emptyBuilder!(context);
143143
}
144144
} else {
145+
final itemIndex = !showSeparator ? index : (index / 2).floor();
145146
if (showSeparator && index.isOdd) {
146-
return widget.separatorBuilder!(context);
147+
return widget.separatorBuilder!(context, itemIndex);
147148
} else {
148-
final itemIndex = !showSeparator ? index : (index / 2).floor();
149149
return widget.itemBuilder(context, itemIndex);
150150
}
151151
}

test/infinite_list_test.dart

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void main() {
169169
InfiniteList(
170170
itemCount: itemCount,
171171
onFetchData: emptyCallback,
172-
separatorBuilder: (_) {
172+
separatorBuilder: (_, __) {
173173
separatorBuilderCalls++;
174174
return const Divider();
175175
},
@@ -181,6 +181,28 @@ void main() {
181181
},
182182
);
183183

184+
testWidgets(
185+
'forward the correct indexes to separatorBuilder',
186+
(tester) async {
187+
const itemCount = 20;
188+
final indexes = <int>[];
189+
190+
await tester.pumpApp(
191+
InfiniteList(
192+
itemCount: itemCount,
193+
onFetchData: emptyCallback,
194+
separatorBuilder: (_, index) {
195+
indexes.add(index);
196+
return const Divider();
197+
},
198+
itemBuilder: (_, i) => Text('$i'),
199+
),
200+
);
201+
202+
expect(indexes, equals(List.generate(itemCount - 1, (index) => index)));
203+
},
204+
);
205+
184206
group('with an empty set of items', () {
185207
testWidgets(
186208
'renders no list items by default',
@@ -380,7 +402,7 @@ void main() {
380402
dimension: 40,
381403
child: ColoredBox(color: colors[i % colors.length]),
382404
),
383-
separatorBuilder: (_) => const SizedBox.square(
405+
separatorBuilder: (_, __) => const SizedBox.square(
384406
dimension: 10,
385407
child: ColoredBox(color: Colors.pink),
386408
),
@@ -422,7 +444,7 @@ void main() {
422444
dimension: 40,
423445
child: ColoredBox(color: colors[i % colors.length]),
424446
),
425-
separatorBuilder: (_) => const SizedBox.square(
447+
separatorBuilder: (_, __) => const SizedBox.square(
426448
dimension: 10,
427449
child: ColoredBox(color: Colors.pink),
428450
),

0 commit comments

Comments
 (0)