Commit df0a9fc1 authored by Hans Muller's avatar Hans Muller

Scrollbar cleanups (#3229)

parent 83ef964a
...@@ -487,7 +487,7 @@ class ScaffoldState extends State<Scaffold> { ...@@ -487,7 +487,7 @@ class ScaffoldState extends State<Scaffold> {
double _floatingAppBarHeight = 0.0; double _floatingAppBarHeight = 0.0;
bool _handleScrollNotification(ScrollNotification notification) { bool _handleScrollNotification(ScrollNotification notification) {
if (config.scrollableKey != null && config.scrollableKey == notification.scrollable.config.key) { if (config.scrollableKey == null || config.scrollableKey == notification.scrollable.config.key) {
final double newScrollOffset = notification.scrollable.scrollOffset; final double newScrollOffset = notification.scrollable.scrollOffset;
setState(() { setState(() {
_scrollOffsetDelta = _scrollOffset - newScrollOffset; _scrollOffsetDelta = _scrollOffset - newScrollOffset;
......
...@@ -6,24 +6,24 @@ import 'package:flutter/widgets.dart'; ...@@ -6,24 +6,24 @@ import 'package:flutter/widgets.dart';
import 'theme.dart'; import 'theme.dart';
const double _kMinScrollbarThumbLength = 18.0; const double _kMinScrollbarThumbExtent = 18.0;
const double _kScrollbarThumbGirth = 6.0; const double _kScrollbarThumbGirth = 6.0;
const Duration _kScrollbarThumbFadeDuration = const Duration(milliseconds: 300); const Duration _kScrollbarThumbFadeDuration = const Duration(milliseconds: 300);
class _ScrollbarPainter extends CustomPainter { class _Painter extends CustomPainter {
_ScrollbarPainter({ _Painter({
this.scrollOffset, this.scrollOffset,
this.scrollDirection, this.scrollDirection,
this.contentExtent, this.contentExtent,
this.containerExtent, this.containerExtent,
this.thumbColor this.color
}); });
final double scrollOffset; final double scrollOffset;
final Axis scrollDirection; final Axis scrollDirection;
final double contentExtent; final double contentExtent;
final double containerExtent; final double containerExtent;
final Color thumbColor; final Color color;
void paintScrollbar(Canvas canvas, Size size) { void paintScrollbar(Canvas canvas, Size size) {
Point thumbOrigin; Point thumbOrigin;
...@@ -32,7 +32,7 @@ class _ScrollbarPainter extends CustomPainter { ...@@ -32,7 +32,7 @@ class _ScrollbarPainter extends CustomPainter {
switch (scrollDirection) { switch (scrollDirection) {
case Axis.vertical: case Axis.vertical:
double thumbHeight = size.height * containerExtent / contentExtent; double thumbHeight = size.height * containerExtent / contentExtent;
thumbHeight = thumbHeight.clamp(_kMinScrollbarThumbLength, size.height); thumbHeight = thumbHeight.clamp(_kMinScrollbarThumbExtent, size.height);
final double maxThumbTop = size.height - thumbHeight; final double maxThumbTop = size.height - thumbHeight;
double thumbTop = (scrollOffset / (contentExtent - containerExtent)) * maxThumbTop; double thumbTop = (scrollOffset / (contentExtent - containerExtent)) * maxThumbTop;
thumbTop = thumbTop.clamp(0.0, maxThumbTop); thumbTop = thumbTop.clamp(0.0, maxThumbTop);
...@@ -41,7 +41,7 @@ class _ScrollbarPainter extends CustomPainter { ...@@ -41,7 +41,7 @@ class _ScrollbarPainter extends CustomPainter {
break; break;
case Axis.horizontal: case Axis.horizontal:
double thumbWidth = size.width * containerExtent / contentExtent; double thumbWidth = size.width * containerExtent / contentExtent;
thumbWidth = thumbWidth.clamp(_kMinScrollbarThumbLength, size.width); thumbWidth = thumbWidth.clamp(_kMinScrollbarThumbExtent, size.width);
final double maxThumbLeft = size.width - thumbWidth; final double maxThumbLeft = size.width - thumbWidth;
double thumbLeft = (scrollOffset / (contentExtent - containerExtent)) * maxThumbLeft; double thumbLeft = (scrollOffset / (contentExtent - containerExtent)) * maxThumbLeft;
thumbLeft = thumbLeft.clamp(0.0, maxThumbLeft); thumbLeft = thumbLeft.clamp(0.0, maxThumbLeft);
...@@ -50,24 +50,24 @@ class _ScrollbarPainter extends CustomPainter { ...@@ -50,24 +50,24 @@ class _ScrollbarPainter extends CustomPainter {
break; break;
} }
final Paint paint = new Paint()..color = thumbColor; final Paint paint = new Paint()..color = color;
canvas.drawRect(thumbOrigin & thumbSize, paint); canvas.drawRect(thumbOrigin & thumbSize, paint);
} }
@override @override
void paint(Canvas canvas, Size size) { void paint(Canvas canvas, Size size) {
if (scrollOffset == null || thumbColor.alpha == 0) if (scrollOffset == null || color.alpha == 0)
return; return;
paintScrollbar(canvas, size); paintScrollbar(canvas, size);
} }
@override @override
bool shouldRepaint(_ScrollbarPainter oldPainter) { bool shouldRepaint(_Painter oldPainter) {
return oldPainter.scrollOffset != scrollOffset return oldPainter.scrollOffset != scrollOffset
|| oldPainter.scrollDirection != scrollDirection || oldPainter.scrollDirection != scrollDirection
|| oldPainter.contentExtent != contentExtent || oldPainter.contentExtent != contentExtent
|| oldPainter.containerExtent != containerExtent || oldPainter.containerExtent != containerExtent
|| oldPainter.thumbColor != thumbColor; || oldPainter.color != color;
} }
} }
...@@ -102,6 +102,8 @@ class _ScrollbarState extends State<Scrollbar> { ...@@ -102,6 +102,8 @@ class _ScrollbarState extends State<Scrollbar> {
} }
void _updateState(ScrollableState scrollable) { void _updateState(ScrollableState scrollable) {
if (scrollable.scrollBehavior is! ExtentScrollBehavior)
return;
final ExtentScrollBehavior scrollBehavior = scrollable.scrollBehavior; final ExtentScrollBehavior scrollBehavior = scrollable.scrollBehavior;
_scrollOffset = scrollable.scrollOffset; _scrollOffset = scrollable.scrollOffset;
_scrollDirection = scrollable.config.scrollDirection; _scrollDirection = scrollable.config.scrollDirection;
...@@ -151,12 +153,12 @@ class _ScrollbarState extends State<Scrollbar> { ...@@ -151,12 +153,12 @@ class _ScrollbarState extends State<Scrollbar> {
animation: _opacity, animation: _opacity,
builder: (BuildContext context, Widget child) { builder: (BuildContext context, Widget child) {
return new CustomPaint( return new CustomPaint(
foregroundPainter: new _ScrollbarPainter( foregroundPainter: new _Painter(
scrollOffset: _scrollOffset, scrollOffset: _scrollOffset,
scrollDirection: _scrollDirection, scrollDirection: _scrollDirection,
containerExtent: _containerExtent, containerExtent: _containerExtent,
contentExtent: _contentExtent, contentExtent: _contentExtent,
thumbColor: Theme.of(context).highlightColor.withOpacity(_opacity.value) color: Theme.of(context).highlightColor.withOpacity(_opacity.value)
), ),
child: child child: child
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment