flutter - 颤动中的多向滚动
问题描述
我来自 Web 开发背景,并且习惯于创建一个同时具有 x 和 y 溢出的元素(允许向任何方向滚动)。我正在努力使用 Flutter 实现相同的功能。
翻阅文档,我找到了 SingleChildScrollView,但它只允许 Axis.horizontal 或 Axis.vertical,而不是两者。
所以我尝试了以下方法:
return SingleChildScrollView( // horizontal scroll widget
scrollDirection: Axis.horizontal,
child: SingleChildScrollView( // vertical scroll widget
scrollDirection: Axis.vertical,
child: ...content of the container etc...
)
);
这适用于 x 和 y,但它不允许对角滚动。
有没有办法实现对角滚动,或者有没有更好的材料小部件,我完全错过了?
谢谢
解决方案
我设法找到了一个解决方案,虽然它并不完美:
我创建了一个 StatefulWidget,Offset _scrollOffset
它使用带有 Transform 类型的子元素的 ClipRect。变换矩阵 ( Matrix4.identity()..translate(_offset.dx, _offset.dy)
) 应用于变换。
AGestureDetector
分配了一个 onPanUpdate 回调来更新滚动位置。_scrollOffset += e.delta
. 如果滚动位置太低或太高,这可以通过设置滚动位置来限制在小部件的边界内。
Animation 和 AnimationController 用于设置投掷速度。onPanEnd 提供最后一个平移的速度,因此只需根据该速度执行一个带有弹跳的 Tween。
动画在 TapDown 上停止,因此用户可以停止滚动速度。
这样做的主要问题是它不能完美地模仿 Android 或 iOS 的滚动速度,尽管我正在努力尝试使用 Flutter 提供的 ScrollSimulation 类让它更好地工作。
推荐阅读
- bixby - 您如何测试访问用户个人资料的胶囊?
- angular - 对空白 ID 的 Http.post 和 app-console 内容运行测试已通过
- linux - 在下面的行中提取一个值并将结果写在原始搜索词旁边(linux)
- c++ - 如果我们在无限循环中等待响应,通过 wininet 进行的异步调用会成功完成吗?
- microsoft-graph-api - 使用 API Graph 在驱动器之间复制文件时出错
- python - 将一种热编码写入 csv
- json - MongoDB一对多数据结构困境
- classification - 将 GPS 数据聚类为“k”组
- wordpress - 如何更改帖子类型“页面”上 CPT 循环的摘录长度?
- python - 如果同一索引处的值相等,则删除两个列表的尾随项