Commit 5f29d950 authored by Adam Barth's avatar Adam Barth

Let clients of Checkbox, Radio, Slider, and Switch customize the activeColor

We might want to let folks customize the inactive color too, but customizing
the active color is a good place to start.
parent 3b3d5983
...@@ -29,10 +29,12 @@ class Checkbox extends StatelessComponent { ...@@ -29,10 +29,12 @@ class Checkbox extends StatelessComponent {
const Checkbox({ const Checkbox({
Key key, Key key,
this.value, this.value,
this.activeColor,
this.onChanged this.onChanged
}) : super(key: key); }) : super(key: key);
final bool value; final bool value;
final Color activeColor;
final ValueChanged<bool> onChanged; final ValueChanged<bool> onChanged;
Widget build(BuildContext context) { Widget build(BuildContext context) {
...@@ -40,7 +42,7 @@ class Checkbox extends StatelessComponent { ...@@ -40,7 +42,7 @@ class Checkbox extends StatelessComponent {
ThemeData themeData = Theme.of(context); ThemeData themeData = Theme.of(context);
return new _CheckboxRenderObjectWidget( return new _CheckboxRenderObjectWidget(
value: value, value: value,
activeColor: themeData.accentColor, activeColor: activeColor ?? themeData.accentColor,
inactiveColor: onChanged != null ? themeData.unselectedColor : themeData.disabledColor, inactiveColor: onChanged != null ? themeData.unselectedColor : themeData.disabledColor,
onChanged: onChanged onChanged: onChanged
); );
......
...@@ -21,11 +21,13 @@ class Radio<T> extends StatelessComponent { ...@@ -21,11 +21,13 @@ class Radio<T> extends StatelessComponent {
Key key, Key key,
this.value, this.value,
this.groupValue, this.groupValue,
this.activeColor,
this.onChanged this.onChanged
}) : super(key: key); }) : super(key: key);
final T value; final T value;
final T groupValue; final T groupValue;
final Color activeColor;
final ValueChanged<T> onChanged; final ValueChanged<T> onChanged;
bool get _enabled => onChanged != null; bool get _enabled => onChanged != null;
...@@ -44,7 +46,7 @@ class Radio<T> extends StatelessComponent { ...@@ -44,7 +46,7 @@ class Radio<T> extends StatelessComponent {
ThemeData themeData = Theme.of(context); ThemeData themeData = Theme.of(context);
return new _RadioRenderObjectWidget( return new _RadioRenderObjectWidget(
selected: value == groupValue, selected: value == groupValue,
activeColor: themeData.accentColor, activeColor: activeColor ?? themeData.accentColor,
inactiveColor: _getInactiveColor(themeData), inactiveColor: _getInactiveColor(themeData),
onChanged: _enabled ? _handleChanged : null onChanged: _enabled ? _handleChanged : null
); );
......
...@@ -19,6 +19,7 @@ class Slider extends StatelessComponent { ...@@ -19,6 +19,7 @@ class Slider extends StatelessComponent {
this.value, this.value,
this.min: 0.0, this.min: 0.0,
this.max: 1.0, this.max: 1.0,
this.activeColor,
this.onChanged this.onChanged
}) : super(key: key) { }) : super(key: key) {
assert(value != null); assert(value != null);
...@@ -30,6 +31,7 @@ class Slider extends StatelessComponent { ...@@ -30,6 +31,7 @@ class Slider extends StatelessComponent {
final double value; final double value;
final double min; final double min;
final double max; final double max;
final Color activeColor;
final ValueChanged<double> onChanged; final ValueChanged<double> onChanged;
void _handleChanged(double value) { void _handleChanged(double value) {
...@@ -41,29 +43,29 @@ class Slider extends StatelessComponent { ...@@ -41,29 +43,29 @@ class Slider extends StatelessComponent {
assert(debugCheckHasMaterial(context)); assert(debugCheckHasMaterial(context));
return new _SliderRenderObjectWidget( return new _SliderRenderObjectWidget(
value: (value - min) / (max - min), value: (value - min) / (max - min),
primaryColor: Theme.of(context).accentColor, activeColor: activeColor ?? Theme.of(context).accentColor,
onChanged: onChanged != null ? _handleChanged : null onChanged: onChanged != null ? _handleChanged : null
); );
} }
} }
class _SliderRenderObjectWidget extends LeafRenderObjectWidget { class _SliderRenderObjectWidget extends LeafRenderObjectWidget {
_SliderRenderObjectWidget({ Key key, this.value, this.primaryColor, this.onChanged }) _SliderRenderObjectWidget({ Key key, this.value, this.activeColor, this.onChanged })
: super(key: key); : super(key: key);
final double value; final double value;
final Color primaryColor; final Color activeColor;
final ValueChanged<double> onChanged; final ValueChanged<double> onChanged;
_RenderSlider createRenderObject() => new _RenderSlider( _RenderSlider createRenderObject() => new _RenderSlider(
value: value, value: value,
primaryColor: primaryColor, activeColor: activeColor,
onChanged: onChanged onChanged: onChanged
); );
void updateRenderObject(_RenderSlider renderObject, _SliderRenderObjectWidget oldWidget) { void updateRenderObject(_RenderSlider renderObject, _SliderRenderObjectWidget oldWidget) {
renderObject.value = value; renderObject.value = value;
renderObject.primaryColor = primaryColor; renderObject.activeColor = activeColor;
renderObject.onChanged = onChanged; renderObject.onChanged = onChanged;
} }
} }
...@@ -78,10 +80,10 @@ final Color _kActiveTrackColor = Colors.grey[500]; ...@@ -78,10 +80,10 @@ final Color _kActiveTrackColor = Colors.grey[500];
class _RenderSlider extends RenderConstrainedBox { class _RenderSlider extends RenderConstrainedBox {
_RenderSlider({ _RenderSlider({
double value, double value,
Color primaryColor, Color activeColor,
this.onChanged this.onChanged
}) : _value = value, }) : _value = value,
_primaryColor = primaryColor, _activeColor = activeColor,
super(additionalConstraints: const BoxConstraints.tightFor(width: _kTrackWidth + 2 * _kReactionRadius, height: 2 * _kReactionRadius)) { super(additionalConstraints: const BoxConstraints.tightFor(width: _kTrackWidth + 2 * _kReactionRadius, height: 2 * _kReactionRadius)) {
assert(value != null && value >= 0.0 && value <= 1.0); assert(value != null && value >= 0.0 && value <= 1.0);
_drag = new HorizontalDragGestureRecognizer(router: FlutterBinding.instance.pointerRouter) _drag = new HorizontalDragGestureRecognizer(router: FlutterBinding.instance.pointerRouter)
...@@ -104,12 +106,12 @@ class _RenderSlider extends RenderConstrainedBox { ...@@ -104,12 +106,12 @@ class _RenderSlider extends RenderConstrainedBox {
markNeedsPaint(); markNeedsPaint();
} }
Color get primaryColor => _primaryColor; Color get activeColor => _activeColor;
Color _primaryColor; Color _activeColor;
void set primaryColor(Color value) { void set activeColor(Color value) {
if (value == _primaryColor) if (value == _activeColor)
return; return;
_primaryColor = value; _activeColor = value;
markNeedsPaint(); markNeedsPaint();
} }
...@@ -168,7 +170,7 @@ class _RenderSlider extends RenderConstrainedBox { ...@@ -168,7 +170,7 @@ class _RenderSlider extends RenderConstrainedBox {
double trackRight = trackLeft + trackLength; double trackRight = trackLeft + trackLength;
double trackActive = trackLeft + trackLength * value; double trackActive = trackLeft + trackLength * value;
Paint primaryPaint = new Paint()..color = enabled ? _primaryColor : _kInactiveTrackColor; Paint primaryPaint = new Paint()..color = enabled ? _activeColor : _kInactiveTrackColor;
Paint trackPaint = new Paint()..color = _active ? _kActiveTrackColor : _kInactiveTrackColor; Paint trackPaint = new Paint()..color = _active ? _kActiveTrackColor : _kInactiveTrackColor;
double thumbRadius = enabled ? _kThumbRadius : _kThumbRadiusDisabled; double thumbRadius = enabled ? _kThumbRadius : _kThumbRadiusDisabled;
...@@ -179,7 +181,7 @@ class _RenderSlider extends RenderConstrainedBox { ...@@ -179,7 +181,7 @@ class _RenderSlider extends RenderConstrainedBox {
Point activeLocation = new Point(trackActive, trackCenter); Point activeLocation = new Point(trackActive, trackCenter);
if (_reaction.status != PerformanceStatus.dismissed) { if (_reaction.status != PerformanceStatus.dismissed) {
Paint reactionPaint = new Paint()..color = _primaryColor.withAlpha(kRadialReactionAlpha); Paint reactionPaint = new Paint()..color = _activeColor.withAlpha(kRadialReactionAlpha);
canvas.drawCircle(activeLocation, _reaction.value, reactionPaint); canvas.drawCircle(activeLocation, _reaction.value, reactionPaint);
} }
canvas.drawCircle(activeLocation, thumbRadius, primaryPaint); canvas.drawCircle(activeLocation, thumbRadius, primaryPaint);
......
...@@ -17,10 +17,11 @@ import 'theme.dart'; ...@@ -17,10 +17,11 @@ import 'theme.dart';
import 'toggleable.dart'; import 'toggleable.dart';
class Switch extends StatelessComponent { class Switch extends StatelessComponent {
Switch({ Key key, this.value, this.onChanged }) Switch({ Key key, this.value, this.activeColor, this.onChanged })
: super(key: key); : super(key: key);
final bool value; final bool value;
final Color activeColor;
final ValueChanged<bool> onChanged; final ValueChanged<bool> onChanged;
Widget build(BuildContext context) { Widget build(BuildContext context) {
...@@ -28,7 +29,7 @@ class Switch extends StatelessComponent { ...@@ -28,7 +29,7 @@ class Switch extends StatelessComponent {
ThemeData themeData = Theme.of(context); ThemeData themeData = Theme.of(context);
final isDark = themeData.brightness == ThemeBrightness.dark; final isDark = themeData.brightness == ThemeBrightness.dark;
Color activeThumbColor = themeData.accentColor; Color activeThumbColor = activeColor ?? themeData.accentColor;
Color activeTrackColor = activeThumbColor.withAlpha(0x80); Color activeTrackColor = activeThumbColor.withAlpha(0x80);
Color inactiveThumbColor; Color inactiveThumbColor;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment