scrollbar.dart 4.81 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

7
import 'package:flutter/cupertino.dart';
8 9 10 11
import 'package:flutter/widgets.dart';

import 'theme.dart';

12
const double _kScrollbarThickness = 6.0;
13 14
const Duration _kScrollbarFadeDuration = Duration(milliseconds: 300);
const Duration _kScrollbarTimeToFade = Duration(milliseconds: 600);
15

16 17 18 19 20
/// A material design scrollbar.
///
/// A scrollbar indicates which portion of a [Scrollable] widget is actually
/// visible.
///
21 22
/// Dynamically changes to an iOS style scrollbar that looks like
/// [CupertinoScrollbar] on the iOS platform.
23
///
24 25 26 27 28 29 30
/// To add a scrollbar to a [ScrollView], simply wrap the scroll view widget in
/// a [Scrollbar] widget.
///
/// See also:
///
///  * [ListView], which display a linear, scrollable list of children.
///  * [GridView], which display a 2 dimensional, scrollable array of children.
31
class Scrollbar extends StatefulWidget {
32 33 34 35
  /// Creates a material design scrollbar that wraps the given [child].
  ///
  /// The [child] should be a source of [ScrollNotification] notifications,
  /// typically a [Scrollable] widget.
36
  const Scrollbar({
37
    Key key,
38
    @required this.child,
39 40
  }) : super(key: key);

41
  /// The widget below this widget in the tree.
42
  ///
43 44 45 46
  /// The scrollbar will be stacked on top of this child. This child (and its
  /// subtree) should include a source of [ScrollNotification] notifications.
  ///
  /// Typically a [ListView] or [CustomScrollView].
47 48 49
  final Widget child;

  @override
50
  _ScrollbarState createState() => _ScrollbarState();
51 52 53
}


54 55 56 57 58
class _ScrollbarState extends State<Scrollbar> with TickerProviderStateMixin {
  ScrollbarPainter _materialPainter;
  TargetPlatform _currentPlatform;
  TextDirection _textDirection;
  Color _themeColor;
59

60 61 62
  AnimationController _fadeoutAnimationController;
  Animation<double> _fadeoutOpacityAnimation;
  Timer _fadeoutTimer;
63 64

  @override
65 66
  void initState() {
    super.initState();
67
    _fadeoutAnimationController = AnimationController(
68 69 70
      vsync: this,
      duration: _kScrollbarFadeDuration,
    );
71
    _fadeoutOpacityAnimation = CurvedAnimation(
72
      parent: _fadeoutAnimationController,
73
      curve: Curves.fastOutSlowIn,
74
    );
75 76
  }

77
  @override
78 79
  void didChangeDependencies() {
    super.didChangeDependencies();
80

81 82
    final ThemeData theme = Theme.of(context);
    _currentPlatform = theme.platform;
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97
    switch (_currentPlatform) {
      case TargetPlatform.iOS:
        // On iOS, stop all local animations. CupertinoScrollbar has its own
        // animations.
        _fadeoutTimer?.cancel();
        _fadeoutTimer = null;
        _fadeoutAnimationController.reset();
        break;
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
        _themeColor = theme.highlightColor.withOpacity(1.0);
        _textDirection = Directionality.of(context);
        _materialPainter = _buildMaterialScrollbarPainter();
        break;
98 99 100
    }
  }

101
  ScrollbarPainter _buildMaterialScrollbarPainter() {
102
    return ScrollbarPainter(
103 104 105 106 107
        color: _themeColor,
        textDirection: _textDirection,
        thickness: _kScrollbarThickness,
        fadeoutOpacityAnimation: _fadeoutOpacityAnimation,
      );
108 109
  }

110 111 112 113 114 115 116 117 118 119 120 121
  bool _handleScrollNotification(ScrollNotification notification) {
    // iOS sub-delegates to the CupertinoScrollbar instead and doesn't handle
    // scroll notifications here.
    if (_currentPlatform != TargetPlatform.iOS
        && (notification is ScrollUpdateNotification
            || notification is OverscrollNotification)) {
      if (_fadeoutAnimationController.status != AnimationStatus.forward) {
        _fadeoutAnimationController.forward();
      }

      _materialPainter.update(notification.metrics, notification.metrics.axisDirection);
      _fadeoutTimer?.cancel();
122
      _fadeoutTimer = Timer(_kScrollbarTimeToFade, () {
123 124 125 126 127
        _fadeoutAnimationController.reverse();
        _fadeoutTimer = null;
      });
    }
    return false;
128 129
  }

130 131 132 133 134 135
  @override
  void dispose() {
    _fadeoutAnimationController.dispose();
    _fadeoutTimer?.cancel();
    _materialPainter?.dispose();
    super.dispose();
136 137
  }

138
  @override
139 140 141
  Widget build(BuildContext context) {
    switch (_currentPlatform) {
      case TargetPlatform.iOS:
142
        return CupertinoScrollbar(
143 144 145 146
          child: widget.child,
        );
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
147
        return NotificationListener<ScrollNotification>(
148
          onNotification: _handleScrollNotification,
149 150
          child: RepaintBoundary(
            child: CustomPaint(
151
              foregroundPainter: _materialPainter,
152
              child: RepaintBoundary(
153 154 155 156 157
                child: widget.child,
              ),
            ),
          ),
        );
158
    }
159
    throw FlutterError('Unknown platform for scrollbar insertion');
160 161
  }
}