一个简单的例子是:
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 错误。 如果它只是一个 Listview 或 Listview.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/