thumb_painter.dart 1.6 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 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.

import 'package:flutter/painting.dart';

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

9 10
final MaskFilter _kShadowMaskFilter = new MaskFilter.blur(BlurStyle.normal, BoxShadow.convertRadiusToSigma(1.0));

Adam Barth's avatar
Adam Barth committed
11 12 13
/// Paints an iOS-style slider thumb.
///
/// Used by [CupertinoSwitch] and [CupertinoSlider].
14
class CupertinoThumbPainter {
Adam Barth's avatar
Adam Barth committed
15
  /// Creates an object that paints an iOS-style slider thumb.
16
  CupertinoThumbPainter({
xster's avatar
xster committed
17
    this.color: CupertinoColors.white,
18 19 20
    this.shadowColor: const Color(0x2C000000),
  });

Adam Barth's avatar
Adam Barth committed
21
  /// The color of the interior of the thumb.
22
  final Color color;
Adam Barth's avatar
Adam Barth committed
23 24

  /// The color of the shadow case by the thumb.
25 26
  final Color shadowColor;

Adam Barth's avatar
Adam Barth committed
27
  /// Half the default diameter of the thumb.
28
  static const double radius = 14.0;
Adam Barth's avatar
Adam Barth committed
29 30

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

Adam Barth's avatar
Adam Barth committed
33 34 35 36
  /// 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.
37 38 39
  void paint(Canvas canvas, Rect rect) {
    final RRect rrect = new RRect.fromRectAndRadius(rect, new Radius.circular(rect.shortestSide / 2.0));

40
    final Paint paint = new Paint()
41 42 43 44 45 46 47 48 49 50 51
      ..color = shadowColor
      ..maskFilter = _kShadowMaskFilter;
    canvas.drawRRect(rrect, paint);
    canvas.drawRRect(rrect.shift(const Offset(0.0, 3.0)), paint);

    paint
      ..color = color
      ..maskFilter = null;
    canvas.drawRRect(rrect, paint);
  }
}