滚动时水平ListView.builder溢出宽度绑定

时间:2019-04-11 07:30:59

标签: dart flutter flutter-layout

我试图使用ListView.builder在堆栈中显示水平列表,但是如果我设置itemCount: 10,则在滚动ListView时遇到这个奇怪的错误。如果设置itemCount: 20,ListView会像平常一样滚动。

我已经在模拟器(Galaxy Nexus 720x1280 android 5.0)和真实设备(Nokia 7 plus,android 9.0)上进行了测试。我该如何解决?

class BugPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Stack(
          children: <Widget>[
            Positioned(
              left: 20.0,
              right: 20.0,
              height: 60.0,
              bottom: 70.0,
              child: Row(
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: 10, // Overflow when scroll.
                      itemBuilder: (BuildContext context, int index) {
                        return Padding(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 3.0,
                          ),
                          child: Container(
                            width: 40.0,
                            height: 40.0,
                            color: Colors.red,
                            child: Center(child: Text("$index")),
                          ),
                        );
                      },
                    ),
                  ),
                  SizedBox(width: 10.0),
                  FloatingActionButton(
                    backgroundColor: Colors.blue,
                    onPressed: () {},
                    child: new Icon(
                      Icons.add,
                      color: Colors.black,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

预期输出:

实际输出:

还有一个video of the problem

2 个答案:

答案 0 :(得分:1)

我尝试了您的代码,并以更大的intemcount运行,也没有问题,但是我有个建议,请更改

SizedBox(width: 10.0)

用于按钮周围的填充

Padding(
  padding: const EdgeInsets.only(left: 10),
  child: FloatingActionButton(
    backgroundColor: Colors.blue,
    onPressed: () {},
    child: new Icon(
      Icons.add,
      color: Colors.black,
    ),
  )
)

答案 1 :(得分:-1)

在我更新到最新版本后,似乎该错误以旧版本出现了。