Created
March 9, 2020 16:08
-
-
Save Christopher2K/f0b8c3d1b6c818937af4c9c4877a0e61 to your computer and use it in GitHub Desktop.
Allow ListView.builder() to have leading / trailling elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you