ExpansionTile 内的 ListView 不起作用

标签 listview dart flutter flutter-sliver

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): 

最佳答案

以上解决方案均未按预期工作。所以我已经解决了这个问题。您需要在 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]);
                      }
                   )
                      
                    ],
                  ),

关于ExpansionTile 内的 ListView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54152176/

相关文章:

安卓 ListView : how to notify datasource when delete one row?

qt - QML 在 ListView 中显示的相机照片被破坏

c# - LargeImages 到 WPF ListView 端口

具有自动处置功能的 Flutter RiverPod StateProvider 在导航到下一个屏幕时不会处置更新的值

android - 如何在没有微调器的情况下像微调器一样弹出列表?

dart - 使用 getter 和 setter 从 1-7 个整数创建字符串

firebase - 上传 Set<String> 会导致 flutter 出错

firebase - Face 对象的属性在 firebase ml vision flutter 中始终为 null

flutter - ListView.Builder 中的 cacheExtent 属性到底有什么作用?

flutter - 如何在 flutter 中重用有状态的小部件