Flutter中如何为StickyGroupedListView添加Scrollbar?
解决方案
问题的核心是GroupedItemScrollController并非直接继承自ScrollController,但它内部封装了一个原生的ScrollController实例,我们可以直接使用这个内部实例来对接Scrollbar。
具体修改步骤:
- 确保你声明的
scrollController是GroupedItemScrollController类型(而非父类或其他类型) - 在
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
相关产品推荐
相关产品推荐

