Commit d11acc41 authored by Hixie's avatar Hixie

Use the presence of handler to determine 'enabled'

Instread of an explicit 'enabled' bool, this uses the presence of the
event handler to determine if a widget is enabled or not. This means
that if you've not passed a handler, your widget will be disabled, which
makes sense, since it wouldn't work anyway.

Adds this feature to checkbox, and ports raised button, flat button, and
radio buttons to this new model.

Adds a checkbox to card_collection that can be disabled.

Hide a (basically bogus) hint from the (soon to be disabled) strong hint
mode in the analyzer that this reveals.
parent ddd4ea81
...@@ -91,7 +91,6 @@ class StockHomeState extends State<StockHome> { ...@@ -91,7 +91,6 @@ class StockHomeState extends State<StockHome> {
actions: <Widget>[ actions: <Widget>[
new FlatButton( new FlatButton(
child: new Text('USE IT'), child: new Text('USE IT'),
enabled: false,
onPressed: () { onPressed: () {
config.navigator.pop(false); config.navigator.pop(false);
} }
......
...@@ -42,6 +42,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -42,6 +42,7 @@ class CardCollectionState extends State<CardCollection> {
bool _snapToCenter = false; bool _snapToCenter = false;
bool _fixedSizeCards = false; bool _fixedSizeCards = false;
bool _sunshine = false; bool _sunshine = false;
bool _varyFontSizes = false;
InvalidatorCallback _invalidator; InvalidatorCallback _invalidator;
Size _cardCollectionSize = new Size(200.0, 200.0); Size _cardCollectionSize = new Size(200.0, 200.0);
...@@ -127,6 +128,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -127,6 +128,7 @@ class CardCollectionState extends State<CardCollection> {
buildDrawerCheckbox("Snap fling scrolls to center", _snapToCenter, _toggleSnapToCenter), buildDrawerCheckbox("Snap fling scrolls to center", _snapToCenter, _toggleSnapToCenter),
buildDrawerCheckbox("Fixed size cards", _fixedSizeCards, _toggleFixedSizeCards), buildDrawerCheckbox("Fixed size cards", _fixedSizeCards, _toggleFixedSizeCards),
buildDrawerCheckbox("Let the sun shine", _sunshine, _toggleSunshine), buildDrawerCheckbox("Let the sun shine", _sunshine, _toggleSunshine),
buildDrawerCheckbox("Vary font sizes", _varyFontSizes, _toggleVaryFontSizes, enabled: !_editable),
new DrawerDivider(), new DrawerDivider(),
buildDrawerColorRadioItem("Deep Purple", Colors.deepPurple, _primaryColor, _selectColor), buildDrawerColorRadioItem("Deep Purple", Colors.deepPurple, _primaryColor, _selectColor),
buildDrawerColorRadioItem("Green", Colors.green, _primaryColor, _selectColor), buildDrawerColorRadioItem("Green", Colors.green, _primaryColor, _selectColor),
...@@ -181,6 +183,12 @@ class CardCollectionState extends State<CardCollection> { ...@@ -181,6 +183,12 @@ class CardCollectionState extends State<CardCollection> {
}); });
} }
void _toggleVaryFontSizes() {
setState(() {
_varyFontSizes = !_varyFontSizes;
});
}
void _selectColor(Map<int, Color> selection) { void _selectColor(Map<int, Color> selection) {
setState(() { setState(() {
_primaryColor = selection; _primaryColor = selection;
...@@ -199,12 +207,15 @@ class CardCollectionState extends State<CardCollection> { ...@@ -199,12 +207,15 @@ class CardCollectionState extends State<CardCollection> {
}); });
} }
Widget buildDrawerCheckbox(String label, bool value, void callback()) { Widget buildDrawerCheckbox(String label, bool value, void callback(), { bool enabled: true }) {
return new DrawerItem( return new DrawerItem(
onPressed: callback, onPressed: enabled ? callback : null,
child: new Row(<Widget>[ child: new Row(<Widget>[
new Flexible(child: new Text(label)), new Flexible(child: new Text(label)),
new Checkbox(value: value, onChanged: (_) { callback(); }) new Checkbox(
value: value,
onChanged: enabled ? (_) { callback(); } : null
)
]) ])
); );
} }
...@@ -218,8 +229,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -218,8 +229,7 @@ class CardCollectionState extends State<CardCollection> {
new Radio<Map<int, Color>>( new Radio<Map<int, Color>>(
value: itemValue, value: itemValue,
groupValue: currentValue, groupValue: currentValue,
enabled: enabled, onChanged: enabled ? onChanged : null
onChanged: onChanged
) )
]) ])
); );
...@@ -234,8 +244,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -234,8 +244,7 @@ class CardCollectionState extends State<CardCollection> {
new Radio<DismissDirection>( new Radio<DismissDirection>(
value: itemValue, value: itemValue,
groupValue: currentValue, groupValue: currentValue,
enabled: enabled, onChanged: enabled ? onChanged : null
onChanged: onChanged
) )
]) ])
); );
...@@ -250,8 +259,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -250,8 +259,7 @@ class CardCollectionState extends State<CardCollection> {
new Radio<TextStyle>( new Radio<TextStyle>(
value: itemValue, value: itemValue,
groupValue: currentValue, groupValue: currentValue,
enabled: enabled, onChanged: enabled ? onChanged : null
onChanged: onChanged
) )
]) ])
); );
...@@ -292,7 +300,9 @@ class CardCollectionState extends State<CardCollection> { ...@@ -292,7 +300,9 @@ class CardCollectionState extends State<CardCollection> {
) )
) )
: new DefaultTextStyle( : new DefaultTextStyle(
style: DefaultTextStyle.of(context).merge(cardLabelStyle).merge(_textStyle), style: DefaultTextStyle.of(context).merge(cardLabelStyle).merge(_textStyle).copyWith(
fontSize: _varyFontSizes ? 5.0 + _cardModels.length.toDouble() : null
),
child: new Column(<Widget>[ child: new Column(<Widget>[
new Text(cardModel.label) new Text(cardModel.label)
], ],
......
...@@ -26,9 +26,7 @@ class ContainerApp extends StatelessComponent { ...@@ -26,9 +26,7 @@ class ContainerApp extends StatelessComponent {
onPressed: () => print("Hello World") onPressed: () => print("Hello World")
), ),
new RaisedButton( new RaisedButton(
child: new Text('DISABLED'), child: new Text('DISABLED')
onPressed: () => print("Hello World"),
enabled: false
) )
]) ])
), ),
......
...@@ -73,7 +73,6 @@ class SectorAppState extends State<SectorApp> { ...@@ -73,7 +73,6 @@ class SectorAppState extends State<SectorApp> {
padding: new EdgeDims.symmetric(horizontal: 8.0, vertical: 25.0), padding: new EdgeDims.symmetric(horizontal: 8.0, vertical: 25.0),
child: new Row(<Widget>[ child: new Row(<Widget>[
new RaisedButton( new RaisedButton(
enabled: _enabledAdd,
child: new IntrinsicWidth( child: new IntrinsicWidth(
child: new Row(<Widget>[ child: new Row(<Widget>[
new Container( new Container(
...@@ -84,10 +83,9 @@ class SectorAppState extends State<SectorApp> { ...@@ -84,10 +83,9 @@ class SectorAppState extends State<SectorApp> {
new Text('ADD SECTOR'), new Text('ADD SECTOR'),
]) ])
), ),
onPressed: addSector onPressed: _enabledAdd ? addSector : null
), ),
new RaisedButton( new RaisedButton(
enabled: _enabledRemove,
child: new IntrinsicWidth( child: new IntrinsicWidth(
child: new Row(<Widget>[ child: new Row(<Widget>[
new Container( new Container(
...@@ -98,7 +96,7 @@ class SectorAppState extends State<SectorApp> { ...@@ -98,7 +96,7 @@ class SectorAppState extends State<SectorApp> {
new Text('REMOVE SECTOR'), new Text('REMOVE SECTOR'),
]) ])
), ),
onPressed: removeSector onPressed: _enabledRemove ? removeSector : null
) )
], ],
justifyContent: FlexJustifyContent.spaceAround justifyContent: FlexJustifyContent.spaceAround
......
...@@ -7,11 +7,10 @@ import 'dart:ui' as ui; ...@@ -7,11 +7,10 @@ import 'dart:ui' as ui;
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'colors.dart';
import 'theme.dart'; import 'theme.dart';
const double _kMidpoint = 0.5; const double _kMidpoint = 0.5;
const Color _kLightUncheckedColor = const Color(0x8A000000);
const Color _kDarkUncheckedColor = const Color(0xB2FFFFFF);
const double _kEdgeSize = 18.0; const double _kEdgeSize = 18.0;
const double _kEdgeRadius = 1.0; const double _kEdgeRadius = 1.0;
const double _kStrokeWidth = 2.0; const double _kStrokeWidth = 2.0;
...@@ -30,21 +29,37 @@ class Checkbox extends StatelessComponent { ...@@ -30,21 +29,37 @@ class Checkbox extends StatelessComponent {
/// ///
/// * `value` determines whether the checkbox is checked. /// * `value` determines whether the checkbox is checked.
/// * `onChanged` is called whenever the state of the checkbox should change. /// * `onChanged` is called whenever the state of the checkbox should change.
const Checkbox({Key key, this.value, this.onChanged}) : super(key: key); const Checkbox({
Key key,
this.value,
this.onChanged
}) : super(key: key);
final bool value; final bool value;
final ValueChanged<bool> onChanged; final ValueChanged<bool> onChanged;
bool get enabled => onChanged != null;
Widget build(BuildContext context) { Widget build(BuildContext context) {
ThemeData themeData = Theme.of(context); ThemeData themeData = Theme.of(context);
Color uncheckedColor = themeData.brightness == ThemeBrightness.light if (enabled) {
? _kLightUncheckedColor Color uncheckedColor = themeData.brightness == ThemeBrightness.light
: _kDarkUncheckedColor; ? Colors.black54
: Colors.white70;
return new _CheckboxWrapper(
value: value,
onChanged: onChanged,
uncheckedColor: uncheckedColor,
accentColor: themeData.accentColor
);
}
Color disabledColor = themeData.brightness == ThemeBrightness.light
? Colors.black26
: Colors.white30;
return new _CheckboxWrapper( return new _CheckboxWrapper(
value: value, value: value,
onChanged: onChanged, uncheckedColor: disabledColor,
uncheckedColor: uncheckedColor, accentColor: disabledColor
accentColor: themeData.accentColor
); );
} }
} }
......
...@@ -12,11 +12,9 @@ class FlatButton extends MaterialButton { ...@@ -12,11 +12,9 @@ class FlatButton extends MaterialButton {
FlatButton({ FlatButton({
Key key, Key key,
Widget child, Widget child,
bool enabled: true,
GestureTapCallback onPressed GestureTapCallback onPressed
}) : super(key: key, }) : super(key: key,
child: child, child: child,
enabled: enabled,
onPressed: onPressed); onPressed: onPressed);
_FlatButtonState createState() => new _FlatButtonState(); _FlatButtonState createState() => new _FlatButtonState();
......
...@@ -36,18 +36,16 @@ abstract class MaterialButton extends StatefulComponent { ...@@ -36,18 +36,16 @@ abstract class MaterialButton extends StatefulComponent {
MaterialButton({ MaterialButton({
Key key, Key key,
this.child, this.child,
this.enabled: true,
this.textColor, this.textColor,
this.onPressed this.onPressed
}) : super(key: key) { }) : super(key: key);
assert(enabled != null);
}
final Widget child; final Widget child;
final bool enabled;
final ButtonColor textColor; final ButtonColor textColor;
final GestureTapCallback onPressed; final GestureTapCallback onPressed;
bool get enabled => onPressed != null;
void debugFillDescription(List<String> description) { void debugFillDescription(List<String> description) {
super.debugFillDescription(description); super.debugFillDescription(description);
if (!enabled) if (!enabled)
......
...@@ -12,19 +12,17 @@ import 'theme.dart'; ...@@ -12,19 +12,17 @@ import 'theme.dart';
class Radio<T> extends StatelessComponent { class Radio<T> extends StatelessComponent {
Radio({ Radio({
Key key, Key key,
this.enabled,
this.value, this.value,
this.groupValue, this.groupValue,
this.onChanged this.onChanged
}) : super(key: key) { }) : super(key: key);
assert(onChanged != null);
}
final bool enabled;
final T value; final T value;
final T groupValue; final T groupValue;
final ValueChanged<T> onChanged; final ValueChanged<T> onChanged;
bool get enabled => onChanged != null;
Color _getColor(BuildContext context) { Color _getColor(BuildContext context) {
ThemeData themeData = Theme.of(context); ThemeData themeData = Theme.of(context);
if (!enabled) if (!enabled)
......
...@@ -12,14 +12,10 @@ class RaisedButton extends MaterialButton { ...@@ -12,14 +12,10 @@ class RaisedButton extends MaterialButton {
RaisedButton({ RaisedButton({
Key key, Key key,
Widget child, Widget child,
bool enabled: true,
GestureTapCallback onPressed GestureTapCallback onPressed
}) : super(key: key, }) : super(key: key,
child: child, child: child,
enabled: enabled, onPressed: onPressed);
onPressed: onPressed) {
assert(enabled != null);
}
_RaisedButtonState createState() => new _RaisedButtonState(); _RaisedButtonState createState() => new _RaisedButtonState();
} }
......
...@@ -19,10 +19,12 @@ const Duration _kToggleDuration = const Duration(milliseconds: 200); ...@@ -19,10 +19,12 @@ const Duration _kToggleDuration = const Duration(milliseconds: 200);
// ValueChanged on a tap gesture and driving a changed animation. Subclasses are // ValueChanged on a tap gesture and driving a changed animation. Subclasses are
// responsible for painting. // responsible for painting.
abstract class RenderToggleable extends RenderConstrainedBox { abstract class RenderToggleable extends RenderConstrainedBox {
RenderToggleable({bool value, Size size, ValueChanged<bool> onChanged}) RenderToggleable({
: _value = value, bool value,
_onChanged = onChanged, Size size,
super(additionalConstraints: new BoxConstraints.tight(size)) { this.onChanged
}) : _value = value,
super(additionalConstraints: new BoxConstraints.tight(size)) {
_performance = new ValuePerformance<double>( _performance = new ValuePerformance<double>(
variable: new AnimatedValue<double>(0.0, end: 1.0, curve: Curves.easeIn, reverseCurve: Curves.easeOut), variable: new AnimatedValue<double>(0.0, end: 1.0, curve: Curves.easeIn, reverseCurve: Curves.easeOut),
duration: _kToggleDuration, duration: _kToggleDuration,
...@@ -30,16 +32,22 @@ abstract class RenderToggleable extends RenderConstrainedBox { ...@@ -30,16 +32,22 @@ abstract class RenderToggleable extends RenderConstrainedBox {
)..addListener(markNeedsPaint); )..addListener(markNeedsPaint);
} }
bool get value => _value;
bool _value;
void set value(bool value) {
if (value == _value)
return;
_value = value;
performance.play(value ? AnimationDirection.forward : AnimationDirection.reverse);
}
ValueChanged<bool> onChanged;
ValuePerformance<double> get performance => _performance; ValuePerformance<double> get performance => _performance;
ValuePerformance<double> _performance; ValuePerformance<double> _performance;
double get position => _performance.value; double get position => _performance.value;
void handleEvent(InputEvent event, BoxHitTestEntry entry) {
if (event.type == 'pointerdown')
_tap.addPointer(event);
}
TapGestureRecognizer _tap; TapGestureRecognizer _tap;
void attach() { void attach() {
...@@ -56,23 +64,13 @@ abstract class RenderToggleable extends RenderConstrainedBox { ...@@ -56,23 +64,13 @@ abstract class RenderToggleable extends RenderConstrainedBox {
super.detach(); super.detach();
} }
void _handleTap() { void handleEvent(InputEvent event, BoxHitTestEntry entry) {
if (_onChanged != null) if (event.type == 'pointerdown' && onChanged != null)
_onChanged(!_value); _tap.addPointer(event);
}
bool get value => _value;
bool _value;
void set value(bool value) {
if (value == _value)
return;
_value = value;
performance.play(value ? AnimationDirection.forward : AnimationDirection.reverse);
} }
ValueChanged<bool> get onChanged => _onChanged; void _handleTap() {
ValueChanged<bool> _onChanged; if (onChanged != null)
void set onChanged(ValueChanged<bool> onChanged) { onChanged(!_value);
_onChanged = onChanged;
} }
} }
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