Is there any way to show the scroll indicator on the ListView
?
Here is my basic code:
ListView.builder(
itemCount: 50,
itemBuilder: (context, index) => ListTile(title: Text("Item= ${index + 1}"),),
)
Is there any way to show the scroll indicator on the ListView
?
Here is my basic code:
ListView.builder(
itemCount: 50,
itemBuilder: (context, index) => ListTile(title: Text("Item= ${index + 1}"),),
)
Thanks to Günter Zöchbauer.
You can wrap your ListView
in Scrollbar
Scrollbar(
child: ListView.builder(
itemCount: 50,
itemBuilder: (context, index) => ListTile(title: Text("Item= ${index + 1}"),),),
)
Scrollbar
) applies to any scrollable widget (for example SingleChildScrollView
), not only to ListView. Set the isAlwaysShown
param to true so that the scrollbar is visible even when not scrolling; however even if isAlwaysShown is true if there is no need for a scrollbar (because the scrollable content does not exceed its limits) the scrollbar will not be displayed. –
Petaloid I think better to use CupertinoScrollbar
instead of Scrollbar
. With CupertinoScrollbar
, you can grab the scrollbar to move around in the list with it, whereas Scrollbar
is just a visual cue and you cannot interact with the scrollbar directly.
Ex:
CupertinoScrollbar(
child: ListView.builder(...),
Or
Scrollbar(
child: ListView.builder(...),
Create a ScrollController variable (ScrollController _scrollController);
Instatiate _scrollController
inside initState()
or wherever you want, _scrollController = ScrollController();
Add _scrollController
variable inside Scrollbar
and ListView
properties,
controller:_scrollController
Here's the code:
ScrollController _scrollController;
@override
void initState() {
super.initState();
_scrollController = ScrollController();
}
Scrollbar(
isAlwaysShown: true,
controller: _scrollController,
child: ListView(
controller: _scrollController,
)
if you don't want it always shown set to false
thumbVisibility: false,
You can implement this designer scrollbar library :
OR
You can wrap ListView in Scrollbar
widget
Scrollbar(
child: ListView.builder(...),
)
Scrollbar(
thickness: 10,
isAlwaysShown: true,
child: ListView.builder(
itemCount: _controller().transactonsList.length,
itemBuilder: (context, index) {
return Card(
elevation: 5,
child: Container(
padding: const EdgeInsets.only(bottom: 16),
height: 80,
child: Row(
children: [
SizedBox(width: 10),
amountOfTransaction(index),
SizedBox(width: 16),
dateAndTitleOfTransaction(index),
],
),
),
);
},
),
)
final ScrollController _scroll = ScrollController();
@override
Widget build(BuildContext context) {
(...)
child: ListView.builder(
controller: _scroll,
)
}
If u need to style your scroll bar a bit wrap the ListView in RawScrollbar and use the same scroll controller instance for both widgets
final ScrollController _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
(...)
child: RawScrollbar(
controller: _scrollController,
thumbColor: Colors.redAccent,
radius: const Radius.circular(8),
crossAxisMargin: 2,
child: ListView.builder(
controller: _scrollController,
itemCount: 50,
itemBuilder: (context, index) => ListTile(
title: Text("Item= ${index + 1}"),
),
),
),
}
Scrollbar( child:ListView.builder( itemCount:20, itemBuilder:(c,i) => MyItem(i), ), ),
You have to give itemcount as how many list you have -----Example: itemCount: items.length,-----
You can also tune this scrollbar with ScrollbarTheme.
Container(
constraints: const BoxConstraints(maxHeight: 200),
child: ScrollbarTheme(
data: const ScrollbarThemeData(
crossAxisMargin: -8,
thickness: MaterialStatePropertyAll(4.0),
thumbColor: MaterialStatePropertyAll(Colors.red),
),
child: Scrollbar(
child: ListView.separated(
...
),
),
),
),
© 2022 - 2024 — McMap. All rights reserved.
ListView
is smaller than it's content and which is why it scrolls, I thought there would be some inbuilt indicator attached with the ListView but there was nothing like that. – Clyte