Home > OS >  How can i add Row children dynamically in flutter?
How can i add Row children dynamically in flutter?

Time:04-28

How can i add row children dynamically based on the input i get from the calling screen. Lets say i get five from the calling screen passed to the current Screen. How can i add 5 row children before i load the page.

return Scaffold(
  body: SafeArea(
    child: Column(
      children: [
        Row(
          children: [
            QuestionIconsWiget(size, 1),
            QuestionIconsWiget(size, 2),
            QuestionIconsWiget(size, 3),
            QuestionIconsWiget(size, 4),
          ],
        )
      ],
    ),
  ),
);


Expanded QuestionIconsWiget(Size size, int questionNo) {
      return Expanded(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Container(
                height: size.height * 0.02,
                decoration: BoxDecoration(
                  color: Colors.black,
                  shape: BoxShape.rectangle,
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
              SizedBox(
                height: size.height * 0.01,
              ),
              Text(questionNo.toString()),
            ],
          ),
        ),
      );
    }
  }

CodePudding user response:

// Create a list of Widget

List<Widget> widgets = [Text("1"), Text("2")];

// Replace it with the var

Row(children: widgets)

//On Button Press or Whatever Action

setState(() {
          widgets.add(Text("3"));
        });
  • Related