简体   繁体   English

Flutter:带 viewportFraction 的水平列表视图

[英]Flutter : Horizontal Listview with viewportFraction

I want to implement this horizontal ListView effect.我想实现这个水平的 ListView 效果。

在此处输入图像描述

Planning to create a horizontal ListView like this.计划像这样创建一个水平的 ListView。 Example the horizontal listView will show 2 items and 1 item only show up 20%.例如,水平 listView 将显示 2 个项目,而 1 个项目仅显示 20%。

在此处输入图像描述

When scrolling it will become like this.滚动的时候会变成这样。 Example front and end show up 20% and center show 2 items.示例前端和末端显示 20%,中心显示 2 个项目。

在此处输入图像描述

Edited: Code I'm using right now:编辑:我现在使用的代码:

viewportFraction = 1 / 2.3;

LayoutBuilder(
    builder: (BuildContext context, BoxConstraints constraints) {
  final double itemWidth =
      (constraints.maxWidth - padding.horizontal) * this.viewportFraction;
  final double itemHeight = (itemWidth * this.aspectRatio);

  return new Container(
    height: itemHeight,
    child: new ListView.custom(
      scrollDirection: Axis.horizontal,
      controller: new PageController(
        initialPage: this.initialPage,
        viewportFraction: this.viewportFraction,
      ),
      physics: const PageScrollPhysics(),
      padding: this.padding,
      itemExtent: itemWidth,
      childrenDelegate: this.childrenDelegate,
    ),
  );
});

The easiest way of doing this is probably with an horizontal ListView builder最简单的方法可能是使用水平 ListView 构建器

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      scrollDirection:Axis.horizontal,
      itemCount:10,
      itemBuilder:(context,index){
        return Padding(
          padding:EdgeInsets.symmetric(horizontal:MediaQuery.of(context).size.width*0.05),
          child:Container(
            color: Colors.blue,
        child:Text(index.toString()),
        height:20,
        width:MediaQuery.of(context).size.width*0.25,
        )
        );
      }
    );
  }

The MediaQuery.of(context).size allows you to get information about your sreen geometry MediaQuery.of(context).size允许您获取有关屏幕几何的信息

I'll let you do some math to find the right fraction of the screen to use to get the final result that you want我会让你做一些数学运算来找到屏幕的正确部分来获得你想要的最终结果

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

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