I'm trying to apply random colors to the list view generated widgets in flutter. here is the example.
this is a list view generated list , and different colors on each widget.
here is how I did that
ListView.separated(
physics: const BouncingScrollPhysics(),
shrinkWrap: true,
itemCount: docList.length,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) {
final data = docList[index];
return Row(
children: [
DocLIstCard(
// here is the color list to apply different colors to each of the generated widget.
backgroundColor: AppColors.gradientIndex[index],
category: data.sectionName,
docImage: data.image,
docName: data.name,
),
],
);
},
separatorBuilder: (BuildContext context, int index) {
return const SizedBox(width: 20);
},
),
and here is the AppColors.gradientIndex[index]'s variable
static const gradientIndex = [
LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFF111A8A), Color(0xFFB1008A)],
),
LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFFE97000), Color(0xFFB1008A)],
),
LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFF111A8A), Color(0xFFB1008A)],
),
LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFFE97000), Color(0xFFB1008A)],
),
];
if there are 1000 of widgets, I've to write the colors list 1000 times, so how can I loop this???

backgroundColor: AppColors.gradientIndex[index % AppColors.gradientIndex.length],gradientIndexlist20 % 4 = 0and0is not out of range of the list, nor will it be out of range with any other value for index.gradientIndexlist. To be honest, from reading the question I'm not 100% certain if what he wants is a random selection or a looped pattern.