Flutter SuperEditor:如何滚动到DocumentNode或获取其垂直偏移
SuperReader滚动到指定DocumentNode的实现方案
先持有
SuperReaderController实例,用于控制阅读器:final _readerController = SuperReaderController(); // 绑定到SuperReader组件 SuperReader( controller: _readerController, // 其他配置项 )获取目标节点的偏移位置:
通过控制器的documentLayout获取文档布局,调用getRectForNode方法拿到节点的视口矩形,其中top属性就是节点的垂直偏移:final nodeRect = _readerController.documentLayout.getRectForNode(targetNode.id); if (nodeRect != null) { final nodeOffsetTop = nodeRect.top; // 节点的垂直偏移位置 }滚动到目标节点:
- 自动适配视口:使用
ensureNodeVisible方法,会自动滚动让节点进入可视区域:_readerController.ensureNodeVisible(targetNode.id); - 精准滚动控制:如果需要对齐视口顶部,用
scrollToPosition传入节点的偏移值:if (nodeRect != null) { _readerController.scrollToPosition(nodeRect.top); }
- 自动适配视口:使用
注意:务必在文档布局完成后执行上述操作,比如放在
WidgetsBinding.instance.addPostFrameCallback回调中,避免获取到null的节点矩形。
内容的提问来源于stack exchange,提问作者Paurakh Sharma Humagain
相关产品推荐
相关产品推荐

