简体   繁体   English

Flutter:滚动小部件列表

[英]Flutter: Scrolling through widgets list

在此处输入图像描述

Is there any way in flutter, to only scroll through a certain number of widgets? flutter 中是否有任何方法只能滚动浏览一定数量的小部件?

Like in PlayStore, the smaller widgets can be scrolled to the next 3 widgets however hard or fast the screen is scrolled.就像在 PlayStore 中一样,较小的小部件可以滚动到接下来的 3 个小部件,无论屏幕滚动的速度有多快。 The same can be seen in the larger widgets, which can be scrolled through only one widget at a time?在较大的小部件中也可以看到相同的情况,一次只能滚动一个小部件?

You just need to think about the top slider as of PageView with each 3 children wrapped in their own parent block.您只需要考虑 PageView 的顶部 slider ,每 3 个孩子都包装在自己的父块中。

Here's an example:这是一个例子:

class SmallSlider extends StatefulWidget {
  @override
  _SmallSliderState createState() => _SmallSliderState();
}

class _SmallSliderState extends State<SmallSlider> {
  final PageController controller = PageController(viewportFraction: 0.9);

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 3 / 2,
      child: PageView.builder(
        controller: controller,
        itemCount: 5,
        itemBuilder: (context, index) => Row(
          children: <Widget>[
            for (int i = 0; i < 3; i++)
              Expanded(
                child: SmallSliderItem(),
              ),
          ],
        ),
      ),
    );
  }
}

class SmallSliderItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 5.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          AspectRatio(
            aspectRatio: 1.0,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.grey,
                borderRadius: BorderRadius.circular(10.0),
              ),
            ),
          ),
          SizedBox(height: 5.0),
          Text('Title'),
          SizedBox(height: 5.0),
          Text('99 MB'),
        ],
      ),
    );
  }
}

After that you just assign a PageController with viewportFraction lower than 1.0 so that it will display the next and previous blocks too.之后,您只需分配一个 viewportFraction 低于 1.0 的 PageController,以便它也会显示下一个和上一个块。

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

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