ExpansionTile 内的 ListView 不起作用

Posted

技术标签:

【中文标题】ExpansionTile 内的 ListView 不起作用【英文标题】:ListView inside ExpansionTile doesn't work 【发布时间】:2019-06-06 17:01:45 【问题描述】:

listview 在 ExpansionTile 中不起作用

我试图在 ExpansionTile 中显示一个 listview.builder,但它会引发一些异常

Card(
       child: ExpansionTile(
           leading: Icon(
              Icons.stars,
              color: Colors.pinkAccent,
           ),
           title: Text('Reviews',
                  style: Theme.of(context).textTheme.title,
                          ),
                children: [
                    ListView.builder(
                        itemCount: 5,
                        itemBuilder: (_, i) 
                          return (Text('item $i'));
                     )
                   ],
             ),
     ),

我想实现这样https://imgur.com/a/0lubABC

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

【问题讨论】:

ListView.builder中添加shrinkWrap: true, 【参考方案1】:

ListView 的垂直大小是无限的,因此无法渲染。将ListViewshrinkWrap 属性设置为true 以解决此问题。

【讨论】:

感谢它的工作,但每次我滚动ExpansionTile ```` [ERROR:flutter/shell/common/shell.cc(184)] Dart 错误:未处理的异常:E/颤振(8182):'package:flutter/src/widgets/page_view.dart':断言失败:第 96 行 pos 7:'positions.isNotEmpty':PageController.page 在构建 PageView 之前无法访问。 E/flutter (8182): #0 _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39) E/flutter (8182): #1 _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart: 36:5) ``` 这又是个例外[ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception: E/flutter ( 8182): 'package:flutter/src/widgets/page_view.dart': Failed assertion: line 96 pos 7: 'positions.isNotEmpty': PageController.page cannot be accessed before a PageView is built with it. E/flutter ( 8182): #0 _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39) E/flutter ( 8182): #1 _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5) 这一定是你的 UI 中的其他内容。我在您的代码中的任何地方都看不到PageView【参考方案2】:

将收缩包装和控制器都添加到 ListView.builder

final ScrollController _scrollController = ScrollController();


    ListView.builder(
          controller: _scrollController,
           shrinkWrap: true,
    ...

【讨论】:

【参考方案3】:

上述解决方案均未按预期工作。所以我已经解决了这个问题。您需要在 ExpansionTile 子项中的 Listview.builder() 中使用它:

shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),

完整代码:

                ExpansionTile( 
                    onExpansionChanged: (e)
                      //Your code
                    ,
                    title: Text("You title text"),
                    children: [
                    ListView.builder(
                     shrinkWrap: true,
                     physics: NeverScrollableScrollPhysics(),
                     scrollDirection: Axis.vertical,
                     itemCount: items.length,
                     itemBuilder: (BuildContext context, int index)
                     return Text(items[index]);
                      
                   )
                      
                    ],
                  ),

【讨论】:

@RudyRudy 很高兴为您提供帮助 :)【参考方案4】:

解决在ListView 参数中使用shrinkWrap: true,

【讨论】:

以上是关于ExpansionTile 内的 ListView 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

颤振| ExpansionTile属性“children”在扩展时抛出了Bottous Overflowed问题

只允许展开一个 ExpansionTile

修改默认的 ExpansionTile 子项垂直填充 Flutter

7-3 基于ExpansionTile实现可展开的列表

ListView Flutter 中的 ListViews

expansiontile 打开关闭楼层