flutter开发tab页面嵌套滚动的最简洁实现方式
flutter通过处理PageView滚动事件实现tab嵌套滚动
在app里实现内外层滚动是很常见的需求,比如app底部四个tab是可以滚动的,首页里的n多个tab又是可以滚动的,当首页里tab滚动到最后一个tab继续滑动时,希望滑动到底部下一个tab,但是flutter组件pageView嵌套时是不会自动处理的,所以需要我们自己处理滑动事件
- 调试了大半天,感觉效果也差不多了,应该可以满足要求,代码很少,直接贴代码了
class PageViewScrollUtils {
final PageController pageController;
final TabController tabController;
Drag _drag;
PageViewScrollUtils(this.pageController, this.tabController);
bool handleNotification(ScrollNotification notification) {
if (notification is UserScrollNotification) {
if (notification.direction == ScrollDirection.reverse && tabController.index == tabController.length - 1) {
_drag = pageController.position.drag(DragStartDetails(), () {
_drag = null;
});
}
}
if (notification is OverscrollNotification) {
if (notification.dragDetails != null && _drag != null) {
_drag.update(notification.dragDetails);
}
}
if (notification is ScrollEndNotification) {
if (notification.dragDetails != null && _drag != null) {
_drag.end(notification.dragDetails);
}
}
return true;
}
}
以上一个工具类搞定,使用也非常简单,下面是使用demo:
NotificationListener<ScrollNotification>(
child: TabBarView(),
onNotification: _pageViewScrollUtils.handleNotification,
)
效果如下所示(视频转为gif后变慢的):
新增TarBarView嵌套滑动实现,效果如下:
完整项目项目地址:https://github.com/yongfengnice/flutter_nest_page_view
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库