'How to find out if a scrollbar is visible/attached in flutter?
I want to add space/padding for a scrollbar when it is visible.
How can I find out, if a scrollbar is visible in a flutter widget?
Solution 1:[1]
this lib can help you to do it https://pub.dev/packages/visibility_detector
@override
Widget build(BuildContext context) {
return VisibilityDetector(
key: Key('my-widget-key'),
onVisibilityChanged: (visibilityInfo) {
var visiblePercentage = visibilityInfo.visibleFraction * 100;
debugPrint(
'Widget ${visibilityInfo.key} is ${visiblePercentage}% visible');
},
child: someOtherWidget,
);
}
Solution 2:[2]
Through the following I could find out if there is a scrollbar
NotificationListener<ScrollMetricsNotification>(
onNotification: (notification) {
setState(() {
hasScrollbar = (notification.metrics.maxScrollExtent > 0);
});
},
and when hasScrollbar is true I add padding for it:
padding: hasScrollbar ? myThemeData.screenPadding.copyWith(right: 20.scaled()) : myThemeData.screenPadding,
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | derChris |
