我正在尝试在Flutter应用中设置搜索页面。此小组件显示搜索字段,监视用户正在键入的内容,并在搜索字段中的每个更改时更新建议的匹配列表。相关的片段在这里:
List<Text> suggestList = [new Text('')];
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
new Form(
child: new TextFormField(
// _controller listens for changes and updates suggestList
controller: _controller,
autocorrect: false,
decoration: InputDecoration(labelText: 'Search here'),
onSaved: (str) => print(str),
),
),
new ListView.builder(
itemBuilder: (context, index) => suggestList[index],
itemCount: suggestList.length,
)
],
);
}
此小部件崩溃时出现页面长的错误消息,但包含此文本表明我的错误是布局问题。我是Flutter和Dart的新手,无法将这些建议真正地应用于我的问题。
══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown during performResize():
Vertical viewport was given unbounded height.
Viewports expand in the scrolling direction to fill their container.
In this case, a vertical
viewport was given an unlimited amount of vertical space in which to expand.
This situation
typically happens when a scrollable widget is nested inside another
scrollable widget.
If this widget is always nested in a scrollable widget there is no
need to use a viewport because
there will always be enough vertical space for the children.
In this case, consider using a Column
instead. Otherwise, consider using the "shrinkWrap" property
(or a ShrinkWrappingViewport) to size
the height of the viewport to the sum of the heights of its children.
在下面的代码片段中,我尝试通过呈现一系列Text小部件来模拟我想要的效果:
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
new Form(
child: new TextFormField(
controller: _controller,
autocorrect: false,
decoration: InputDecoration(labelText: 'Search here'),
onSaved: (str) => print(str),
),
),
// Present the two first items manually as Text widgets
suggestList[0],
suggestList.length > 1 ? suggestList[1] : new Text('...'),
],
);
}
这完美无缺,但看起来很丑陋,显然是我在最终应用中不想要的黑客。此外,我已经确认我的逻辑工作正常 - 我已经检查过suggestList
总是填充了至少一个适合显示的Text
小部件。关于如何修复ListView方法的任何想法?
答案 0 :(得分:1)
一种方法是像这样构建Column子项
@override
Widget build(BuildContext context) {
List<Widget> columnChildren = [
new Form(
child: new TextFormField(
// _controller listens for changes and updates suggestList
//controller: _controller,
autocorrect: false,
decoration: InputDecoration(labelText: 'Search here'),
onSaved: (str) => print(str),
),
)
];
columnChildren.addAll(suggestList);
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Column(
children: columnChildren,
),
);
}
或更好仍然只是将suggestList保持为List
List<String> suggestList = [
'hello',
'world',
];
@override
Widget build(BuildContext context) {
List<Widget> columnChildren = [
new Form(
child: new TextFormField(
// _controller listens for changes and updates suggestList
//controller: _controller,
autocorrect: false,
decoration: InputDecoration(labelText: 'Search here'),
onSaved: (str) => print(str),
),
)
];
columnChildren.addAll(suggestList.map((s) => new Text(s)).toList());
或者,将ListView包装在Expanded
中 body: new Column(
children: <Widget>[
new Form(
child: new TextFormField(
// _controller listens for changes and updates suggestList
//controller: _controller,
autocorrect: false,
decoration: InputDecoration(labelText: 'Search here'),
onSaved: (str) => print(str),
),
),
new Expanded(
child: new ListView.builder(
itemBuilder: (context, i) => new Text(wordList[i]),
itemCount: wordList.length,
),
),
],
),
这允许在固定表单下方的无限滚动列表。