| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 import 'dart:sky' as sky; |
| 6 |
| 7 import 'package:sky/theme2/colors.dart' as colors; |
| 8 import 'package:sky/theme2/shadows.dart'; |
| 9 |
| 10 import '../painting/shadows.dart'; |
| 11 import '../rendering/box.dart'; |
| 12 import 'basic.dart'; |
| 13 import 'toggleable.dart'; |
| 14 |
| 15 // TODO(jackson): This should change colors with the theme |
| 16 sky.Color _kThumbOnColor = colors.Purple[500]; |
| 17 const sky.Color _kThumbOffColor = const sky.Color(0xFFFAFAFA); |
| 18 sky.Color _kTrackOnColor = new sky.Color(_kThumbOnColor.value & (0x80 << 24)); |
| 19 const sky.Color _kTrackOffColor = const sky.Color(0x42000000); |
| 20 const double _kSwitchWidth = 35.0; |
| 21 const double _kThumbRadius = 10.0; |
| 22 const double _kSwitchHeight = _kThumbRadius * 2.0; |
| 23 const double _kTrackHeight = 14.0; |
| 24 const double _kTrackRadius = _kTrackHeight / 2.0; |
| 25 const double _kTrackWidth = _kSwitchWidth - (_kThumbRadius - _kTrackRadius) * 2.
0; |
| 26 |
| 27 class Switch extends Toggleable { |
| 28 // TODO(jackson): Hit-test the switch so that it can respond to both taps and
swipe gestures |
| 29 |
| 30 Switch({ |
| 31 Object key, |
| 32 bool value, |
| 33 ValueChanged onChanged |
| 34 }) : super(key: key, value: value, onChanged: onChanged); |
| 35 |
| 36 Size get size => const Size(_kSwitchWidth + 2.0, _kSwitchHeight + 2.0); |
| 37 |
| 38 void customPaintCallback(sky.Canvas canvas, Size size) { |
| 39 sky.Color thumbColor = value ? _kThumbOnColor : _kThumbOffColor; |
| 40 sky.Color trackColor = value ? _kTrackOnColor : _kTrackOffColor; |
| 41 |
| 42 // Draw the track rrect |
| 43 sky.Paint paint = new sky.Paint()..color = trackColor; |
| 44 paint.setStyle(sky.PaintingStyle.fill); |
| 45 sky.Rect rect = new sky.Rect.fromLTRB( |
| 46 0.0, |
| 47 _kSwitchHeight / 2.0 - _kTrackHeight / 2.0, |
| 48 _kTrackWidth, |
| 49 _kSwitchHeight / 2.0 + _kTrackHeight / 2.0 |
| 50 ); |
| 51 sky.RRect rrect = new sky.RRect()..setRectXY(rect, _kTrackRadius, _kTrackRad
ius); |
| 52 canvas.drawRRect(rrect, paint); |
| 53 |
| 54 // Draw the raised thumb with a shadow |
| 55 paint.color = thumbColor; |
| 56 var builder = new ShadowDrawLooperBuilder(); |
| 57 for (BoxShadow boxShadow in shadows[1]) |
| 58 builder.addShadow(boxShadow.offset, boxShadow.color, boxShadow.blur); |
| 59 paint.setDrawLooper(builder.build()); |
| 60 |
| 61 // The thumb contracts slightly during the animation |
| 62 double inset = 2.0 - (toggleAnimation.value - 0.5).abs() * 2.0; |
| 63 Point thumbPos = new Point( |
| 64 _kTrackRadius + toggleAnimation.value * (_kTrackWidth - _kTrackRadius * 2)
, |
| 65 _kSwitchHeight / 2.0 |
| 66 ); |
| 67 canvas.drawCircle(thumbPos.x, thumbPos.y, _kThumbRadius - inset, paint); |
| 68 } |
| 69 } |
| OLD | NEW |