thumb_painter.dart 2.43 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/painting.dart';

xster's avatar
xster committed
7 8
import 'colors.dart';

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
const Color _kThumbBorderColor = Color(0x0A000000);

const List<BoxShadow> _kSwitchBoxShadows = <BoxShadow> [
  BoxShadow(
    color: Color(0x26000000),
    offset: Offset(0, 3),
    blurRadius: 8.0,
  ),
  BoxShadow(
    color: Color(0x0F000000),
    offset: Offset(0, 3),
    blurRadius: 1.0,
  ),
];

const List<BoxShadow> _kSliderBoxShadows = <BoxShadow> [
  BoxShadow(
    color: Color(0x26000000),
    offset: Offset(0, 3),
    blurRadius: 8.0,
  ),
  BoxShadow(
    color: Color(0x29000000),
    offset: Offset(0, 1),
    blurRadius: 1.0,
  ),
  BoxShadow(
    color: Color(0x1A000000),
    offset: Offset(0, 3),
    blurRadius: 1.0,
  ),
];

/// Paints an iOS-style slider thumb or switch thumb.
Adam Barth's avatar
Adam Barth committed
43 44
///
/// Used by [CupertinoSwitch] and [CupertinoSlider].
45
class CupertinoThumbPainter {
Adam Barth's avatar
Adam Barth committed
46
  /// Creates an object that paints an iOS-style slider thumb.
47
  const CupertinoThumbPainter({
48
    this.color = CupertinoColors.white,
49 50 51 52 53 54 55 56
    this.shadows = _kSliderBoxShadows,
  }) : assert(shadows != null);

  /// Creates an object that paints an iOS-style switch thumb.
  const CupertinoThumbPainter.switchThumb({
    Color color = CupertinoColors.white,
    List<BoxShadow> shadows = _kSwitchBoxShadows,
  }) : this(color: color, shadows: shadows);
57

Adam Barth's avatar
Adam Barth committed
58
  /// The color of the interior of the thumb.
59
  final Color color;
Adam Barth's avatar
Adam Barth committed
60

61 62 63 64
  /// The list of [BoxShadow] to paint below the thumb.
  ///
  /// Must not be null.
  final List<BoxShadow> shadows;
65

Adam Barth's avatar
Adam Barth committed
66
  /// Half the default diameter of the thumb.
67
  static const double radius = 14.0;
Adam Barth's avatar
Adam Barth committed
68 69

  /// The default amount the thumb should be extended horizontally when pressed.
70 71
  static const double extension = 7.0;

Adam Barth's avatar
Adam Barth committed
72 73 74 75
  /// Paints the thumb onto the given canvas in the given rectangle.
  ///
  /// Consider using [radius] and [extension] when deciding how large a
  /// rectangle to use for the thumb.
76
  void paint(Canvas canvas, Rect rect) {
77
    final RRect rrect = RRect.fromRectAndRadius(
78
      rect,
79
      Radius.circular(rect.shortestSide / 2.0),
80 81
    );

82
    for (final BoxShadow shadow in shadows) {
83
      canvas.drawRRect(rrect.shift(shadow.offset), shadow.toPaint());
84
    }
85 86 87 88 89

    canvas.drawRRect(
      rrect.inflate(0.5),
      Paint()..color = _kThumbBorderColor,
    );
90
    canvas.drawRRect(rrect, Paint()..color = color);
91 92
  }
}