flutter 使用Scrollbar 时出现滚动条不置顶问题

Flutter 使用 CupertinoScrollbar 、Scrollbar  与 ListView.builder 结合使用时, 当把 ListView.builder 边距设置为 padding: const EdgeInsets.all(0) 的时候, Scrollbar 的滚动条不置顶。

如图:右侧边上的滚动条

解决方法:

MediaQuery.removePadding(
    context: context,
    removeTop: true,
        Scrollbar(
            child: ListView.builder(
                padding: const EdgeInsets.all(0),
                itemBuilder:  (...) {
                }
            )
        )
    )

*注意 使用Scrollbar 可能出现以下错误

════════ Exception caught by animation library ═════════════════════════════════
The following assertion was thrown while notifying status listeners for AnimationController:
The Scrollbar's ScrollController has no ScrollPosition attached.
A Scrollbar cannot be painted without a ScrollPosition.
The Scrollbar attempted to use the PrimaryScrollController. This ScrollController should be associated with the ScrollView that the Scrollbar is being applied to.When ScrollView.scrollDirection is Axis.vertical on mobile platforms will automatically use the PrimaryScrollController if the user has not provided a ScrollController. To use the PrimaryScrollController explicitly, set ScrollView.primary to true for the Scrollable widget.

When the exception was thrown, this was the stack:

 其实就是 Scrollbar 无法找到对应的 ListView, 正常是可以找到的,但需要页面只有一个ListView 情况下, 出现这种错误大概率是你的页面出现了两个滚动相关的实例, 有可能不是你自己写的,而是使用其他插件 内部存在的。

总之 只要用 ScrollController 把他们两个关联起来即可

ScrollController _scrollController = ScrollController();

Widget build(BuildContext context) {
....
.....

// 使用以下内容

MediaQuery.removePadding(
    context: context,
    removeTop: true,
        Scrollbar(
            controller: _scrollController,
            child: ListView.builder(
                controller: _scrollController,
                padding: const EdgeInsets.all(0),
                itemBuilder:  (...) {
                }
            )
        )
    )

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值