// A stateful component is required to manage the selected rating.
class MultiColorRatingExample extends StatefulComponent {
@override
State<MultiColorRatingExample> createState() => _MultiColorRatingExampleState();
}
class _MultiColorRatingExampleState extends State<MultiColorRatingExample> {
num _ratingValue = 2;
void _handleSelect(num newValue) {
setState(() => _ratingValue = newValue);
}
@override
Component build(BuildContext context) {
// Use RatingContainer as the parent for a custom group.
return RatingContainer(
name: 'rating-3',
classes: 'gap-1', // Adds spacing between items
[
// Each RatingItem can have its own independent style.
RatingItem(
value: 1,
isChecked: _ratingValue == 1,
onSelect: _handleSelect,
style: [Mask.heart, const BgUtil('bg-red-400')],
),
RatingItem(
value: 2,
isChecked: _ratingValue == 2,
onSelect: _handleSelect,
style: [Mask.heart, const BgUtil('bg-orange-400')],
),
RatingItem(
value: 3,
isChecked: _ratingValue == 3,
onSelect: _handleSelect,
style: [Mask.heart, const BgUtil('bg-yellow-400')],
),
RatingItem(
value: 4,
isChecked: _ratingValue == 4,
onSelect: _handleSelect,
style: [Mask.heart, const BgUtil('bg-lime-400')],
),
RatingItem(
value: 5,
isChecked: _ratingValue == 5,
onSelect: _handleSelect,
style: [Mask.heart, const BgUtil('bg-green-400')],
),
],
);
}
}