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

Flutter中如何为StickyGroupedListView添加Scrollbar?

解决方案

问题的核心是GroupedItemScrollController并非直接继承自ScrollController,但它内部封装了一个原生的ScrollController实例,我们可以直接使用这个内部实例来对接Scrollbar。

具体修改步骤:

  1. 确保你声明的scrollController是GroupedItemScrollController类型(而非父类或其他类型)
  2. 在Scrollbar的controller参数中,调用该控制器的scrollController属性,获取其内部的原生滚动控制器

修改后的代码:

Scrollbar(
  interactive: true,
  // 使用GroupedItemScrollController内部的原生ScrollController
  controller: widget.scrollController.scrollController,
  radius: const Radius.circular(10),
  thickness: 5,
  child: Padding(
    padding: const EdgeInsets.only(top: 25, right: 5, left: 5),
    child: StickyGroupedListView<Repostaje, DateTime>(
      elements: repostajes,
      groupBy: (Repostaje repostaje) => DateTime(
        repostaje.fecha!.year,
        repostaje.fecha!.month,
      ),
      groupSeparatorBuilder: (Repostaje repostaje) =>
          separadorRepostje(repostaje, themeProvider),
      stickyHeaderBackgroundColor: Colors.transparent,
      itemBuilder: (context, repostaje) => repostajeCard(repostaje),
      itemComparator: (item1, item2) =>
          item1.fecha!.compareTo(item2.fecha!),
      itemScrollController: widget.scrollController,
      physics: const AlwaysScrollableScrollPhysics(
          parent: BouncingScrollPhysics()),
      order: StickyGroupedListOrder.DESC,
    ),
  ),
),

额外注意事项:

  • 初始化scrollController时,要明确创建GroupedItemScrollController实例,比如:
    final scrollController = GroupedItemScrollController();
    
  • 这个方案依赖sticky_grouped_list包的内部实现,目前该包稳定版本中GroupedItemScrollController一直保留了scrollController公开属性,兼容性有保障

内容的提问来源于stack exchange,提问作者eljorgit

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:50:37