I am trying to insert a custom tile after every 4 entries in ListView.builder. The problem is that when the Listview.builder is scrolled up/down, the page number changes. Please see the clip. (Notice my custom tile in grey stating page numbers)
https://youtube.com/shorts/BTm7BEya62A?feature=share
My Code is as follows:
int pageCounter = 1;
int oldPageCounter = 0;
final int pageLength = 735;
int pageLengthLeft = 735;
Listview.builder...
itemBuilder: (BuildContext context, int index) {
adjustPageCounter(widget.mapOfProblems.values.elementAt(index), index);
...
child: (oldPageCounter != pageCounter)
? Column(
children: [
getPageDivider(),
MyUsualListTile()
])
: MyUsualListTile()
)}
getPageDivider() {
oldPageCounter = pageCounter;
return Container(
padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 16),
width: double.infinity,
color: Colors.grey[300],
child: Align(alignment: Alignment.topRight,child: Text('Page $pageCounter'),),
);
}
void adjustPageCounter(element, int index) {
if (element is Note || element is Snag){
if (pageLengthLeft<165) resetPageLengthIncCounter();
pageLengthLeft -= 165;
}
if (element is Photos) {
if (pageLengthLeft < 250) resetPageLengthIncCounter();
pageLengthLeft -= 250;
}
}
void resetPageLengthIncCounter() { pageLengthLeft = pageLength; pageCounter++;}
2
Answers
The best way to do this is to use ListView.separated() like so:
See screenshot
You can try like any of bellow approach.
Using simple listView
Using separated ListView