Flutter-容器宽度和高度适合父级

时间:2019-09-01 16:55:06

标签: flutter dart flutter-layout

我有一个Card小部件,其中包括Column和其他几个小部件。其中之一是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]),
                    ),
                  );
                },
              )),
        ),
      ],
    );
  }

在这里,如果我未指定widthheight,则会出错。但是,有没有办法使它们适合父元素?

编辑

完整代码:

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();
                                }
                              },
                            )
                    ],
                  ),
                )),
          ),
        ),
      ],
    );
  }

2 个答案:

答案 0 :(得分:0)

您在ListView内使用Column,这就是为什么需要在Container中指定高度以防止错误的原因。

最好的方法是使用ExpandedFlexible作为ListView的父对象

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

更新:

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]),
                ),
              );
            },
          ),
        ),
      ),
    ],
  );
}

答案 1 :(得分:-1)

只需将容器的var icons = ['js', 'json', 'css', 'sass', 'less', 'html', 'jpg', 'png', 'gif', 'bmp', 'svg', 'raw', 'rs', 'ts'] var container = document.querySelector('.icons') function spawn() { var icon = icons[Math.floor(Math.random() * icons.length)] var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg') svg.style.left = 25 + Math.random() * 50 + '%' svg.setAttribute('class', 'icon') var use = document.createElementNS('http://www.w3.org/2000/svg', 'use') use.setAttributeNS('http://www.w3.org/1999/xlink', 'href', 'assets/icons.svg#' + icon) svg.appendChild(use) container.appendChild(svg) setTimeout(function() { container.removeChild(svg) }, 3000) } setTimeout(function run() { spawn() setTimeout(run, 500 + Math.random() * 400) }, 500 + Math.random() * 400) spawn() height属性设置为width

解决方案代码:

double.infinity