Android Flutter实现上拉加载组件的示例代码

 更新时间:2022年08月29日 09:34:24   作者:JulyYu  
既然列表有下拉刷新外当然还有上拉加载更多操作了,本次就为大家详细介绍如何利用Flutter实现为列表增加上拉加载更多的交互,感兴趣的可以了解一下

前言

在此之前对列表下拉刷新做了调整方案,具体介绍可以阅读下拉刷新组件交互调整。既然列表有下拉刷新外当然还有上拉加载更多操作了,本次就来介绍如何为列表增加上拉加载更多的交互实现。

实现方案

上拉刷新实现形式上可以有多种实现方式,若不带交互形式可采用NotificationListener组件监听滑动来实现上拉加载更多;如果对操作交互上有一定要求期望上拉刷新带有直观动画可操作性就需要实现一定样式来实现了。

监听NotificationListener实现

NotificationListener(
      onNotification: (scrollNotification) {
        if (scrollNotification.metrics.pixels >=
                scrollNotification.metrics.maxScrollExtent - size.height &&
            scrollNotification.depth == 0) {
          if (!isLoading) {
            isLoading = true;
            Future.delayed(Duration(seconds: 1), () {
              isLoading = false;
              length += 10;
              Scaffold.of(context).showSnackBar(SnackBar(
                content: Text('下拉加载更多了!!!'),
                duration: Duration(milliseconds: 700),
              ));
              setState(() {});
            });
          }
        }
        return false;
      }
  ......
  )

NotificationListener增加样式

     SliverToBoxAdapter(
       child: Center(
         child: Text(
           length < 30 ? "加载更多...." : "没有更多",
           style: TextStyle(fontSize: 25),
         ),
       ),
     )

ScrollPhysics调整

BouncingScrollPhysicsiOS带有阻尼效果滑动交互,在下拉刷新中带有回弹阻尼效果是比较好的交互,但在上拉加载更多获取交互上这种效果或许有点多余。因此需要定制下拉刷新带有回弹阻尼效果,上拉加载没有回弹阻尼效果的。ScrollPhysics

CustomScrollView(
   physics: BouncingScrollPhysics(),
   slivers: <Widget>[]
)

具体实现代码如下所示:

class CustomBouncingScrollPhysics extends ScrollPhysics {
  const CustomBouncingScrollPhysics({ ScrollPhysics parent }) : super(parent: parent);

  @override
  CustomBouncingScrollPhysics applyTo(ScrollPhysics ancestor) {
    return CustomBouncingScrollPhysics(parent: buildParent(ancestor));
  }

  double frictionFactor(double overscrollFraction) => 0.52 * math.pow(1 - overscrollFraction, 2);



  /// 阻尼参数计算
  @override
  double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
    assert(offset != 0.0);
    assert(position.minScrollExtent <= position.maxScrollExtent);

    if (!position.outOfRange)
      return offset;

    final double overscrollPastStart = math.max(position.minScrollExtent - position.pixels, 0.0);
    final double overscrollPastEnd = math.max(position.pixels - position.maxScrollExtent, 0.0);
    final double overscrollPast = math.max(overscrollPastStart, overscrollPastEnd);
    final bool easing = (overscrollPastStart > 0.0 && offset < 0.0)
        || (overscrollPastEnd > 0.0 && offset > 0.0);
    final double friction = easing
    // Apply less resistance when easing the overscroll vs tensioning.
        ? frictionFactor((overscrollPast - offset.abs()) / position.viewportDimension)
        : frictionFactor(overscrollPast / position.viewportDimension);
    final double direction = offset.sign;

