Mohamad Alkousi
Mohamad Alkousi

Reputation: 33

How to use loop and array with widget? Flutter

I'm making an app that contains ViewList that have some Widget Inside it,

the Widget has functions And three Inputs (text title, text SubTitle, button with action 'OnPressed' it will change every Widget)

I need to duplicate this Widget 42 Times and every Widget has a different (title, subtitle, button) so how I can make a loop that duplicates the Widgets and Array to enter a specific (title, subtitle, button) to each Widget?

(more Explanation) array list Contains(title, subtitle, button), every time the loop creates a new Widget in the ViewList, the Widget Get's (title, subtitle, button) from the Arraylist.

I had done that before but not in flutter not even using dart so I'm a little bit confused

this picture explains what I need press here

Upvotes: 1

Views: 1621

Answers (1)

Jaime Ortiz
Jaime Ortiz

Reputation: 1309

This uses a for loop to loop through a the lists of lists containing the information for your widgets, and add each element of the list to a text widget, you just have to make sure the elements of each list are the correct type that you have to pass to the widget.

@override
  Widget build(BuildContext context) {
    List lists = [
      ['title', 'subtitle', 'button'],
      ['title', 'subtitle', 'button'],
      ['title', 'subtitle', 'button'],
    ];

    return MaterialApp(
      title: MyApp._title,
      home: Scaffold(
          appBar: AppBar(title: const Text(MyApp._title)),
          body: SingleChildScrollView(
            child: Column(
              children: [
                for (var i in lists)
                  Card(
                    child: Column(
                      children: [
                        Text(i[0]),
                        Text(i[1]),
                        Text(i[2]),
                      ],
                    ),
                  )
              ],
            ),
          )),
    );
  }
}

Upvotes: 1

Related Questions