美文网首页
Flutter CustomScrollView 快速快转到指定

Flutter CustomScrollView 快速快转到指定

作者: gruan | 来源:发表于2021-04-10 16:55 被阅读0次

关于快速跳转,我找了几个优秀的组件,试用了一下, 发现和我的项目搭配起来, 配合的不好,因为我的行高不是固定的。

究其原因,这些组件是在假设:所有行高都是固定的, 然后找到数据在数据源中的位置, 然后乘以行高,在去跳转。

抛开上面的做法,我能搜到的办法只有:Scrollable.ensureVisible(<GlobalObjectKey>.currentContext);

Scrolls the scrollables that enclose the given context so as to make the
given context visible.

但是这个方法有个特点,当某个组件不在 tree (什么 tree ? tree new bee 的 tree) 上时, 它返回的是 null.

The current context is null if there is no widget in the tree that matches
this global key.

这个特点导至的结果是:不能在 ListView/SliverList/GridView 等有虚化功能的组件中使用Scrollable.ensureVisible 来定位,因为这些组件只绘制需要显示的子组件,不显示的子组件不在 tree 上,不在 treecurrentContext 就是 null.

简单来说,就是要实现快速跳转的数据不能太多,只能用 CustomScrollView 等没有 虚化功能的容器组件上。

相关文章

网友评论

      本文标题:Flutter CustomScrollView 快速快转到指定

      本文链接:https://www.haomeiwen.com/subject/nrknkltx.html