1

I am trying to display a row of buttons. Since the number of buttons depends on the number of elements in a List, I have to use GridView.builder to dynamically create the right amount of buttons. Unfortunately it seems that GridView.builder is taking up alot of unnecessary space. Anyone know what is wrong here?

body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // Dice buttons
            Flexible(
                child: GridView.builder(
                    itemCount: dices.length,
                    gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: dices.length,
                    ),
                    itemBuilder: (BuildContext context, int index) {
                      return new Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            ButtonTheme(
                              minWidth: 50.0,
                              height: 50.0,
                              child: RaisedButton(
                                child: Text(
                                  dices[index].toString(),
                                  style: TextStyle(
                                    fontSize: 20,
                                    color: Colors.white,
                                  ),
                                ),
                                color: usedDices[index] || !expectNum
                                    ? Colors.grey
                                    : Colors.black,
                                onPressed: () {
                                  if (!usedDices[index] && expectNum) {
                                    setState(() {
                                      numUsed[turn] = dices[index].toString();
                                      numUsedIndex[turn] = index;
                                    });
                                    expectNum = false;
                                    usedDices[index] = true;
                                  }
                                },
                              ),
                            ),
                          ]);
                    })),

Link to Pic: https://drive.google.com/file/d/1_Jr4rz9GJ-D8-Xjxs2w8Sn8lOdnBBqTc/view?usp=sharing

As you can see are lots of unnecessary space here and it seems to be the reuslt of GridView.builder

2 Answers 2

1

That space is the result of Flexible, which fills the available space. You will see that if you replace it with a Container and give it a height, it won't produce that much space below.

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

1 Comment

Thanks for the response! Switching Flexible to Container indeed helped with the issue. Another problem that i'm running into is that since the size of my List is changing, the number of buttons shown will change as well. The problem is the position of the buttons goes downwards as the number of buttons decreases. I want the buttons to be in the same row regardless of how many buttons are being displayed. Would you happen to know what is wrong here?
0

Just had this problem, top SliverPadding is set to 20.0 by default. Looking at docs I see:

/// By default, [ListView] will automatically pad the list's scrollable
/// extremities to avoid partial obstructions indicated by [MediaQuery]'s
/// padding. To avoid this behavior, override with a zero [padding] property.

So,

  GridView.builder(
              // shrinkWrap: true,
              padding: EdgeInsets.zero,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisSpacing: 1,
                mainAxisSpacing: 1,
                crossAxisCount: 3,
              ),
              itemBuilder: (context, index) {
                return Container(
                  color: Colors.black,
                );
              },
              itemCount: 10,
            ),

or If you put a widget before the ListView, you should wrap the ListView with a

MediaQuery.removePadding widget (with removeTop: true)

Comments

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.