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;
  }
}

效果图

相关推荐
web行路人3 分钟前
React中类组件和函数组件的理解和区别
前端·javascript·react.js·前端框架
番茄小酱0014 分钟前
Expo|ReactNative 中实现扫描二维码功能
javascript·react native·react.js
晨曦_子画4 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
子非鱼92122 分钟前
【Ajax】跨域
javascript·ajax·cors·jsonp
超雄代码狂25 分钟前
ajax关于axios库的运用小案例
前端·javascript·ajax
孤客网络科技工作室26 分钟前
AJAX 全面教程:从基础到高级
android·ajax·okhttp
周亚鑫1 小时前
vue3 pdf base64转成文件流打开
前端·javascript·pdf
落魄小二1 小时前
el-table 表格索引不展示问题
javascript·vue.js·elementui
y5236481 小时前
Javascript监控元素样式变化
开发语言·javascript·ecmascript
fruge2 小时前
纯css制作声波扩散动画、js+css3波纹催眠动画特效、【css3动画】圆波扩散效果、雷达光波效果完整代码
javascript·css·css3