Unverified Commit ddfe8a35 authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

Update `chip.dart` to use set of `MaterialState` (#128507)

parent 29c858ad
...@@ -19,7 +19,6 @@ import 'ink_well.dart'; ...@@ -19,7 +19,6 @@ import 'ink_well.dart';
import 'material.dart'; import 'material.dart';
import 'material_localizations.dart'; import 'material_localizations.dart';
import 'material_state.dart'; import 'material_state.dart';
import 'material_state_mixin.dart';
import 'text_theme.dart'; import 'text_theme.dart';
import 'theme.dart'; import 'theme.dart';
import 'theme_data.dart'; import 'theme_data.dart';
...@@ -842,7 +841,7 @@ class RawChip extends StatefulWidget ...@@ -842,7 +841,7 @@ class RawChip extends StatefulWidget
State<RawChip> createState() => _RawChipState(); State<RawChip> createState() => _RawChipState();
} }
class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProviderStateMixin<RawChip> { class _RawChipState extends State<RawChip> with TickerProviderStateMixin<RawChip> {
static const Duration pressedAnimationDuration = Duration(milliseconds: 75); static const Duration pressedAnimationDuration = Duration(milliseconds: 75);
late AnimationController selectController; late AnimationController selectController;
...@@ -854,6 +853,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -854,6 +853,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
late Animation<double> deleteDrawerAnimation; late Animation<double> deleteDrawerAnimation;
late Animation<double> enableAnimation; late Animation<double> enableAnimation;
late Animation<double> selectionFade; late Animation<double> selectionFade;
late final Set<MaterialState> states;
bool get hasDeleteButton => widget.onDeleted != null; bool get hasDeleteButton => widget.onDeleted != null;
bool get hasAvatar => widget.avatar != null; bool get hasAvatar => widget.avatar != null;
...@@ -871,8 +871,10 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -871,8 +871,10 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
void initState() { void initState() {
assert(widget.onSelected == null || widget.onPressed == null); assert(widget.onSelected == null || widget.onPressed == null);
super.initState(); super.initState();
setMaterialState(MaterialState.disabled, !widget.isEnabled); states = <MaterialState>{
setMaterialState(MaterialState.selected, widget.selected); if (!widget.isEnabled) MaterialState.disabled,
if (widget.selected) MaterialState.selected,
};
selectController = AnimationController( selectController = AnimationController(
duration: _kSelectDuration, duration: _kSelectDuration,
value: widget.selected ? 1.0 : 0.0, value: widget.selected ? 1.0 : 0.0,
...@@ -947,7 +949,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -947,7 +949,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
if (!canTap) { if (!canTap) {
return; return;
} }
setMaterialState(MaterialState.pressed, true); states.add(MaterialState.pressed);
setState(() { setState(() {
_isTapping = true; _isTapping = true;
}); });
...@@ -957,7 +959,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -957,7 +959,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
if (!canTap) { if (!canTap) {
return; return;
} }
setMaterialState(MaterialState.pressed, false); states.remove(MaterialState.pressed);
setState(() { setState(() {
_isTapping = false; _isTapping = false;
}); });
...@@ -967,7 +969,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -967,7 +969,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
if (!canTap) { if (!canTap) {
return; return;
} }
setMaterialState(MaterialState.pressed, false); states.remove(MaterialState.pressed);
setState(() { setState(() {
_isTapping = false; _isTapping = false;
}); });
...@@ -976,13 +978,33 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -976,13 +978,33 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
widget.onPressed?.call(); widget.onPressed?.call();
} }
void _updateFocusState(bool focused) {
setState(() {
if (focused) {
states.add(MaterialState.focused);
} else {
states.remove(MaterialState.focused);
}
});
}
void _updateHoverState(bool hovered) {
setState(() {
if (hovered) {
states.add(MaterialState.hovered);
} else {
states.remove(MaterialState.hovered);
}
});
}
OutlinedBorder _getShape(ThemeData theme, ChipThemeData chipTheme, ChipThemeData chipDefaults) { OutlinedBorder _getShape(ThemeData theme, ChipThemeData chipTheme, ChipThemeData chipDefaults) {
final BorderSide? resolvedSide = MaterialStateProperty.resolveAs<BorderSide?>(widget.side, materialStates) final BorderSide? resolvedSide = MaterialStateProperty.resolveAs<BorderSide?>(widget.side, states)
?? MaterialStateProperty.resolveAs<BorderSide?>(chipTheme.side, materialStates) ?? MaterialStateProperty.resolveAs<BorderSide?>(chipTheme.side, states)
?? MaterialStateProperty.resolveAs<BorderSide?>(chipDefaults.side, materialStates); ?? MaterialStateProperty.resolveAs<BorderSide?>(chipDefaults.side, states);
final OutlinedBorder resolvedShape = MaterialStateProperty.resolveAs<OutlinedBorder?>(widget.shape, materialStates) final OutlinedBorder resolvedShape = MaterialStateProperty.resolveAs<OutlinedBorder?>(widget.shape, states)
?? MaterialStateProperty.resolveAs<OutlinedBorder?>(chipTheme.shape, materialStates) ?? MaterialStateProperty.resolveAs<OutlinedBorder?>(chipTheme.shape, states)
?? MaterialStateProperty.resolveAs<OutlinedBorder?>(chipDefaults.shape, materialStates) ?? MaterialStateProperty.resolveAs<OutlinedBorder?>(chipDefaults.shape, states)
?? const StadiumBorder(); ?? const StadiumBorder();
return resolvedShape.copyWith(side: resolvedSide); return resolvedShape.copyWith(side: resolvedSide);
} }
...@@ -1032,7 +1054,9 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -1032,7 +1054,9 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
if (oldWidget.isEnabled != widget.isEnabled) { if (oldWidget.isEnabled != widget.isEnabled) {
setState(() { setState(() {
setMaterialState(MaterialState.disabled, !widget.isEnabled); if (!widget.isEnabled) {
states.add(MaterialState.disabled);
}
if (widget.isEnabled) { if (widget.isEnabled) {
enableController.forward(); enableController.forward();
} else { } else {
...@@ -1051,7 +1075,9 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -1051,7 +1075,9 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
} }
if (oldWidget.selected != widget.selected) { if (oldWidget.selected != widget.selected) {
setState(() { setState(() {
setMaterialState(MaterialState.selected, widget.selected); if (widget.selected) {
states.add(MaterialState.selected);
}
if (widget.selected) { if (widget.selected) {
selectController.forward(); selectController.forward();
} else { } else {
...@@ -1187,7 +1213,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -1187,7 +1213,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
?? chipDefaults.iconTheme; ?? chipDefaults.iconTheme;
final TextStyle effectiveLabelStyle = labelStyle.merge(widget.labelStyle); final TextStyle effectiveLabelStyle = labelStyle.merge(widget.labelStyle);
final Color? resolvedLabelColor = MaterialStateProperty.resolveAs<Color?>(effectiveLabelStyle.color, materialStates); final Color? resolvedLabelColor = MaterialStateProperty.resolveAs<Color?>(effectiveLabelStyle.color, states);
final TextStyle resolvedLabelStyle = effectiveLabelStyle.copyWith(color: resolvedLabelColor); final TextStyle resolvedLabelStyle = effectiveLabelStyle.copyWith(color: resolvedLabelColor);
final Widget? avatar = iconTheme != null && hasAvatar final Widget? avatar = iconTheme != null && hasAvatar
? IconTheme(data: iconTheme, child: widget.avatar!) ? IconTheme(data: iconTheme, child: widget.avatar!)
...@@ -1201,14 +1227,14 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid ...@@ -1201,14 +1227,14 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
shape: resolvedShape, shape: resolvedShape,
clipBehavior: widget.clipBehavior, clipBehavior: widget.clipBehavior,
child: InkWell( child: InkWell(
onFocusChange: updateMaterialState(MaterialState.focused), onFocusChange: _updateFocusState,
focusNode: widget.focusNode, focusNode: widget.focusNode,
autofocus: widget.autofocus, autofocus: widget.autofocus,
canRequestFocus: widget.isEnabled, canRequestFocus: widget.isEnabled,
onTap: canTap ? _handleTap : null, onTap: canTap ? _handleTap : null,
onTapDown: canTap ? _handleTapDown : null, onTapDown: canTap ? _handleTapDown : null,
onTapCancel: canTap ? _handleTapCancel : null, onTapCancel: canTap ? _handleTapCancel : null,
onHover: canTap ? updateMaterialState(MaterialState.hovered) : null, onHover: canTap ? _updateHoverState : null,
customBorder: resolvedShape, customBorder: resolvedShape,
child: AnimatedBuilder( child: AnimatedBuilder(
animation: Listenable.merge(<Listenable>[selectController, enableController]), animation: Listenable.merge(<Listenable>[selectController, enableController]),
......
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