Home > database >  Flutter - Duplicate Widgets with an Action
Flutter - Duplicate Widgets with an Action

Time:08-17

I need to replicate my widgets with an action in app.

Example : I have an Row, and this row create duplicate elements with directions differents

controller.turns.value == 1 ?
            Container(
              padding: const EdgeInsets.all(10),
              child: RotatedBox(
                quarterTurns: controller.rotation.value,
                child: Image(
                  image: const AssetImage('reference.png'),
                  height: (MediaQuery.of(context).size.height * 0.8) *
                      controller.size.value,
                  width: (MediaQuery.of(context).size.width * 0.8) *
                      controller.size.value,
                ),
              ),
            )

when turns.value == 2, i need 2 Containers, and with 3 == 3, 4 == 4, 5 == 5.

CodePudding user response:

Create a function like this:

List<Widget> getWidgets(int count) => List.generate(count, (_) => yourWidget);

and use it like this:

Column(
  children: getWidgets(4),
)

CodePudding user response:

You can create a list<Widget> calls containers. and make for loop like this:

for (var i = 0; i < controller.turns.value; i  ) {

            containers.add(Container(
              padding: const EdgeInsets.all(10),
              child: RotatedBox(
                quarterTurns: controller.rotation.value,
                child: Image(
                  image: const AssetImage('reference.png'),
                  height: (MediaQuery.of(context).size.height * 0.8) *
                      controller.size.value,
                  width: (MediaQuery.of(context).size.width * 0.8) *
                      controller.size.value,
                ),
              ),
            ),)
          }

then use containers in listview or column widget.

  • Related