thumb_painter.dart 1.58 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';

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
  CupertinoThumbPainter({
15 16
    this.color = CupertinoColors.white,
    this.shadowColor = const Color(0x2C000000),
17 18 19 20
  }) : _shadowPaint = new BoxShadow(
         color: shadowColor,
         blurRadius: 1.0,
       ).toPaint();
21

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

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

28 29 30
  /// The paint used to draw the shadow case by the thumb.
  final Paint _shadowPaint;

Adam Barth's avatar
Adam Barth committed
31
  /// Half the default diameter of the thumb.
32
  static const double radius = 14.0;
Adam Barth's avatar
Adam Barth committed
33 34

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

Adam Barth's avatar
Adam Barth committed
37 38 39 40
  /// 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.
41
  void paint(Canvas canvas, Rect rect) {
42 43 44 45 46 47 48 49
    final RRect rrect = new RRect.fromRectAndRadius(
      rect,
      new Radius.circular(rect.shortestSide / 2.0),
    );

    canvas.drawRRect(rrect, _shadowPaint);
    canvas.drawRRect(rrect.shift(const Offset(0.0, 3.0)), _shadowPaint);
    canvas.drawRRect(rrect, new Paint()..color = color);
50 51
  }
}