questionasker
questionasker

Reputation: 2697

flutter - how to random widget sequence/order

Let's say I have below widget:

ListView(
   physics: NeverScrollableScrollPhysics(),
   shrinkWrap: true,
   children: <Widget>[

     this.widgetA();
     this.widgetB();
     this.widgetC();

     this.widgetX();
     this.widgetY();
     this.widgetZ();

   ]
)

Is there a way to random the order of widget X, Y and Z, so the order will be Y, Z, X or Z, X, Y while widget A, B, C remain the same (no random).

Is this possible?

===Update

What's this warning means?

enter image description here

in ...randomOrderedWidgets,

Upvotes: 0

Views: 1762

Answers (1)

10100111001
10100111001

Reputation: 1832

This doesn't really have anything to do with Flutter but rather Dart. You can create a new list called randomOrderedWidgets, shuffle it, and then use the spread operator or concatenation of the lists to include it as part of the ListView children.

final randomOrderedWidgets = [
    this.widgetX(),
    this.widgetY(),
    this.widgetZ()
];
randomOrderedWidgets.shuffle();

ListView(
   physics: NeverScrollableScrollPhysics(),
   shrinkWrap: true,
   children: <Widget>[
     this.widgetA(),
     this.widgetB(),
     this.widgetC(),
   ]..addAll(randomOrderedWidgets)
)

Upvotes: 3

Related Questions