列内的ListView导致“垂直视口被赋予无限制的高度”

Har*_*dia 3 android listview flutter flutter-sliver flutter-layout

我是新手,对布局有麻烦。

我想要这样的东西: 下方的粘性标题和滚动视图

粘性标头和其下方的滚动视图。我想到了使用带有两个子项的Column小部件-第一个是标题,第二个是ListView。

这是我的代码

 Widget build(BuildContext context) {
 return Material(
   elevation: 8.0,
   child: Column(
     crossAxisAlignment: CrossAxisAlignment.start,
     children: <Widget>[
       Padding(
         padding: const EdgeInsets.all(16.0),
         child: Text(
           title,
           style:
               Theme.of(context).textTheme.subhead.copyWith(fontSize: 18.0),
           textAlign: TextAlign.left,
         ),
       ),
       Divider(height: 4.0),
       ListView.builder(itemBuilder: (context, i) {
         return ListTile(
           title: Text("Title $i"),
           subtitle: Text("Subtitle $i"),
         );
       }),
     ],
   ),
 );
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误。

I/flutter ( 5725): ??? EXCEPTION CAUGHT BY RENDERING LIBRARY ??????????????????????????????????????????????????????????
I/flutter ( 5725): The following assertion was thrown during performResize():
I/flutter ( 5725): Vertical viewport was given unbounded height.
I/flutter ( 5725): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 5725): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 5725): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 5725): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 5725): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 5725): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 5725): the height of the viewport to the sum of the heights of its children.
Run Code Online (Sandbox Code Playgroud)

如消息中所述,我添加了使ListView的rinkleWrap为true的功能,但这也不起作用。

此布局的父级是一个包含各种此类布局页面的堆栈,所有这些布局页面都在后台被一个用户选择了。

我究竟做错了什么?

azi*_*iza 8

尝试ListView.builder使用FlexibleExpanded小部件包装:

Flexible(
            child: ListView.builder(
              itemBuilder: (context, i) {
                return ListTile(
                  title: Text("Title $i"),
                  subtitle: Text("Subtitle $i"),
                );
              },
            ),
          ),
Run Code Online (Sandbox Code Playgroud)

  • 它不起作用,现在我得到“RenderFlex 子项具有非零弯曲,但传入的高度约束是无界的。” (4认同)
  • 这对我有用,谢谢。我不知道为什么在 [How to build layouts](https://flutter.io/tutorials/layout/) 的 flutter 文档中没有提到灵活。我已经尝试了 ConstrainedBox、FittedBox、Expand 等所有东西...无论如何再次感谢您的帮助。 (2认同)