【发布时间】:2020-04-16 20:49:41
【问题描述】:
我需要用ListView.Builder 的搜索结果创建一个搜索框,但是我崩溃了!我试图通过获取Container 高度值MediaQuery.of(context).size.height 来解决它,但遇到同样的问题!
════════ 渲染库捕获的异常═════════════════════
在布局期间抛出了以下断言:A RenderFlex 在底部溢出了 41 个像素。
相关的导致错误的小部件是 列 lib/pages/search.dart:78 溢出的 RenderFlex 具有 Axis.vertical 的方向。 RenderFlex 的边缘是 在渲染中已经用黄色和黑色标记了溢出 条纹图案。这通常是由于内容太大造成的 对于 RenderFlex。
考虑应用弹性因子(例如,使用扩展小部件) 强制 RenderFlex 的子级适应可用空间 而不是按照自然大小调整大小。这被认为是 错误条件,因为它表明有内容不能 可见。如果内容合法地大于可用的 空间,请考虑在放置之前使用 ClipRect 小部件对其进行裁剪 在 flex 中,或者使用可滚动容器而不是 Flex,例如 一个列表视图。
具体的 RenderFlex 是:RenderFlex#1a63c relayoutBoundary=up1 溢出 ══════════════════════════════════════════════════ ══════════
我的代码:
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: new Border.all(
color: Colors.grey,
width: 1,
),
borderRadius: BorderRadius.all(new 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(),
)),
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),
)),
),
ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${items[index]}'),
);
},
),
],
),
),
],
),
问题是什么,我该如何解决??
【问题讨论】:
-
您应该将您的 ListView 包装在 Expanded 小部件中。
-
@SlahLayouni 如果我这样做,请给出另一个问题!