Home > OS >  how to duplicate elements in a list in flutter?
how to duplicate elements in a list in flutter?

Time:08-03

Does anyone know how I can make a list of items such that one row has 1 item, the second row has 2 items, and the third row has 3? You need to duplicate elements in a row. As in the screenshot, one zipper, then on the next row 2 zippers, on the next row 3 zippers.

Need to do

enter image description here

CodePudding user response:

Something like this?:

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          for (int i in [1, 2, 3])
            Row(children: [
              for (var j = 1; j <= i; j  ) const Icon(Icons.lightbulb)
            ])
        ],
      ),
    );
  }
}

Output:

enter image description here

CodePudding user response:

For Random zipper you may try out below code

ListView.builder(
          itemCount: 13,
          shrinkWrap: true,
          itemBuilder: (context, index) {
            return Row(children: [
              for (var j = 0, i = 0;
                  j <= 10 && i <= Random().nextInt(5);
                  j  , i  )
                const Icon(Icons.airplanemode_on_rounded)
            ]);
          })

output:

enter image description here

  • Related