Unverified Commit 17ddfb1b authored by MH Johnson's avatar MH Johnson Committed by GitHub

make RawChip.selected non-nullable (#37556)

parent aa83b284
...@@ -1285,8 +1285,8 @@ class RawChip extends StatefulWidget ...@@ -1285,8 +1285,8 @@ class RawChip extends StatefulWidget
/// The [onPressed] and [onSelected] callbacks must not both be specified at /// The [onPressed] and [onSelected] callbacks must not both be specified at
/// the same time. /// the same time.
/// ///
/// The [label], [isEnabled], and [clipBehavior] arguments must not be null. /// The [label], [isEnabled], [selected], and [clipBehavior] arguments must
/// The [pressElevation] and [elevation] must be null or non-negative. /// not be null. The [pressElevation] and [elevation] must be null or non-negative.
/// Typically, [pressElevation] is greater than [elevation]. /// Typically, [pressElevation] is greater than [elevation].
const RawChip({ const RawChip({
Key key, Key key,
...@@ -1303,7 +1303,7 @@ class RawChip extends StatefulWidget ...@@ -1303,7 +1303,7 @@ class RawChip extends StatefulWidget
this.onSelected, this.onSelected,
this.pressElevation, this.pressElevation,
this.tapEnabled = true, this.tapEnabled = true,
this.selected, this.selected = false,
this.showCheckmark = true, this.showCheckmark = true,
this.isEnabled = true, this.isEnabled = true,
this.disabledColor, this.disabledColor,
...@@ -1319,6 +1319,7 @@ class RawChip extends StatefulWidget ...@@ -1319,6 +1319,7 @@ class RawChip extends StatefulWidget
this.avatarBorder = const CircleBorder(), this.avatarBorder = const CircleBorder(),
}) : assert(label != null), }) : assert(label != null),
assert(isEnabled != null), assert(isEnabled != null),
assert(selected != null),
assert(clipBehavior != null), assert(clipBehavior != null),
assert(pressElevation == null || pressElevation >= 0.0), assert(pressElevation == null || pressElevation >= 0.0),
assert(elevation == null || elevation >= 0.0), assert(elevation == null || elevation >= 0.0),
...@@ -1430,7 +1431,7 @@ class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip ...@@ -1430,7 +1431,7 @@ class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip
assert(widget.onSelected == null || widget.onPressed == null); assert(widget.onSelected == null || widget.onPressed == null);
super.initState(); super.initState();
_updateState(MaterialState.disabled, !widget.isEnabled); _updateState(MaterialState.disabled, !widget.isEnabled);
_updateState(MaterialState.selected, widget.selected ?? false); _updateState(MaterialState.selected, widget.selected);
selectController = AnimationController( selectController = AnimationController(
duration: _kSelectDuration, duration: _kSelectDuration,
value: widget.selected == true ? 1.0 : 0.0, value: widget.selected == true ? 1.0 : 0.0,
...@@ -1588,7 +1589,7 @@ class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip ...@@ -1588,7 +1589,7 @@ class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip
} }
if (oldWidget.selected != widget.selected) { if (oldWidget.selected != widget.selected) {
setState(() { setState(() {
_updateState(MaterialState.selected, widget.selected ?? false); _updateState(MaterialState.selected, widget.selected);
if (widget.selected == true) { if (widget.selected == true) {
selectController.forward(); selectController.forward();
} else { } else {
...@@ -1655,7 +1656,6 @@ class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip ...@@ -1655,7 +1656,6 @@ class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip
final double pressElevation = widget.pressElevation ?? chipTheme.pressElevation ?? _defaultPressElevation; final double pressElevation = widget.pressElevation ?? chipTheme.pressElevation ?? _defaultPressElevation;
final Color shadowColor = widget.shadowColor ?? chipTheme.shadowColor ?? _defaultShadowColor; final Color shadowColor = widget.shadowColor ?? chipTheme.shadowColor ?? _defaultShadowColor;
final Color selectedShadowColor = widget.selectedShadowColor ?? chipTheme.selectedShadowColor ?? _defaultShadowColor; final Color selectedShadowColor = widget.selectedShadowColor ?? chipTheme.selectedShadowColor ?? _defaultShadowColor;
final bool selected = widget.selected ?? false;
final TextStyle effectiveLabelStyle = widget.labelStyle ?? chipTheme.labelStyle; final TextStyle effectiveLabelStyle = widget.labelStyle ?? chipTheme.labelStyle;
final Color resolvedLabelColor = MaterialStateProperty.resolveAs<Color>(effectiveLabelStyle?.color, _states); final Color resolvedLabelColor = MaterialStateProperty.resolveAs<Color>(effectiveLabelStyle?.color, _states);
...@@ -1665,7 +1665,7 @@ class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip ...@@ -1665,7 +1665,7 @@ class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip
onFocusChange: _handleFocus, onFocusChange: _handleFocus,
child: Material( child: Material(
elevation: isTapping ? pressElevation : elevation, elevation: isTapping ? pressElevation : elevation,
shadowColor: selected ? selectedShadowColor : shadowColor, shadowColor: widget.selected ? selectedShadowColor : shadowColor,
animationDuration: pressedAnimationDuration, animationDuration: pressedAnimationDuration,
shape: shape, shape: shape,
clipBehavior: widget.clipBehavior, clipBehavior: widget.clipBehavior,
......
...@@ -1168,7 +1168,7 @@ void main() { ...@@ -1168,7 +1168,7 @@ void main() {
deleteIcon: deleteIcon, deleteIcon: deleteIcon,
isEnabled: isSelectable || isPressable, isEnabled: isSelectable || isPressable,
shape: chipTheme.shape, shape: chipTheme.shape,
selected: isSelectable ? value : null, selected: isSelectable && value,
label: Text('$value'), label: Text('$value'),
onSelected: isSelectable onSelected: isSelectable
? (bool newValue) { ? (bool newValue) {
...@@ -1738,6 +1738,20 @@ void main() { ...@@ -1738,6 +1738,20 @@ void main() {
expect(find.byType(InkWell), findsOneWidget); expect(find.byType(InkWell), findsOneWidget);
}); });
testWidgets('RawChip.selected can not be null', (WidgetTester tester) async {
expect(() async {
MaterialApp(
home: Material(
child: RawChip(
onPressed: () { },
selected: null,
label: const Text('Chip'),
),
),
);
}, throwsAssertionError);
});
testWidgets('Chip uses stateful color for text color in different states', (WidgetTester tester) async { testWidgets('Chip uses stateful color for text color in different states', (WidgetTester tester) async {
final FocusNode focusNode = FocusNode(); final FocusNode focusNode = FocusNode();
......
...@@ -71,7 +71,6 @@ void main() { ...@@ -71,7 +71,6 @@ void main() {
backgroundColor: Colors.blue, backgroundColor: Colors.blue,
); );
final ChipThemeData chipTheme = theme.chipTheme; final ChipThemeData chipTheme = theme.chipTheme;
bool value;
Widget buildChip(ChipThemeData data) { Widget buildChip(ChipThemeData data) {
return MaterialApp( return MaterialApp(
...@@ -91,8 +90,8 @@ void main() { ...@@ -91,8 +90,8 @@ void main() {
avatar: const Placeholder(), avatar: const Placeholder(),
deleteIcon: const Placeholder(), deleteIcon: const Placeholder(),
isEnabled: true, isEnabled: true,
selected: value, selected: false,
label: Text('$value'), label: const Text('Chip'),
onSelected: (bool newValue) { }, onSelected: (bool newValue) { },
onPressed: null, onPressed: null,
), ),
......
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