RenderFlex 底部溢出了 41 个像素。导致错误的相关小部件是 Column

dev*_*996 6 dart flutter

我需要创建一个搜索框,其中包含搜索结果ListView.Builder,但我遇到了崩溃!我试图通过获取Container高度值来解决它MediaQuery.of(context).size.height,但给我带来了同样的问题!

\n\n
\n

\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 捕获异常通过渲染库 \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\ x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\ xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90

\n\n

布局期间抛出以下断言:RenderFlex 底部溢出 41 像素。

\n\n

导致错误的相关小部件为\n Column lib/pages/search.dart:78 溢出的 RenderFlex 的方向为 Axis.vertical。渲染中溢出的 RenderFlex 边缘已用黄色和黑色条纹图案标记。这通常是由于内容对于 RenderFlex 来说太大造成的。

\n\n

考虑应用弹性因子(例如使用 Expanded widget)来强制 RenderFlex 的子项适应可用空间,而不是调整其自然大小。这被视为错误情况,因为它表明存在无法看到的内容。如果内容确实大于可用空间,请考虑在将其放入 Flex 之前使用 ClipRect 小部件对其进行剪辑,或者使用可滚动容器而不是 Flex,例如 ListView。

\n\n

有问题的具体 RenderFlex 是: RenderFlex#1a63c\n relayoutBoundary=up1 OVERFLOWING\n \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2 \x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95 \x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2 \x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95 \x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2 \x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90

\n
\n\n

我的代码:

\n\n
body: Column(\n        children: <Widget>[\n          Container(\n              width: MediaQuery.of(context).size.width,\n              margin: EdgeInsets.all(10),\n              padding: EdgeInsets.only(right: 10),\n              decoration: BoxDecoration(\n                  color: Colors.white,\n                  border: new Border.all(\n                    color: Colors.grey,\n                    width: 1,\n                  ),\n                  borderRadius: BorderRadius.all(new Radius.circular(10))),\n              child: DropdownButton<String>(\n                value: dropdownValue,\n                icon: Icon(Icons.keyboard_arrow_down),\n                iconEnabledColor: Colors.grey,\n                iconSize: 50,\n                elevation: 16,\n                isExpanded: true,\n                style: TextStyle(color: Colors.black),\n                underline: Container(\n                  height: 2,\n                ),\n                onChanged: (String newValue) {\n                  setState(() {\n                    dropdownValue = newValue;\n                  });\n                },\n                items: <String>["Swift", "Dart"]\n                    .map<DropdownMenuItem<String>>((String value) {\n                  return DropdownMenuItem<String>(\n                    value: value,\n                    child: Text(value),\n                  );\n                }).toList(),\n              )),\n          Container(\n            margin: EdgeInsets.all(10),\n            child: Column(\n              children: <Widget>[\n                  TextField(\n                    obscureText: true,\n                    onChanged: (value) {\n                      filterSearchResults(value);\n                    },\n                    decoration: InputDecoration(\n                        filled: true,\n                        fillColor: Colors.white,\n                        focusedBorder: OutlineInputBorder(\n                            borderSide: const BorderSide(\n                                color: Color.fromRGBO(111, 192, 81, 1),\n                                width: 1.5),\n                            borderRadius: BorderRadius.circular(10)),\n                        contentPadding:\n                            EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),\n                        hintText: "Search",\n                        prefixIcon: new Icon(Icons.search, color: Colors.grey),\n                        enabledBorder: OutlineInputBorder(\n                          borderRadius: BorderRadius.all(Radius.circular(10)),\n                          borderSide: BorderSide(width: 1, color: Colors.grey),\n                        )),\n                  ),\n                ListView.builder(\n                  shrinkWrap: true,\n                  scrollDirection: Axis.vertical,\n                  itemCount: items.length,\n                  itemBuilder: (context, index) {\n                    return ListTile(\n                      title: Text(\'${items[index]}\'),\n                    );\n                  },\n                ),\n              ],\n            ),\n          ),\n        ],\n      ),\n
Run Code Online (Sandbox Code Playgroud)\n\n

问题是什么?我该如何解决它?

\n

Cra*_*Cat 10

您需要放置Expanded在两个地方

body: Column(
  children: <Widget>[
    Container(
      width: MediaQuery.of(context).size.width,
      margin: EdgeInsets.all(10),
      padding: EdgeInsets.only(right: 10),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(
          color: Colors.grey,
          width: 1,
        ),
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
      child: DropdownButton<String>(
        value: dropdownValue,
        icon: Icon(Icons.keyboard_arrow_down),
        iconEnabledColor: Colors.grey,
        iconSize: 50,
        elevation: 16,
        isExpanded: true,
        style: TextStyle(color: Colors.black),
        underline: Container(height: 2),
        onChanged: (String newValue) {
          setState(() {
            dropdownValue = newValue;
          });
        },
        items: <String>["Swift", "Dart"]
            .map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
      ),
    ),
    Expanded(  //TODO: Add Expanded here
      child: Container(
        margin: EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            TextField(
              obscureText: true,
              onChanged: (value) {
                filterSearchResults(value);
              },
              decoration: InputDecoration(
                filled: true,
                fillColor: Colors.white,
                focusedBorder: OutlineInputBorder(
                  borderSide: const BorderSide(
                    color: Color.fromRGBO(111, 192, 81, 1),
                    width: 1.5,
                  ),
                  borderRadius: BorderRadius.circular(10),
                ),
                contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
                hintText: "Search",
                prefixIcon: new Icon(Icons.search, color: Colors.grey),
                enabledBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                  borderSide: BorderSide(width: 1, color: Colors.grey),
                ),
              ),
            ),
            Expanded( //TODO: Add Expanded here
              child: ListView.builder(
                shrinkWrap: true,
                scrollDirection: Axis.vertical,
                itemCount: items.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text('${items[index]}'),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    ),
  ],
)
Run Code Online (Sandbox Code Playgroud)

  • 您能否也解释一下原因? (8认同)