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.
This commit is contained in:
Hixie
2015-10-26 14:51:05 -07:00
parent ddd4ea81ec
commit d11acc41eb
10 changed files with 78 additions and 70 deletions

View File

@@ -91,7 +91,6 @@ class StockHomeState extends State<StockHome> {
actions: <Widget>[
new FlatButton(
child: new Text('USE IT'),
enabled: false,
onPressed: () {
config.navigator.pop(false);
}

View File

@@ -42,6 +42,7 @@ class CardCollectionState extends State<CardCollection> {
bool _snapToCenter = false;
bool _fixedSizeCards = false;
bool _sunshine = false;
bool _varyFontSizes = false;
InvalidatorCallback _invalidator;
Size _cardCollectionSize = new Size(200.0, 200.0);
@@ -127,6 +128,7 @@ class CardCollectionState extends State<CardCollection> {
buildDrawerCheckbox("Snap fling scrolls to center", _snapToCenter, _toggleSnapToCenter),
buildDrawerCheckbox("Fixed size cards", _fixedSizeCards, _toggleFixedSizeCards),
buildDrawerCheckbox("Let the sun shine", _sunshine, _toggleSunshine),
buildDrawerCheckbox("Vary font sizes", _varyFontSizes, _toggleVaryFontSizes, enabled: !_editable),
new DrawerDivider(),
buildDrawerColorRadioItem("Deep Purple", Colors.deepPurple, _primaryColor, _selectColor),
buildDrawerColorRadioItem("Green", Colors.green, _primaryColor, _selectColor),
@@ -181,6 +183,12 @@ class CardCollectionState extends State<CardCollection> {
});
}
void _toggleVaryFontSizes() {
setState(() {
_varyFontSizes = !_varyFontSizes;
});
}
void _selectColor(Map<int, Color> selection) {
setState(() {
_primaryColor = selection;
@@ -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(
onPressed: callback,
onPressed: enabled ? callback : null,
child: new Row(<Widget>[
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> {
new Radio<Map<int, Color>>(
value: itemValue,
groupValue: currentValue,
enabled: enabled,
onChanged: onChanged
onChanged: enabled ? onChanged : null
)
])
);
@@ -234,8 +244,7 @@ class CardCollectionState extends State<CardCollection> {
new Radio<DismissDirection>(
value: itemValue,
groupValue: currentValue,
enabled: enabled,
onChanged: onChanged
onChanged: enabled ? onChanged : null
)
])
);
@@ -250,8 +259,7 @@ class CardCollectionState extends State<CardCollection> {
new Radio<TextStyle>(
value: itemValue,
groupValue: currentValue,
enabled: enabled,
onChanged: onChanged
onChanged: enabled ? onChanged : null
)
])
);
@@ -292,7 +300,9 @@ class CardCollectionState extends State<CardCollection> {
)
)
: 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>[
new Text(cardModel.label)
],

View File

@@ -26,9 +26,7 @@ class ContainerApp extends StatelessComponent {
onPressed: () => print("Hello World")
),
new RaisedButton(
child: new Text('DISABLED'),
onPressed: () => print("Hello World"),
enabled: false
child: new Text('DISABLED')
)
])
),

View File

@@ -73,7 +73,6 @@ class SectorAppState extends State<SectorApp> {
padding: new EdgeDims.symmetric(horizontal: 8.0, vertical: 25.0),
child: new Row(<Widget>[
new RaisedButton(
enabled: _enabledAdd,
child: new IntrinsicWidth(
child: new Row(<Widget>[
new Container(
@@ -84,10 +83,9 @@ class SectorAppState extends State<SectorApp> {
new Text('ADD SECTOR'),
])
),
onPressed: addSector
onPressed: _enabledAdd ? addSector : null
),
new RaisedButton(
enabled: _enabledRemove,
child: new IntrinsicWidth(
child: new Row(<Widget>[
new Container(
@@ -98,7 +96,7 @@ class SectorAppState extends State<SectorApp> {
new Text('REMOVE SECTOR'),
])
),
onPressed: removeSector
onPressed: _enabledRemove ? removeSector : null
)
],
justifyContent: FlexJustifyContent.spaceAround