buildStar method

dynamic buildStar(
  1. BuildContext context,
  2. int index
)

buildStar is a method that returns stars. index is the index of the star. buildStar returns a GestureDetector that contains a Icon. If allowHalfRating is true, the star is filled if the rating is greater than or equal to the index of the star. If allowHalfRating is false, the star is filled if the rating is greater than the index of the star. If onRatingChanged is not null, the GestureDetector is clickable. If onRatingChanged is null, the GestureDetector is not clickable. If the user clicks on the star, the onRatingChanged method is called.

Implementation

buildStar(BuildContext context, int index) {
  Icon icon;

  /// [ratingStar] is the rating of the star.
  double ratingSize = size ?? MediaQuery.of(context).size.width / starCount;

  if (index >= rating) {
    icon = Icon(
      Icons.star_border,
      color: borderColor,
      size: ratingSize,
    );
  } else if (index > rating - 1 && index < rating) {
    icon = Icon(
      Icons.star_half,
      color: color,
      size: ratingSize,
    );
  } else {
    icon = Icon(
      Icons.star,
      color: color,
      size: size ?? ratingSize,
    );
  }

  return InkResponse(
    highlightColor: Colors.transparent,
    splashColor: Colors.transparent,
    radius: (size ?? ratingSize) / 2,
    // onTap: onRatingChanged == null ? null : () => onRatingChanged!(index + 1.0),
    onTapDown: onRatingChanged == null
        ? null
        : (tapDetails) {
            final tappedPosition = tapDetails.localPosition.dx;
            final tappedOnFirstHalf = tappedPosition <= ratingSize / 2;
            double value =
                index + (tappedOnFirstHalf && allowHalfRating ? 0.5 : 1.0);
            onRatingChanged!(value);
          },
    child: SizedBox(
      height: (size ?? ratingSize) * 1.5,
      child: icon,
    ),
  );
}