前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter 实现简单聊天界面 下拉滑动加载更多

Flutter 实现简单聊天界面 下拉滑动加载更多

作者头像
用户4458175
发布2020-07-15 10:11:21
1.1K0
发布2020-07-15 10:11:21
举报
文章被收录于专栏:andy的小窝andy的小窝

先上效果图。

刷新功能实现

将刷新组件嵌入滑动组件中,因为聊天界面都是由下往上滑,所以ListView设置了reverse: true实现反转列表组件。组件itemCount的值设置消息数组长度+1,因为设置了reverse,所以需要将刷新加载组件放到当index == 消息长度的位置。

代码语言:javascript
复制
return index == _chatRecords.length ? LoadIndicator() : chatItemWidget(index);

判断是否出发刷新的逻辑也很简单,当_scrollController.position.pixels大于等于_scrollController.position.maxScrollExtent的时候触发刷新操作就可以了。

代码语言:javascript
复制
if (_scrollController.position.pixels >= _scrollController.position.maxScrollExtent) {
      if (_isLoading) return;
      _isLoading = true;
      onLoadMore();
}

界面优化

·优化列表滑动弹性效果

列表的physics使用了自己实现的ChatScrollPhysics。目的是为了实现下滑加载带弹性效果,上滑屏蔽弹性效果。(BouncingScrollPhysics是上下都有弹性效果)

重写applyBoundaryConditions方法

代码语言:javascript
复制
///base on BouncingScrollPhysics
///
class ChatScrollPhysics extends ScrollPhysics {
  ...
  @override
  double applyBoundaryConditions(ScrollMetrics position, double value) {
    if (value < position.pixels && position.pixels <= position.minScrollExtent) // underscroll
      return value - position.pixels;
    if (value < position.minScrollExtent && position.minScrollExtent < position.pixels) // hit top edge
      return value - position.minScrollExtent;
    return 0.0;
  }
  
  ...
}

同时修改 0.52的值可以实现这个系数从某个值开始,逐渐变得难以过度滚动。

代码语言:javascript
复制
  double frictionFactor(double overscrollFraction) =>
      0.52 * math.pow(1 - overscrollFraction, 2);

·关闭过度滚动产生的光晕

使用ScrollConfiguration包裹滑动组件behavior设置成自己实现的behavior。

代码语言:javascript
复制
ScrollConfiguration(
   behavior: ChatScrollBehavior(),
   child: yourScrollViewWidget,
),
代码语言:javascript
复制
class ChatScrollBehavior extends ScrollBehavior {
  final bool showLeading;
  final bool showTrailing;

  ChatScrollBehavior({
    this.showLeading: false,
    this.showTrailing: false,
  });

  @override
  Widget buildViewportChrome(BuildContext context, Widget child, AxisDirection axisDirection) {
    return GlowingOverscrollIndicator(
      showLeading: showLeading,
      showTrailing: showTrailing,
      child: child,
      axisDirection: axisDirection,
      color: Theme.of(context).primaryColor,
    );
  }
}

代码地址传送门:https://github.com/hwh97/chat_demo.git

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档