Flutter 将 Draggable Scrollbar 添加到 CustomScrollView?它一直给我 'The argument type CustomScrollView can' t 被分配给 BoxScrollView'

标签 flutter dart

一个简单的例子是:

Scaffold(
          floatingActionButton: _fab(),
          floatingActionButtonLocation: _fabLocation(),
          body: Scrollbar(
            child: CustomScrollView(
              slivers: <Widget>[
                SliverAppBar(
                  automaticallyImplyLeading: false,
                  leading: null,
                  backgroundColor: Theme.of(context).colorScheme.secondary,
                  title: AutoSizeText(
                    _catalogSearchRatio,
                    style: Theme.of(context).textTheme.title.copyWith(
                        color: Theme.of(context).colorScheme.onSecondary),
                  ),
                  floating: true,
                  pinned: false,
                ),
                SliverFixedExtentList(
                  itemExtent: 90.0,
                  delegate: SliverChildBuilderDelegate((context, index) {
                    return Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 5.0, vertical: 5.0),
                      child: CatalogItems.medidaGroups
                              .contains(_itemsCatalogDisplay[index].group)
                          ? _medidasCard(
                              index, _itemsCatalogDisplay, _catalogSearchQuery)
                          : _catalogCard(index, _itemsCatalogDisplay,
                              _catalogSearchQuery, _cartItems, _filteredGroups),
                    );
                  }, childCount: itemsCatalogDisplayLength),
                ),
              ],
              controller: _catalogScrollController,
            ),
          ),
        );

在上面的例子中,可以使用滚动条,这很好。但你不能拖动滚动条。所以我使用一个名为 draggable_scrollbar 的包这让我可以拖动滚动条。但是,我似乎无法替换示例中的滚动条,因为它给了我无法分配给 BoxScrollView 错误。 如果它只是一个 ListviewListview.builder 甚至是 GridView

最佳答案

首先导入 Cupertino 包,如下所示。

Import 'package: flutter/cupertino.dart';

现在,在包含 ScrollViews 的类中添加如下 ScrollController...

final ScrollController myScrollWorks = ScrollController();

现在包装您的任何 ScrollViews...在您的情况下是一个 CustomScrollView ,代码如下

body: PrimaryScrollController(
    controller: myScrollWorks,
        child: CupertinoScrollbar(
            child: CustomScrollView(
                slivers:[
                    your slivers code here
                 ];

关于Flutter 将 Draggable Scrollbar 添加到 CustomScrollView?它一直给我 'The argument type CustomScrollView can' t 被分配给 BoxScrollView',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60696181/

相关文章:

dart - 使用 Web View 向服务器发送参数(发布方法)

android - 在 Column 中包装一个 listviewBuilder?

intellij-idea - IntelliJ 重新格式化设置

dart - 当不指定返回类型时,dart 显示另一个函数内部的精确返回类型,但对于顶层是动态的

annotations - 如何创建自定义的Polymer和PolymerDart注释?

ios - 有没有人在 iOS flutter 上看到过 stripe_payments 的这个编译错误?

flutter - 如何解决家庭酿造的 flutter 医生问题?

FirebaseAnimatedList 实时改变内容

flutter - 通过DropDownList中的DropdownMenuItem在Flutter中传递对象

jquery-animate - 如何在 Dart 中设置滚动动画?