简体   繁体   English

Flutter:水平使用无限滚动 ListView

[英]Flutter: Using Infinite Scroll ListView Horizontally

I am using the flutter package infiniteListView in order to get a horizontal list of infinitely scrolling list of days that users can click on.我正在使用 flutter 包infiniteListView以获得用户可以点击的无限滚动天数的水平列表。

This is the following error I am getting这是我收到的以下错误

The following assertion was thrown during performLayout():
RenderFlex children have non-zero flex but incoming height constraints are unbounded.
When a column is in a parent that does not provide a finite height constraint, for example 
if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting aflex
on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
space in the vertical direction.

Here is my code这是我的代码

 Widget build(BuildContext context) {
    return Container(
        // Leave margin here for top bar
        color: Colors.grey[900],
        padding: EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 5.0),
        child: (Column(children: [
     Expanded(
            child: InfiniteListView.builder(
                scrollDirection: Axis.horizontal,
                controller: _infiniteController,
                anchor: 0.5,
                itemBuilder: (BuildContext context, int index) {
                  return Material(
                    child: InkWell(
                      
                      onTap: () {},
                      child: ListTile(
                        
                        title: Text('Item #$index'),
                        subtitle: Text('Subtitle $index'),
                        trailing: const Icon(Icons.chevron_right),
                      ),
                    ),
                  );
                }),
          ),

        ])));
  }

wrap your ListTile with Fixed width, ListTile by default takes full width(something like double.infinity ).用固定宽度包裹你的ListTile ,默认情况下ListTile采用全宽(类似于double.infinity )。 also our Axis.horizontal, will take double.infinity , and this is where errors come.我们的Axis.horizontal,也将采用double.infinity ,这就是错误的来源。

  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        // Leave margin here for top bar
        color: Colors.grey[900],
        padding: EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 5.0),
        child: (Column(mainAxisSize: MainAxisSize.min, children: [
          Expanded(
          
            child: InfiniteListView.builder(
              // itemCount: 222,
              scrollDirection: Axis.horizontal,
              controller: _infiniteController,
              anchor: 0.5,
              itemBuilder: (BuildContext context, int index) {
                return Material(
                  child: InkWell(
                    onTap: () {},
                    child: SizedBox(
                      width: 100,
                      child: ListTile(
                        title: Text('Item #$index'),
                        subtitle: Text('Subtitle $index'),
                        trailing: const Icon(Icons.chevron_right),
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ])),
      ),
    );
  }

Solution 1:解决方案1:

  • Column has Unbounded Constriants (Infinite Height).列具有无界约束(无限高)。 Wrap your InfiniteLiewView into a SizedBox(height 300: child: //InfiniteListView);将你的 InfiniteLiewView 包装成一个 SizedBox(height 300: child: //InfiniteListView);

Solution 2:解决方案2:

  • If that does not work, Pass shrinkWrap: true to InfiniteLiewView,如果这不起作用,则将 shrinkWrap: true 传递给 InfiniteLiewView,

Solution 3:解决方案3:

  • Wrap you Column into a SizedBox(height 400, child: //code) .将你的 Column 包装成一个 SizedBox(height 400, child: //code) 。

Try this out, Let me know then.试试这个,然后让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM