Reputation: 10923
I am trying to set up a search page in a Flutter app. This widget presents a search field, monitors what the user is typing, and updates a list of suggested hits with every change in the search field. The relevant snippet is here:
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,
)
],
);
}
This widget crashes with an error message that is pages long, but includes this text that suggests my error is a layout issue. I am new to Flutter and Dart and unable to really apply the advice here meaningfully to my problem.
══╡ 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.
In the snippet below, I try to simulate the effect I want by presenting a succession of Text widgets:
@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('...'),
],
);
}
This works perfectly but looks ugly and is clearly a hack that I do not want in the final app. Also I have confirmed that my logic is working perfectly -- I have checked that suggestList
is always populated with at least one Text
widget suitable for display. Any ideas on how to repair the ListView approach?
Upvotes: 1
Views: 2123
Reputation: 51682
One way is to just build the Column children like this
@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,
),
);
}
or better still just keep the suggestList as a 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());
Alternatively, wrap the ListView in an 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,
),
),
],
),
This allows an infinite scrolling list below a fixed Form.
Upvotes: 2