Shuffle the items in a list every time you open the app

  1. I’m working on making a card game however at the moment every time you open the app, the cards are always in the same order.
  • My goal is to have those cards be in a complete random order every time the app is launched
  • I am extremely new to coding and have a very basic understanding of what I am doing so any help would be greatly appreciated.

Here’s a bit of the code from the area I’m assuming the “shuffle” command would need to be:


  List<String> vehicleNames = [
    'Draw 4',
    'Give 2',
    'Skip turn',
    'Everyone Draw',
    'Point to the sky',
    'Pick a date',
    ];

Try shuffle() method on your List object:

List vehicleNames = [
‘Draw 4’,
‘Give 2’,
‘Skip turn’,
‘Everyone Draw’,
‘Point to the sky’,
‘Pick a date’,
];
// before shuffle
print(vehicleNames);
//shuffling
vehicleNames.shuffle();
// after the shuffle
print(vehicleNames);
//shuffling
vehicleNames.shuffle();
// after the shuffle
print(vehicleNames);

1 Like

Hasn’t worked yet, I’ll share a bit more code, maybe it’ll help diagnose why.

""class _MyHomePageState extends State with TickerProviderStateMixin {

List vehicleNames = [
‘Draw 4’,
‘Give 2’,
‘Skip turn’,
‘Everyone Draw’,
‘Point to the sky’,
‘Pick a date’,
];

//before shuffle
print(vehicleNames)

//shuffling
vehicleNames.shuffle();

//after the shuffle

String currentVehicleName = ‘’;

double scrollPercent = 0.0;
Offset startDrag;
double startDragPercentScroll;
double finishScrollStart;
double finishScrollEnd;
AnimationController finishScrollController;

@override
initState() {
super.initState();

finishScrollController = AnimationController(
  duration: Duration(milliseconds: 150),
  vsync: this,
)
  ..addListener(() {
    setState(() {
      scrollPercent = lerpDouble(finishScrollStart, finishScrollEnd,
          finishScrollController.value);
    });
  });

}

@override
dispose() {
finishScrollController.dispose();
super.dispose();
}

List buildCards() {
List cardsList = [];
for (int i = 0; i < vehicleNames.length; i++) {
cardsList.add(buildCard(i, scrollPercent));
}
return cardsList;
}

Widget buildCard(int cardIndex, double scrollPercent) {
final cardScrollPercent = scrollPercent / (1 / vehicleNames.length);

return FractionalTranslation(
  translation: Offset(cardIndex - cardScrollPercent, 0.0),
  child: Padding(
    padding: EdgeInsets.all(8.0),
    child: ImageCard(imageName: vehicleNames[cardIndex]),
  ),
);

}

onHorizontalDragStart(DragStartDetails details) {
startDrag = details.globalPosition;
startDragPercentScroll = scrollPercent;
}

onHorizontalDragUpdate(DragUpdateDetails details) {
final currentDrag = details.globalPosition;
final dragDistance = currentDrag.dx - startDrag.dx;
final singleCardDragPercent = dragDistance / context.size.width;

setState(() {
  scrollPercent = (startDragPercentScroll + (-singleCardDragPercent
      / vehicleNames.length)).clamp(0.0, 1.0 - (1 / vehicleNames.length));
});

}

onHorizontalDragEnd(DragEndDetails details) {
finishScrollStart = scrollPercent;
finishScrollEnd = (scrollPercent * vehicleNames.length).round()
/ vehicleNames.length;
finishScrollController.forward(from: 0.0);

setState(() {
  startDrag = null;
  startDragPercentScroll = null;
  currentVehicleName = '';
});

}
}""

This may be related to state. You should move this vehicleNames.shuffle(); inside initState();

2 Likes

Thank you so much, that worked!

1 Like

I am glad it helped!

1 Like