Flutter滚动条的ScrollController没有附加任何ScrollPosition。

35

当我使用AppBar时,出现以下错误:

滚动条的ScrollController没有附加ScrollPosition

这是我的CustomScrollBar

class CustomScrollBar extends StatelessWidget {
  final Widget child;
  final ScrollController scrollController;

  const CustomScrollBar({
    required this.scrollController,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return RawScrollbar(
      thumbColor: AppColors.gray,
      radius: Radius.circular(8),
      thickness: 4,
      isAlwaysShown: true,
      controller: scrollController,
      child: child,
    );
  }
}

我应该始终可见。这是我使用它的方式:

child: CustomScrollBar(
              scrollController: _scrollControllerForScrollBar,
              child: SingleChildScrollView(
                controller: _scrollControllerForScrollBar,
                child: Padding(
                  padding: EdgeInsets.all(7.0.scaled),
                  child: Container(
                    width: double.infinity,
                    child: Text(
                      'any text bla bla bla \n\n\n this is a lot of \n text \n .'
                    ),
                  ),
                ),
              ),
            ),

正如您所看到的,ScrollBarSingleChildScrollView 都使用相同的 ScrollController。我不知道为什么会出现这个错误。你有任何想法吗?


你解决了吗? - Balaji
@Balaji 不好意思,不是的... - Chris
我也遇到了同样的问题,如果我找到解决方案,我会告诉你。 - Balaji
在您的CustomScrollBar中,您需要将scrollController分配给RawScrollBar。 - Sami Issa
@SamiIssa 啊,我在问题中漏掉了那个,在我的代码里已经附加上了。 - Chris
这里有一些解决方案:https://dev59.com/G1QK5IYBdhLWcg3wSeAK - Uzair Leo
6个回答

75

我在使用带有 ListView 小部件的 ScrollBar 时,在调试控制台中遇到了这个错误。为了摆脱这个错误,我不得不在两个小部件上都设置 ScrollController

final yourScrollController = ScrollController();

Scrollbar(
  isAlwaysShown: true,
  thickness: 10,
  controller: yourScrollController, // Here 
  child: ListView.builder(
    padding: EdgeInsets.zero,
    scrollDirection: Axis.vertical,
    controller: yourScrollController, // AND Here
    itemCount: yourRecordList?.length
    ....
  )
)

6
如果你的子组件是 SingleChildScrollView 而不是 ListView.builder,这个方法同样适用。 - bitinerant

11

假设您同时使用Scrollbar小部件和ListView小部件作为滚动条的子级 - 两者都应该具有相同的控制器:

ScrollController _controller = ScrollController();

     Scrollbar(
              controller: _controller,
              child: ListView(
                controller: _controller,
           ....), // ListView
    ) // Scrollbar

2
如果你像这样使用嵌套滚动器:
 Expanded(
                          flex: 2,
                          child: Center(
                            child: Scrollbar(
                              child: SingleChildScrollView(
                                primary: true,
                                child: Scrollbar(
                                  child: SingleChildScrollView(
                                    scrollDirection: Axis.horizontal,
                                   .....)

您可以通过将以下行添加到第一个可滚动视图来解决此问题:

 primary: true

UI绘图程序想要知道移动设备和低分辨率桌面上滚动条的起始位置,大小为:

MediaQuery.of(context).size.shortestSide < 600

2

完全删除scrollController并且问题应该就解决了。这就是我修复代码的方式,所以你的代码应该看起来像这样:

class CustomScrollBar extends StatelessWidget {


 final Widget child;

  const CustomScrollBar({
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return RawScrollbar(
      thumbColor: AppColors.gray,
      radius: Radius.circular(8),
      thickness: 4,
      isAlwaysShown: true,
      child: child,
    );
  }
}

第二部分应该是这样的

,并且需要符合此格式。
child: CustomScrollBar(
              child: SingleChildScrollView(
                child: Padding(
                  padding: EdgeInsets.all(7.0.scaled),
                  child: Container(
                    width: double.infinity,
                    child: Text(
                      'any text bla bla bla \n\n\n this is a lot of \n text \n .'
                    ),
                  ),
                ),
              ),
            ),

0

对于这个错误,您必须为ScrollBar和SingleSchildScrollView/ListView定义相同的1个scrollController:

例如,来自我的项目:

  final ScrollController _scrollController = ScrollController(); //define just 1 controller
  final _currencyFormatter = NumberFormat('#,##0.00', 'vi_VN');
  final double _safeSpaceToShowToolTip = 1.4;
  final double _fixedChartHeight = 350.0;
  int _touchedIndex = -1;

  @override
  Widget build(BuildContext context) {
    return Scrollbar(
      controller: _scrollController, // and use it both here
      isAlwaysShown: true,
      trackVisibility: true,
      radius: Radius.circular(6.0),
      child: SingleChildScrollView(
        controller: _scrollController, // AND HERE
        scrollDirection: Axis.horizontal,
        physics: BouncingScrollPhysics(),
        child: Container(
          margin: EdgeInsets.only(bottom: R.dimens.mediumSpacing),
          height:
              R.dimens.getSpecificSize(_fixedChartHeight, isForHeight: true),
          width: R.dimens
              .getSpecificSize(_fixedChartHeight * 1.9, isForHeight: false),
          child: BarChart(
            _barChartData(),
          ),
        ),
      ),
    );
  }

0
在调度程序回调期间抛出了以下断言: 滚动条的ScrollController未附加滚动位置。 没有ScrollPosition,无法绘制滚动条。 Scrollbar尝试使用提供的ScrollController。此ScrollController应与应用Scrollbar的ScrollView相关联。在提供自己的ScrollController时,请确保Scrollbar和Scrollable小部件使用相同的ScrollController。
这是Flutter的错误,https://github.com/flutter/flutter/issues/82573
它已经在Flutter主分支上修复。
flutter channel master
flutter upgrade

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接