Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Christopher2K/f0b8c3d1b6c818937af4c9c4877a0e61 to your computer and use it in GitHub Desktop.
Save Christopher2K/f0b8c3d1b6c818937af4c9c4877a0e61 to your computer and use it in GitHub Desktop.
Allow ListView.builder() to have leading / trailling elements
import 'package:flutter/material.dart';
class ListViewWithExtraElements<T> extends StatelessWidget {
final List<T> elements;
final List<Widget> leadingElements;
final List<Widget> trailingElements;
final Widget Function(BuildContext context, T element) itemBuilder;
ListViewWithExtraElements({
this.leadingElements = const [],
this.trailingElements = const [],
@required this.elements,
@required this.itemBuilder,
}) : assert(leadingElements != null),
assert(trailingElements != null);
@override
Widget build(BuildContext context) {
final totalElements =
elements.length + leadingElements.length + trailingElements.length;
return ListView.builder(
itemBuilder: (ctx, idx) {
if (idx < leadingElements.length) {
return leadingElements[idx];
} else if (idx >= leadingElements.length &&
idx < leadingElements.length + elements.length) {
final elementIndex = idx - leadingElements.length;
return itemBuilder(ctx, elements[elementIndex]);
} else {
final trailingIndex = idx - leadingElements.length - elements.length;
return trailingElements[trailingIndex];
}
},
itemCount: totalElements,
);
}
}
@Christopher2K
Copy link
Author

Np !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment