Bloc: BlocBuilder inside ExpansionTile

Created on 2 May 2019  路  5Comments  路  Source: felangel/bloc

Hi,
I'm trying to create a dynamic Expansiontile list for my drawer. But I get the following error:
The following assertion was thrown during performResize():, Vertical viewport was given unbounded height. I think I making a small error but I just started with flutter and cannot figure out.
Here is my code:

      ExpansionTile(
              title: Text("Normal Odds"),
              initiallyExpanded: false,
              children: <Widget>[
                BlocBuilder(
                  bloc: _drawtypeBloc,
                  builder: (BuildContext context, DrawtypeState state) {
                    if (state is Loading) {
                      print("Loading");
                      return ListTile(
                        title: Text("data"),
                      );
                    }
                    if (state is Loaded) {
                      return ListView.builder(
                        itemCount: state.types.norm.length,
                        itemBuilder: (BuildContext context, int index) {
                          return ListTile(
                            title: Text(state.types.norm[index]),

                          );
                        },
                      );
                    }
                  },
                ),
              ]),

And here is a static example of what I would like to achieve:

ExpansionTile(
            title: Text("Low Odds"),
            children: <Widget>[
              ListTile(
                title: Text("ELITE 3"),
                leading: Icon(Icons.arrow_right),
              ),
              ListTile(
                leading: Icon(Icons.arrow_right),
                title: Text("DOUBLE 1"),
              ),
              ListTile(
                title: Text("DOUBLE OR NOTHING"),
                leading: Icon(Icons.arrow_right),
              )
            ],
          ),
question

Most helpful comment

I found a solution, it works now, but maybe not the best :)

       children: List<Widget>.unmodifiable(() sync* {
                  if (state is Loading) yield ListTile(title: Text("Loading"));
                  if (state is Loaded){
                    for (var i = 0; i < state.types.norm.length; i++) {
                      yield  ListTile(title: Text(state.types.norm[i]));
                    }
                  }
                }()),

All 5 comments

Hi @MatyasK 馃憢
Thanks for opening an issue.

Regarding your question you can do something like:

BlocBuilder(
        bloc: _drawtypeBloc,
        builder: (BuildContext context, DrawtypeState state) {
          return ExpansionTile(
            title: Text("Normal Odds"),
            initiallyExpanded: false,
            children: List<Widget>.unmodifiable(() sync* {
              if (state is Loading) yield ListTile(title: Text("Loading"));
              // yield other ListTiles based on state
            }()),
          );
        },
      ),

Hope that helps 馃憤

Hi thank you very much for your answer, it works finally, but my next noob question is how can yield multiple tiles? I tried it from a foreach loop but it doesn't work.

Thanks again!

Hey no problem! Can you show me the code that you tried and the error you're running into? Thanks!

I found a solution, it works now, but maybe not the best :)

       children: List<Widget>.unmodifiable(() sync* {
                  if (state is Loading) yield ListTile(title: Text("Loading"));
                  if (state is Loaded){
                    for (var i = 0; i < state.types.norm.length; i++) {
                      yield  ListTile(title: Text(state.types.norm[i]));
                    }
                  }
                }()),

That looks like a solid solution to me 馃憤

Was this page helpful?
0 / 5 - 0 ratings