You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 06:45:55