Sachitha Ilayperuma
Sachitha Ilayperuma

Reputation: 43

Is there a way in flutter to generate Widgets in a List without using Listview , Gridview?

What I want to do is to put my Widgets in the List to a Stack. Every widget in the list will return a container with different alignment.

Container( alignment: Alignment(xValue,yValue))

My stack is as Follows:

stack(
children: <Widget>[
SizedBox(
width :MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Container(
color:Colors.greenAccent,
child: ListView.builder(
           physics: const NeverScrollableScrollPhysics(),
           itemCount: widgetList.length,
           itemBuilder(BuildContext contet, int index){
                return widgetList[index]; })
))])

Here even though my containers appear in the Stack their positions are not the positions I wanted. Their alignment is done inside the listtile. What i want is to align those containers in the widgetlist inside the SizedBox.I could get it done as follows.

stack(
   children: <Widget>[
        SizedBox(
            width :MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: Container( color: Colors.greenAccent)),
        widgetList[0],
        widgetList[1], ])

This way even if could get the widgets in the list to be at the need location it is not dynamic. I need help to do this dynamically.

Upvotes: 1

Views: 1559

Answers (1)

Kedar Karki
Kedar Karki

Reputation: 538

Use the spread operator

Stack(
   children: <Widget>[
        SizedBox(
            width :MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: Container( color: Colors.greenAccent)),
        ...widgetList,    // Note the three dots before the widgetList
])

Upvotes: 2

Related Questions