Use @required for onPressed and onChanged (#4534)

We now use the `@required` annotation to encourage developers to
explicitly set onPressed and onChanged callbacks to null when that would
disable the widget.

Fixes #287
This commit is contained in:
Adam Barth
2016-06-12 13:25:06 -07:00
committed by GitHub
parent cc44696754
commit 1a3adae101
23 changed files with 133 additions and 101 deletions

View File

@@ -106,7 +106,8 @@ class _ButtonsDemoState extends State<ButtonsDemo> {
}
),
new RaisedButton(
child: new Text('DISABLED')
child: new Text('DISABLED'),
onPressed: null
)
]
)
@@ -126,7 +127,8 @@ class _ButtonsDemoState extends State<ButtonsDemo> {
}
),
new FlatButton(
child: new Text('DISABLED')
child: new Text('DISABLED'),
onPressed: null
)
]
)
@@ -173,7 +175,8 @@ class _ButtonsDemoState extends State<ButtonsDemo> {
color: iconButtonToggle ? Theme.of(context).primaryColor : null
),
new IconButton(
icon: Icons.thumb_up
icon: Icons.thumb_up,
onPressed: null
)
]
.map((Widget button) => new SizedBox(width: 64.0, height: 64.0, child: button))

View File

@@ -59,8 +59,9 @@ class _PersistentBottomSheetDemoState extends State<PersistentBottomSheetDemo> {
key: _scaffoldKey,
appBar: new AppBar(title: new Text('Persistent bottom sheet')),
floatingActionButton: new FloatingActionButton(
child: new Icon(icon: Icons.add),
backgroundColor: Colors.redAccent[200]
onPressed: null,
backgroundColor: Colors.redAccent[200],
child: new Icon(icon: Icons.add)
),
body: new Center(
child: new RaisedButton(

View File

@@ -101,8 +101,8 @@ class _SelectionControlsDemoState extends State<SelectionControlsDemo> {
mainAxisAlignment: MainAxisAlignment.collapse,
children: <Widget>[
// Disabled checkboxes
new Checkbox(value: true),
new Checkbox(value: false)
new Checkbox(value: true, onChanged: null),
new Checkbox(value: false, onChanged: null)
]
)
]
@@ -142,15 +142,18 @@ class _SelectionControlsDemoState extends State<SelectionControlsDemo> {
children: <Widget>[
new Radio<int>(
value: 0,
groupValue: 0
groupValue: 0,
onChanged: null
),
new Radio<int>(
value: 1,
groupValue: 0
groupValue: 0,
onChanged: null
),
new Radio<int>(
value: 2,
groupValue: 0
groupValue: 0,
onChanged: null
)
]
)
@@ -171,8 +174,8 @@ class _SelectionControlsDemoState extends State<SelectionControlsDemo> {
});
}),
// Disabled switches
new Switch(value: true),
new Switch(value: false)
new Switch(value: true, onChanged: null),
new Switch(value: false, onChanged: null)
]
)
);

View File

@@ -34,7 +34,7 @@ class _SliderDemoState extends State<SliderDemo> {
}
)
),
new Center(child: new Slider(value: 0.25)),
new Center(child: new Slider(value: 0.25, onChanged: null)),
new Center(
child: new Slider(
value: _discreteValue,

View File

@@ -26,7 +26,8 @@ new RaisedButton(
// Buttons are disabled when onPressed isn't
// specified or is null.
new RaisedButton(
child: new Text('BUTTON TITLE')
child: new Text('BUTTON TITLE'),
onPressed: null
);
// END
@@ -44,7 +45,8 @@ new FlatButton(
// Buttons are disabled when onPressed isn't
// specified or is null.
new FlatButton(
child: new Text('BUTTON TITLE')
child: new Text('BUTTON TITLE'),
onPressed: null
);
// END
@@ -97,7 +99,8 @@ new Scaffold(
title: new Text('Demo')
),
floatingActionButton: new FloatingActionButton(
child: new Icon(icon: Icons.add)
child: new Icon(icon: Icons.add),
onPressed: null
)
);
// END
@@ -127,7 +130,7 @@ new Checkbox(
// Create a disabled checkbox.
// Checkboxes are disabled when onChanged isn't
// specified or null.
new Checkbox(value: false);
new Checkbox(value: false, onChanged: null);
// END
@@ -166,7 +169,8 @@ new Row(
// Creates a disabled radio button.
new Radio<int>(
value: 0,
groupValue: 0
groupValue: 0,
onChanged: null
);
// END
@@ -188,7 +192,7 @@ new Switch(
// Create a disabled switch.
// Switches are disabled when onChanged isn't
// specified or null.
new Switch(value: false);
new Switch(value: false, onChanged: null);
// END
}
}

View File

@@ -52,7 +52,8 @@ class AdaptedGridItem extends StatelessWidget {
child: new Text(name)
),
new IconButton(
icon: Icons.more_vert
icon: Icons.more_vert,
onPressed: null
)
]
)