flutter 实现AppStore左右滑动

在AppStore中如何实现左右滑动,因为使用PageView会居中显示,不会居左显示,目前没有找到解决方案,我使用的方案是ListView+自定义physics实现的。

代码

SizedBox(
      width: 200,
      height: 400,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        physics: const MyScreenPagePhysics(
          viewportDimension: 200 + 20 * 2, //需要计算viewport宽度
        ),
        itemBuilder: (context, index) {
          return Padding(
            padding: EdgeInsets.symmetric(horizontal: 20),
            child: Column(
              children: [
                CachedNetworkImage(
                  //图标
                  width: 200,
                  height: 400,
                  fit: BoxFit.fill,
                  imageUrl:
                      "https://is1-ssl.mzstatic.com/image/thumb/Purple221/v4/07/b1/d7/07b1d7f0-76b1-e292-541b-c04a2eede928/AppIcon-1x_U007emarketing-0-7-0-sRGB-85-220.png/512x512bb.jpg",
                  placeholder: (context, url) =>
                      LoadingAnimationWidget.threeArchedCircle(
                    color: Colors.white,
                    size: 20,
                  ).center(),
                  errorWidget: (context, url, error) =>
                      const Icon(Icons.error_outline_rounded),
                ).border(color: Colors.blue),
              ],
            ),
          );
        },
        itemCount: 2,
      ),
    )

MyScreenPagePhysics.dart源码

import 'package:flutter/cupertino.dart';
import 'dart:math' as math;

class MyScreenPagePhysics extends ScrollPhysics {
  final double viewportDimension;

  const MyScreenPagePhysics({
    super.parent,
    required this.viewportDimension,
  });

  @override
  MyScreenPagePhysics applyTo(ScrollPhysics? ancestor) {
    return MyScreenPagePhysics(
      parent: buildParent(ancestor),
      viewportDimension: viewportDimension,
    );
  }

  double _getPage(ScrollMetrics position) {
    return position.pixels / position.viewportDimension;
  }

  double _getPixels(ScrollMetrics position, double page) {
    return math.min(
      position.maxScrollExtent,
      page * position.viewportDimension,
    );
  }

  double _getTargetPixels(
      ScrollMetrics position, Tolerance tolerance, double velocity) {
    double page = _getPage(position);
    if (velocity < -tolerance.velocity) {
      page -= 0.5;
    } else if (velocity > tolerance.velocity) {
      page += 0.5;
    }
    return _getPixels(position, page.roundToDouble());
  }

  @override
  Simulation? createBallisticSimulation(
      ScrollMetrics _position, double velocity) {
    ScrollMetrics position = _position.copyWith(
      viewportDimension: viewportDimension,
    );
    if ((velocity <= 0.0 && position.pixels <= position.minScrollExtent) ||
        (velocity >= 0.0 && position.pixels >= position.maxScrollExtent)) {
      return super.createBallisticSimulation(position, velocity);
    }
    final Tolerance tolerance = toleranceFor(position);
    final double target = _getTargetPixels(position, tolerance, velocity);
    if (target != position.pixels) {
      return ScrollSpringSimulation(spring, position.pixels, target, velocity,
          tolerance: tolerance);
    }
    return null;
  }
}

效果图

相关推荐
AntDreamer几秒前
在实际开发中,如何根据项目需求调整 RecyclerView 的缓存策略?
android·java·缓存·面试·性能优化·kotlin
子非鱼92140 分钟前
【前端】ES6:Set与Map
前端·javascript·es6
想退休的搬砖人1 小时前
vue选项式写法项目案例(购物车)
前端·javascript·vue.js
啥子花道1 小时前
Vue3.4 中 v-model 双向数据绑定新玩法详解
前端·javascript·vue.js
麒麟而非淇淋1 小时前
AJAX 入门 day3
前端·javascript·ajax
茶茶只知道学习2 小时前
通过鼠标移动来调整两个盒子的宽度(响应式)
前端·javascript·css
清汤饺子2 小时前
实践指南之网页转PDF
前端·javascript·react.js
蒟蒻的贤2 小时前
Web APIs 第二天
开发语言·前端·javascript
清灵xmf2 小时前
揭开 Vue 3 中大量使用 ref 的隐藏危机
前端·javascript·vue.js·ref
蘑菇头爱平底锅2 小时前
十万条数据渲染到页面上如何优化
前端·javascript·面试