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

5 6
// @dart = 2.8

7 8
import 'package:flutter/painting.dart';

xster's avatar
xster committed
9 10
import 'colors.dart';

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 43 44
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
45 46
///
/// Used by [CupertinoSwitch] and [CupertinoSlider].
47
class CupertinoThumbPainter {
Adam Barth's avatar
Adam Barth committed
48
  /// Creates an object that paints an iOS-style slider thumb.
49
  const CupertinoThumbPainter({
50
    this.color = CupertinoColors.white,
51 52 53 54 55 56 57 58
    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);
59

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

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

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

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

Adam Barth's avatar
Adam Barth committed
74 75 76 77
  /// 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.
78
  void paint(Canvas canvas, Rect rect) {
79
    final RRect rrect = RRect.fromRectAndRadius(
80
      rect,
81
      Radius.circular(rect.shortestSide / 2.0),
82 83
    );

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

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