    return direction * _applyFriction(overscrollPast, offset.abs(), friction);
  }

  static double _applyFriction(double extentOutside, double absDelta, double gamma) {
    assert(absDelta > 0);
    double total = 0.0;
    if (extentOutside > 0) {
      final double deltaToLimit = extentOutside / gamma;
      if (absDelta < deltaToLimit)
        return absDelta * gamma;
      total += extentOutside;
      absDelta -= deltaToLimit;
    }
    return total + absDelta;
  }


  /// 边界条件 复用ClampingScrollPhysics的方法 保留列表在底部的边界判断条件
  @override
  double applyBoundaryConditions(ScrollMetrics position, double value){
    if (position.maxScrollExtent <= position.pixels && position.pixels < value) // overscroll
      return value - position.pixels;
    if (position.pixels < position.maxScrollExtent && position.maxScrollExtent < value) // hit bottom edge
      return value - position.maxScrollExtent;
    return 0.0;
  }

  @override
  Simulation createBallisticSimulation(ScrollMetrics position, double velocity) {
    final Tolerance tolerance = this.tolerance;
    if (velocity.abs() >= tolerance.velocity || position.outOfRange) {
      return BouncingScrollSimulation(
        spring: spring,
        position: position.pixels,
        velocity: velocity * 0.91, // TODO(abarth): We should move this constant closer to the drag end.
        leadingExtent: position.minScrollExtent,
        trailingExtent: position.maxScrollExtent,
        tolerance: tolerance,
      );
    }
    return null;
  }

  @override
  double get minFlingVelocity => kMinFlingVelocity * 2.0;

  @override
  double carriedMomentum(double existingVelocity) {
    return existingVelocity.sign *
        math.min(0.000816 * math.pow(existingVelocity.abs(), 1.967).toDouble(), 40000.0);
  }

  @override
  double get dragStartDistanceMotionThreshold => 3.5;
}

但是直接会发生错误日志,由于回调中OverscrollIndicatorNotification是没有metrics对象。对于

The following NoSuchMethodError was thrown while notifying listeners for AnimationController:
Class 'OverscrollIndicatorNotification' has no instance getter 'metrics'.

由于上滑没有阻尼滑动到底部回调Notification发生了变化,因此需要在NotificationListener中增加判断对超出滑动范围回调进行过滤处理避免异常情况发生。

   if(scrollNotification is OverscrollIndicatorNotification){
     return false;
   }

结果展示

演示代码看这里

以上就是Android Flutter实现上拉加载组件的示例代码的详细内容,更多关于Android Flutter上拉加载的资料请关注脚本之家其它相关文章!

相关文章

  • Android 自定义dialog的实现代码

    Android 自定义dialog的实现代码

    这篇文章主要介绍了Android 自定义dialog的实现代码的相关资料,需要的朋友可以参考下
    2017-03-03
  • Android项目迁移到AndroidX的方法步骤

    Android项目迁移到AndroidX的方法步骤

    这篇文章主要介绍了Android项目迁移到AndroidX的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Android实现阿里云oss上传流程解析

    Android实现阿里云oss上传流程解析

    这篇文章主要介绍了Android实现阿里云oss上传流程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Android之FanLayout制作圆弧滑动效果

    Android之FanLayout制作圆弧滑动效果

    这篇文章主要介绍了Android之FanLayout制作圆弧滑动效果,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • 一文教你如何使用Databinding写一个关注功能

    一文教你如何使用Databinding写一个关注功能

    这篇文章主要介绍了一文教你如何使用Databinding写一个关注功能,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • Android View事件分发和消费源码简单理解

    Android View事件分发和消费源码简单理解

    这篇文章主要介绍了Android View事件分发和消费源码简单理解的相关资料,需要的朋友可以参考下
    2017-07-07
  • Android studio设置文件头定制代码注释的方法

    Android studio设置文件头定制代码注释的方法

    这篇文章主要介绍了Android studio设置文件头定制代码注释的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-08-08
  • AndroidStudio集成OpenCV的实现教程

    AndroidStudio集成OpenCV的实现教程

    本文主要介绍了Android Studio集成OpenCV的实现教程,文中通过图文介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • android编程实现局部界面动态切换的方法

    android编程实现局部界面动态切换的方法

    这篇文章主要介绍了android编程实现局部界面动态切换的方法,以实例形式较为详细的分析了Android局部切换的布局及功能实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • Android实现房贷计算器

    Android实现房贷计算器

    这篇文章主要为大家详细介绍了Android实现房贷计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05

最新评论