Ad
How To Increment Index In A GridView In Flutter?
I have a GridView
that is as shown below. I am trying to section it out in smaller GridView
s and have a separator in between.
However instead of showing me a continuous list, the elements from 0-5 keep repeating. I am trying to continue the elements in batches of 6 where the first batch is 0-5 and the next is from 6-11 and so on..
This is my code:
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.separated(
separatorBuilder: (context, int) {
return Divider(color: Colors.black,);
},
// shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return GridView.count(
shrinkWrap: true,
crossAxisCount: 3,
childAspectRatio: 2.0,
children: List.generate(6, (index) {
return Center(
child: RaisedButton(
onPressed: (){},
color: Colors.greenAccent,
child: Text(
'$index AM',
),
),
);
}),
);
},
itemCount: 4,
));
}
This is what the GridView
looks like:
Ad
Answer
You can use the index from the itembuilder to know which group of 6 you're generating.
Change the name index
in itemBuilder
to itemBuilderIndex
like so:
itemBuilder: (BuildContext context, int itemBuilderIndex) {
And do this to get the calculated number you want:
'${(itemBuilderIndex * 6) + index} AM'
(6 being the number of items you have per segment)
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad