简体   繁体   English

ListView 不在定位的小部件中滚动

[英]ListView not scrolling in positioned Widget

I currently have a stack with a backdrop for my AppBar and the contents of my page.我目前有一个堆栈,其中包含我的AppBar和页面内容的背景。
I'm displaying a Container in the center of the page, positioned with the Positioned widget, which contains a grid/listview.我在页面中央显示一个Container ,使用Positioned小部件Positioned ,其中包含一个网格/列表视图。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: VehicleScreenAppBar(
        title: title,
        context: context,
      ),
      body: LayoutBuilder(
        builder: (contxt, vehicleListConstraints) {
          return Stack(
            overflow: Overflow.visible,
            children: <Widget>[
              AppBarBackDrop(constraints: vehicleListConstraints),
              Positioned(
                top: vehicleListConstraints.maxHeight * .15,
                left: (vehicleListConstraints.maxWidth - vehicleListConstraints.maxWidth * .9) / 2,
                child: Container(
                  color: Colors.red,
                  height: vehicleListConstraints.maxHeight * .6,
                  width: vehicleListConstraints.maxWidth * .9,
                  child: ListView.builder(
                    itemCount: 20,
                    itemBuilder: (context, i) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          height: 50,
                          color: Colors.blue,
                          child: Text('text'),
                        ),
                      );
                    },
                  ),
                ),
              )
            ],
          );
        },
      ),
    );
  }

截屏

The problem that I am running into is that the grid/listview do not scroll when I have Positioned widget AND I have explicitly named any BUT NOT ALL of its constructors, ie top: , bottom: , left: , right: .我遇到的问题是,当我Positioned小部件并且我已经明确命名了任何但不是所有的构造函数时,网格/列表视图不会滚动,即top: , bottom: , left: , right: (I'm lazily building the grid/listview lazily via builder). (我正在通过构建器懒惰地构建网格/列表视图)。

I have a work around/hack where I remove the Positioned and replace it with a PageView .我有一个解决方法/黑客,我删除了Positioned并将其替换为PageView The PageView then has a children: <Widget> [ Container() ] that I then position via the margin constructors' top: and left: .然后PageView有一个children: <Widget> [ Container() ]然后我通过边距构造函数的top:left:

This will work for now, but not something that I want to implement for production.这现在可以使用,但不是我想为生产实现的东西。 How can I get the grid/listview to scroll within a Positioned widget WITHOUT naming all of its constructors?如何让网格/列表视图在Positioned小部件内滚动而不命名其所有构造函数?

For scroll-able widgets wrapped inside the positioned widget, you should give all the parameters (left, right , top , bottom) of Positioned widget a value then it will work.对于包裹在定位小部件内的可滚动小部件,您应该给定位小部件的所有参数(左、右、上、下)一个值,然后它就会起作用。

 Positioned(
              top: 20.0,
              left: 20.0,
              right:0.0,
               bottom:0.0

              child: SizedBox(
              //what ever code is)),
                )
                  )

This display the Container in the center of the page这将在页面中央显示Container

 Positioned( top: 0.0, left: 0.0, right: 0.0, bottom: 0.0, child: //your code )

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

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