0

当我像这样在颤振 2.0.1 中使用 CupertinoScrollbar 时:

return GestureDetector(
      onHorizontalDragStart: _onHorizontalDragStart,
      onHorizontalDragUpdate: _onHorizontalDragUpdate,
      onHorizontalDragEnd: _onHorizontalDragEnd,
      child: CupertinoScrollbar(
          child: Container(
        constraints: BoxConstraints(
          minHeight: MediaQuery.of(context).size.height * 0.9,
        ),
        color: Theme.of(context).scaffoldBackgroundColor,
        child: Padding(
          padding: const EdgeInsets.all(
            16.0,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              InkWell(
                onTap: () => CommonUtils.launchUrl(item.link),
                child: Padding(
                  padding: const EdgeInsets.only(bottom: 8.0),
                  child: Container(
                    child: Text(
                      item.title == "" ? "Comment" : item.title,
                      style: Theme.of(context).textTheme.headline5!.copyWith(
                            fontWeight: FontWeight.w600,
                          ),
                    ),
                  ),
                ),
              ),
              if (item.domain != "")
                Padding(
                  padding: const EdgeInsets.only(bottom: 8.0),
                  child: InkWell(
                      onTap: () async {
                        navToChannelDetail();
                      },
                      child: Text(
                        item.domain,
                        style: Theme.of(context).textTheme.caption!.copyWith(color: Theme.of(context).primaryColor),
                      )),
                ),
              InkWell(
                onTap: () {},
                child: RichText(
                  text: TextSpan(
                    children: <TextSpan>[
                      TextSpan(
                        text: item.author,
                        style: Theme.of(context).textTheme.caption,
                      ),
                      TextSpan(
                        text: " ${String.fromCharCode(8226)} ",
                        style: Theme.of(context).textTheme.caption,
                      ),
                      TextSpan(
                        text: item.ago,
                        style: Theme.of(context).textTheme.caption,
                      ),
                    ],
                  ),
                ),
              ),
              if (item.content != "")
                Html(
                  data: item.content,
                  style: {
                    "body": Style(
                      fontSize: FontSize(19.0),
                    ),
                  },
                  onLinkTap: (url) => CommonUtils.launchUrl(url),
                ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(right: 16.0),
                        child: Row(
                          children: [
                            if (item.isFav == 1)
                              IconButton(
                                icon: Icon(Icons.bookmark, color: Theme.of(context).primaryColor),
                                onPressed: () => touchFav("unfav", FavStatus.UNFAV),
                              ),
                            if (item.isFav != 1)
                              IconButton(
                                icon: Icon(Icons.bookmark),
                                onPressed: () => touchFav("fav", FavStatus.FAV),
                              ),
                            Padding(
                              padding: const EdgeInsets.only(left: 0.0),
                              child: Text(
                                "${item.favCount}",
                                textAlign: TextAlign.center,
                                style: Theme.of(context).textTheme.caption!.copyWith(
                                      color: Theme.of(context).primaryColor,
                                    ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(bottom: 0.0),
                        child: Row(
                          children: [
                            if (item.isUpvote == 1)
                              IconButton(
                                icon: Icon(Icons.thumb_up, color: Theme.of(context).primaryColor),
                                onPressed: () => touchUpvote("unupvote", UpvoteStatus.UNUPVOTE),
                              ),
                            if (item.isUpvote != 1)
                              IconButton(
                                icon: Icon(Icons.thumb_up),
                                onPressed: () => touchUpvote("upvote", UpvoteStatus.UPVOTE),
                              ),
                            Padding(
                              padding: const EdgeInsets.only(left: 8.0),
                              child: Text(
                                "${item.upvoteCount}",
                                textAlign: TextAlign.center,
                                style: Theme.of(context).textTheme.caption!.copyWith(
                                      color: Theme.of(context).primaryColor,
                                    ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  IconButton(
                    icon: Icon(
                      Feather.share_2,
                    ),
                    onPressed: () => handleShare(id: item.id, title: item.title, postUrl: item.link),
                  ),
                ],
              ),
            ],
          ),
        ),
      )));

但用户界面不显示滚动条,哪里出了问题,我应该怎么做才能解决它?

在此处输入图像描述

错误日志显示:

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
ScrollController not attached to any scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 108 pos 12: '_positions.isNotEmpty'


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

When the exception was thrown, this was the stack: 
#2      ScrollController.position (package:flutter/src/widgets/scroll_controller.dart:108:12)
#3      RawScrollbarState._maybeTriggerScrollbar.<anonymous closure> (package:flutter/src/widgets/scrollbar.dart:862:27)
#4      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#5      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1090:9)
#6      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:998:5)
...
====================================================================================================
4

1 回答 1

1

将控制器添加到 CupertinoScrollbar

final ScrollController _controller = ScrollController();

用 SingleChildScrollView 包装你的容器并传递它 _controller

完整代码

return GestureDetector(
      onHorizontalDragStart: _onHorizontalDragStart,
      onHorizontalDragUpdate: _onHorizontalDragUpdate,
      onHorizontalDragEnd: _onHorizontalDragEnd,
      child: CupertinoScrollbar(
                controller: _controller, //Add this

           child : SingleChildScrollView( // wrap 
                controller: _controller, // add this

          child: Container(
        constraints: BoxConstraints(
          minHeight: MediaQuery.of(context).size.height * 0.9,
        ),
        color: Theme.of(context).scaffoldBackgroundColor,
        child: Padding(
          padding: const EdgeInsets.all(
            16.0,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              InkWell(
                onTap: () => CommonUtils.launchUrl(item.link),
                child: Padding(
                  padding: const EdgeInsets.only(bottom: 8.0),
                  child: Container(
                    child: Text(
                      item.title == "" ? "Comment" : item.title,
                      style: Theme.of(context).textTheme.headline5!.copyWith(
                            fontWeight: FontWeight.w600,
                          ),
                    ),
                  ),
                ),
              ),
              if (item.domain != "")
                Padding(
                  padding: const EdgeInsets.only(bottom: 8.0),
                  child: InkWell(
                      onTap: () async {
                        navToChannelDetail();
                      },
                      child: Text(
                        item.domain,
                        style: Theme.of(context).textTheme.caption!.copyWith(color: Theme.of(context).primaryColor),
                      )),
                ),
              InkWell(
                onTap: () {},
                child: RichText(
                  text: TextSpan(
                    children: <TextSpan>[
                      TextSpan(
                        text: item.author,
                        style: Theme.of(context).textTheme.caption,
                      ),
                      TextSpan(
                        text: " ${String.fromCharCode(8226)} ",
                        style: Theme.of(context).textTheme.caption,
                      ),
                      TextSpan(
                        text: item.ago,
                        style: Theme.of(context).textTheme.caption,
                      ),
                    ],
                  ),
                ),
              ),
              if (item.content != "")
                Html(
                  data: item.content,
                  style: {
                    "body": Style(
                      fontSize: FontSize(19.0),
                    ),
                  },
                  onLinkTap: (url) => CommonUtils.launchUrl(url),
                ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(right: 16.0),
                        child: Row(
                          children: [
                            if (item.isFav == 1)
                              IconButton(
                                icon: Icon(Icons.bookmark, color: Theme.of(context).primaryColor),
                                onPressed: () => touchFav("unfav", FavStatus.UNFAV),
                              ),
                            if (item.isFav != 1)
                              IconButton(
                                icon: Icon(Icons.bookmark),
                                onPressed: () => touchFav("fav", FavStatus.FAV),
                              ),
                            Padding(
                              padding: const EdgeInsets.only(left: 0.0),
                              child: Text(
                                "${item.favCount}",
                                textAlign: TextAlign.center,
                                style: Theme.of(context).textTheme.caption!.copyWith(
                                      color: Theme.of(context).primaryColor,
                                    ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(bottom: 0.0),
                        child: Row(
                          children: [
                            if (item.isUpvote == 1)
                              IconButton(
                                icon: Icon(Icons.thumb_up, color: Theme.of(context).primaryColor),
                                onPressed: () => touchUpvote("unupvote", UpvoteStatus.UNUPVOTE),
                              ),
                            if (item.isUpvote != 1)
                              IconButton(
                                icon: Icon(Icons.thumb_up),
                                onPressed: () => touchUpvote("upvote", UpvoteStatus.UPVOTE),
                              ),
                            Padding(
                              padding: const EdgeInsets.only(left: 8.0),
                              child: Text(
                                "${item.upvoteCount}",
                                textAlign: TextAlign.center,
                                style: Theme.of(context).textTheme.caption!.copyWith(
                                      color: Theme.of(context).primaryColor,
                                    ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  IconButton(
                    icon: Icon(
                      Feather.share_2,
                    ),
                    onPressed: () => handleShare(id: item.id, title: item.title, postUrl: item.link),
                  ),
                ],
              ),
            ],
          ),
        ),
      ))));
于 2021-04-02T11:19:21.670 回答