0

I want to display widget in a specific pattern. I try to use GridView, but it seems that GridView can only set one value at crossAxisCount. I want it to be 3, 2 pattern. I have refer to @chunhunghan answer at this, but the pattern is in 00, 111, 22, 333, 44, 555 pattern vertically. I want it to be 012, 34, 567, 89 pattern vertically. I have attached the expected output. Thank you.

 GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3, childAspectRatio: 1),
      itemCount: 10,
      itemBuilder: (context, index) {
        return Column(children: <Widget>[
          Icon(Icons.access_alarm,
              color: Colors.redAccent, size: 100.0),
          Text(index.toString())
        ],);
      })

Output Expected Output

3 Answers 3

3

modify the below code as per your requirement ! UPDATED

    int itemCount = 100, lastCount = 3, lastNum = 0;

@override
  void initState(){
    super.initState();
    itemCount++;
  }

      @override
      Widget build(BuildContext context) {
        return ListView.builder(
            itemCount: ((itemCount / 3 / 2) + (itemCount / 2 / 2)).toInt(),
            itemBuilder: (con, ind) {
              if (lastCount == 3) {
                lastCount = 2;

                //Add below condition to show exact pattern by avoiding extra numbers
                return (itemCount - lastNum) >= lastCount + 1
                    ? Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: List.generate(lastCount + 1, (cInd) {
                          lastNum++;

                          //Below condition to show upto itemCount by showing remaining numbers in next line ! It won't work if you use above formula. i.e Showing Exact Pattern
                          return lastNum < itemCount + 2
                              ? Column(mainAxisSize: MainAxisSize.min, children: [
                                  Icon(Icons.check_circle),
                                  Text('${lastNum - 1}')
                                ])
                              : SizedBox();
                        }))
                    : SizedBox();
              } else {
                lastCount = 3;

                //Add below condition to show exact pattern by avoiding extra numbers
                return (itemCount - lastNum) >= lastCount - 1
                    ? Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: List.generate(lastCount - 1, (cInd) {
                          lastNum++;

                          //Below condition to show upto itemCount by showing remaining numbers in next line ! It won't work if you use above formula. i.e Showing Exact Pattern
                          return lastNum < itemCount + 2
                              ? Column(mainAxisSize: MainAxisSize.min, children: [
                                  Icon(Icons.check_circle),
                                  Text('${lastNum - 1}')
                                ])
                              : SizedBox();
                        }))
                    : SizedBox();
              }
            });
      }

Pic

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you @Naveen Avidi! I will try to run the codes and get back to you.
Ok let me know ! I tested on dart pad !
Hi @Naveen Avidi! I have run the codes. It works dynamically on larger itemCount. Thank you very much for your help!
But, I have issues when I change the itemCount to numbers that is not in multiple of 5. I guess the above code only suitable for itemCount that is multiple of 5?
You have to change this line according to pattern==> itemCount: ((itemCount / 3 / 2) + (itemCount / 2 / 2)).toInt(),
|
0

Maybe you could just "pad" the rows with only 2 items with an empty Container() if that workaround works for you?

GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3, childAspectRatio: 1),
      itemCount: 10,
      itemBuilder: (context, index) {
        return Column(children: <Widget>[
          Icon(Icons.access_alarm,
              color: Colors.redAccent, size: 100.0),
          Text(index.toString()),
          Container()
        ],);
      })

or check if the index is odd or even then have two builder functions, one return 3 items and the other returning only 2

1 Comment

in your answer the empty container() is in the item so that would just add space (and maybe 0 space I'm not certain) to the bottom of each icon and number item. How do you get padding (and the right size padding) to both the left and right of each row of clocks?
0

Keep things simple and just use Row and Column with MainAxisAlignment.center. Here is a working DartPad and the code:

  itemBuilder(index) {
    return Column(children: [
      Icon(Icons.access_alarm, color: Colors.redAccent, size: 100.0),
      Text(index.toString())
    ]);
  }

  rowBuilder(start, end) {
    var length = end - start + 1;
    var itemArray = new List<Widget>(length);

    for (var index = 0; index < length; index++) {
      itemArray[index] = itemBuilder(start + index);
    }

    return Row(
        mainAxisAlignment: MainAxisAlignment.center, children: itemArray);
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      rowBuilder(0, 2),
      rowBuilder(3, 4),
      rowBuilder(5, 7),
      rowBuilder(8, 9),
    ]);
  }

2 Comments

Thank you @Brett Pontarelli! Actually, I want to use GridView because I want it to be dynamic. The solution that you suggest achieve the expected output. But how to make it dynamic without need to specify rowBuilder(0, 2), rowBuilder(3, 4), rowBuilder(5, 7), rowBuilder(8, 9)?
Let say the length of the item is 100, I don't want to specify manually the rowBuilder. I want it to be dynamic. Can you help me? I spend 2 weeks to find solution on this matter. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.