harunB10
harunB10

Reputation: 5207

Flutter - Container width and height fit parent

I have a Card widget which includes Column with several other Widgets. One of them is Container.

Widget _renderWidget() {
    return Column(
      children: <Widget>[
        Visibility(
          visible: _isVisible,
          child: Container(
              width: 300,
              height: 200,
              decoration:
                  BoxDecoration(border: Border.all(color: Colors.grey)),
              child: ListView.builder(
                itemCount: titles.length,
                itemBuilder: (context, index) {
                  return Card(
                    child: ListTile(
                      leading: Icon(icons[index]),
                      title: Text(titles[index]),
                    ),
                  );
                },
              )),
        ),
      ],
    );
  }

Here, if I don't specify width and height I get error. But, is there a way to make them to fit parent element?

EDIT

Full code:

return ListView(
      children: <Widget>[
        Center(
          child: Container(
            child: Card(
                margin: new EdgeInsets.all(20.0),
                child: new Container(
                  margin: EdgeInsets.all(20.0),
                  width: double.infinity,
                  child: Column(
                    children: <Widget>[
                      FormBuilder(
                        key: _fbKey,
                        autovalidate: true,
                        child: Column(
                          children: <Widget>[
                            FormBuilderDropdown(

                             onChanged: (t) {
                                setState(() {
                                  text = t.vrsta;
                                });
                              },
                              validators: [FormBuilderValidators.required()],
                              items: user.map((v) {
                                return DropdownMenuItem(
                                    value: v,
                                    child: ListTile(
                                      leading: Image.asset(
                                        'assets/img/car.png',
                                        width: 50,
                                        height: 50,
                                      ),
                                      title: Text("${v.vrsta}"),
                                    ));
                              }).toList(),
                            ),
                          ],
                        ),
                      ),
                      Container(child: _renderWidget()),
                      text.isEmpty
                          ? Container()
                          : RaisedButton(
                              child: Text("Submit"),
                              onPressed: () {
                                _fbKey.currentState.save();
                                if (_fbKey.currentState.validate()) {
                                  print(_fbKey.currentState.value.toString());
                                  print(formData.startLocation);
                                  getAutocomplete();
                                }
                              },
                            )
                    ],
                  ),
                )),
          ),
        ),
      ],
    );
  }

Upvotes: 3

Views: 43249

Answers (2)

CopsOnRoad
CopsOnRoad

Reputation: 267554

You are using ListView inside Column which is why you need to specify height in your Container to prevent errors.

The best way is to use Expanded or Flexible as parent of your ListView

Expanded(
  child: ListView.builder(...)
)

Update:

Widget _renderWidget() {
  return Column(
    children: <Widget>[
      Visibility(
        visible: _isVisible,
        child: Expanded(
          child: ListView.builder(
            itemCount: titles.length,
            itemBuilder: (context, index) {
              return Card(
                child: ListTile(
                  leading: Icon(icons[index]),
                  title: Text(titles[index]),
                ),
              );
            },
          ),
        ),
      ),
    ],
  );
}

Upvotes: 4

Son of Stackoverflow
Son of Stackoverflow

Reputation: 1679

Just set the height and width property of your Container to double.infinity

Solution Code:

Widget _renderWidget() {
    return Column(
      children: <Widget>[
        Visibility(
          visible: _isVisible,
          child: Container(
              width: double.infinity,
              height: double.infinity,
              decoration:
                  BoxDecoration(border: Border.all(color: Colors.grey)),
              child: ListView.builder(
                itemCount: titles.length,
                itemBuilder: (context, index) {
                  return Card(
                    child: ListTile(
                      leading: Icon(icons[index]),
                      title: Text(titles[index]),
                    ),
                  );
                },
              )),
        ),
      ],
    );
  }

Upvotes: 5

Related Questions