简体   繁体   English

滚动时水平 ListView.builder 溢出宽度限制

[英]Horizontal ListView.builder overflow width bound when scroll

I am trying to use ListView.builder to display a horizontal list in a Stack but am encountering this weird bug when scrolling ListView if I set itemCount: 10 .我正在尝试使用 ListView.builder 在 Stack 中显示水平列表,但是如果我设置itemCount: 10则在滚动 ListView 时会遇到这个奇怪的错误。 If I set itemCount: 20 the ListView scrolls like normal.如果我设置itemCount: 20 ListView 滚动正常。

I have tested in the emulator (Galaxy Nexus 720x1280 android 5.0) and on a real device (Nokia 7 plus, android 9.0).我已经在模拟器(Galaxy Nexus 720x1280 android 5.0)和真实设备(诺基亚 7 plus,android 9.0)上进行了测试。 How can I fix this?我怎样才能解决这个问题?

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,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Expected output:预期输出:

Actual output:实际输出:

And a video of the problem以及问题视频

I tried your code and run with bigger intemcount too without problems, but i have an advice, change the我试过你的代码并且用更大的 intemcount 运行也没有问题,但我有一个建议,改变

SizedBox(width: 10.0)

for a padding around the button用于按钮周围的填充

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

在我更新到最新版本后,这个错误似乎出现在旧的颤振版本中,一切正常。

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

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