Search code examples
flutterflutter-layout

Flutter - Duplicate Widgets with an Action


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.


Solution

  • Create a function like this:

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

    and use it like this:

    Column(
      children: getWidgets(4),
    )