thumb_painter.dart 1.58 KB
Newer Older
1 2 3 4 5 6 7 8
// 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';

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

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

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

  /// The color of the shadow case by the thumb.
23 24
  final Color shadowColor;

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

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

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

38
    final Paint paint = new Paint()
39 40 41 42 43 44 45 46 47 48 49
      ..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);
  }
}