I was wondering how can I print/display text of random item from the list? Here is the code of the list i have
class TaskData extends ChangeNotifier {
List<Task> _tasks = [
Task(name: "Task one"),
Task(name: "Task two"),
Task(name: "Task three"),
];
UnmodifiableListView<Task> get tasks {
return UnmodifiableListView(_tasks);
}
int get taskCount {
return _tasks.length;
}
Here I have different file with buttons and i managed to get some code to just test it out (Found online) but it only displays second task, doesn't randomize not sure why.
class TasksScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
List yourList = ["first item", "second item", "third item"];
int randomIndex = Random().nextInt(yourList.length);
return Scaffold(
backgroundColor: Colors.lightBlueAccent,
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton.extended(
label: const Text("Randomize"),
icon: const Icon(Icons.touch_app),
backgroundColor: Colors.lightBlueAccent,
onPressed: () {
print(yourList[randomIndex]);
}),
Would really appreciate if someone could help me with this one Thank you!
Aucun commentaire:
Enregistrer un commentaire