Skip to content

Commit

Permalink
test: Added debugfillproperties test to chip_test
Browse files Browse the repository at this point in the history
feat: Added disabled variant to chip

feat: Added onTaps to chips in example app
  • Loading branch information
DE7924 committed Nov 7, 2024
1 parent 51b4172 commit 4aea008
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 34 deletions.
3 changes: 3 additions & 0 deletions example/lib/pages/components/chip_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class _ChipExampleState extends State<ChipExample> {
label: 'Label',
leading: ZetaIcon(ZetaIcons.user),
trailing: IconButton(icon: Icon(ZetaIcons.close), onPressed: () {}),
onTap: () {},
),
]);

Expand All @@ -42,6 +43,7 @@ class _ChipExampleState extends State<ChipExample> {
leading: ZetaIcon(ZetaIcons.star),
draggable: true,
data: 'Assist chip',
onTap: () {},
),
),
]);
Expand All @@ -60,6 +62,7 @@ class _ChipExampleState extends State<ChipExample> {
selected: true,
data: 'Filter chip',
draggable: true,
onTap: (bool selected) {},
),
),
]);
Expand Down
92 changes: 65 additions & 27 deletions lib/src/components/chips/chip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ class ZetaChip extends ZetaStatefulWidget {

class _ZetaChipState extends State<ZetaChip> {
bool selected = false;
bool _draggable = false;

@override
void initState() {
super.initState();
selected = widget.selected ?? false;
_draggable = widget.draggable;
WidgetsBinding.instance.addPostFrameCallback((_) {
_updateControllerState();
});
}

@override
Expand All @@ -101,10 +106,27 @@ class _ZetaChipState extends State<ZetaChip> {
if (oldWidget.selected != widget.selected) {
selected = widget.selected ?? false;
}
WidgetsBinding.instance.addPostFrameCallback((_) {
_updateControllerState();
});
}

void _updateControllerState() {
if (widget.onTap == null && widget.onToggle == null) {
_controller.update(WidgetState.disabled, true);
setState(() {
_draggable = false;
});
} else {
_controller.update(WidgetState.disabled, false);
setState(() {
_draggable = widget.draggable;
});
}
}

Widget _renderLeading(Color foregroundColor) {
if (widget.leading.runtimeType == Icon) {
if (widget.leading.runtimeType == ZetaIcon || widget.leading.runtimeType == Icon) {
return IconTheme(
data: IconThemeData(color: foregroundColor, size: Zeta.of(context).spacing.xl),
child: widget.leading!,
Expand All @@ -120,7 +142,6 @@ class _ZetaChipState extends State<ZetaChip> {
@override
Widget build(BuildContext context) {
final colors = Zeta.of(context).colors;
final foregroundColor = selected ? colors.textInverse : colors.textDefault;

return ZetaRoundedScope(
rounded: context.rounded,
Expand All @@ -129,18 +150,18 @@ class _ZetaChipState extends State<ZetaChip> {
button: widget.onTap != null,
label: widget.semanticLabel,
child: SelectionContainer.disabled(
child: widget.draggable
child: _draggable
? Draggable(
feedback: Material(
color: Colors.transparent,
child: child(colors, foregroundColor, isDragging: true),
child: child(colors, isDragging: true),
),
childWhenDragging: const Nothing(),
data: widget.data,
onDragCompleted: widget.onDragCompleted,
child: child(colors, foregroundColor),
child: child(colors),
)
: child(colors, foregroundColor),
: child(colors),
),
),
);
Expand Down Expand Up @@ -171,25 +192,41 @@ class _ZetaChipState extends State<ZetaChip> {
return Zeta.of(context).spacing.large;
}

Color _foregroundColor(ZetaColors colors, bool disabled) {
if (!disabled) {
if (selected) {
return colors.textInverse;
} else {
return colors.textDefault;
}
} else {
return colors.textDisabled;
}
}

ValueListenableBuilder<Set<WidgetState>> child(
ZetaColors colors,
Color foregroundColor, {
ZetaColors colors, {
bool isDragging = false,
}) {
return ValueListenableBuilder(
valueListenable: _controller,
builder: (context, states, child) {
final disabled = states.contains(WidgetState.disabled);
final Color foregroundColor = _foregroundColor(colors, disabled);
final double iconSize = selected ? Zeta.of(context).spacing.xl_2 : Zeta.of(context).spacing.none;
final bool rounded = context.rounded;
return InkWell(
statesController: _controller,
statesController: !disabled ? _controller : null,
mouseCursor: !disabled ? SystemMouseCursors.click : SystemMouseCursors.basic,
borderRadius: rounded ? Zeta.of(context).radius.full : Zeta.of(context).radius.none,
onTap: () {
if (widget.selected != null) {
setState(() => selected = !selected);
widget.onToggle?.call(selected);
} else {
widget.onTap?.call();
if (!disabled) {
if (widget.selected != null) {
setState(() => selected = !selected);
widget.onToggle?.call(selected);
} else {
widget.onTap?.call();
}
}
},
child: AnimatedContainer(
Expand All @@ -203,22 +240,23 @@ class _ZetaChipState extends State<ZetaChip> {
),
decoration: BoxDecoration(
color: () {
if (states.contains(WidgetState.disabled)) {
if (disabled) {
return colors.surfaceDisabled;
}
if (selected) {
} else {
if (selected) {
if (states.contains(WidgetState.hovered)) {
return colors.borderHover;
}
return colors.surfaceDefaultInverse;
}
if (states.contains(WidgetState.pressed) || isDragging) {
return colors.surfaceSelected;
}
if (states.contains(WidgetState.hovered)) {
return colors.borderHover;
return colors.surfaceHover;
}
return colors.surfaceDefaultInverse;
}
if (states.contains(WidgetState.pressed) || isDragging) {
return colors.surfaceSelected;
}
if (states.contains(WidgetState.hovered)) {
return colors.surfaceHover;
return colors.surfacePrimary;
}
return colors.surfacePrimary;
}(),
borderRadius: rounded ? Zeta.of(context).radius.full : Zeta.of(context).radius.none,
border: Border.fromBorderSide(
Expand All @@ -242,7 +280,7 @@ class _ZetaChipState extends State<ZetaChip> {
child: (selected
? ZetaIcon(
ZetaIcons.check_mark,
color: widget.selected! ? colors.iconInverse : Colors.transparent,
color: states.contains(WidgetState.disabled) ? colors.iconDisabled : colors.iconInverse,
)
: const Nothing()),
)
Expand Down
22 changes: 15 additions & 7 deletions test/src/components/chips/chip_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ void main() {

group('Accessibility Tests', () {});
group('Content Tests', () {
// final debugFillProperties = {
// '': '',
// };
// debugFillPropertiesTest(
// widget,
// debugFillProperties,
// );
final debugFillProperties = {
'label': '"Test Chip"',
'rounded': 'null',
'selected': 'null',
'draggable': 'false',
'data': 'null',
'onDragCompleted': 'null',
'semanticLabel': 'null',
'onTap': 'null',
'onToggle': 'null',
};
debugFillPropertiesTest(
const ZetaChip(label: 'Test Chip'),
debugFillProperties,
);

testWidgets('renders label correctly', (WidgetTester tester) async {
await tester.pumpWidget(
Expand Down

0 comments on commit 4aea008

Please sign in to comment.