buildStar method

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

Implementation

Widget buildStar(BuildContext context, int index) {
  Icon icon;
  if (index >= rating) {
    icon = Icon(
      Icons.star_border,
      color: borderColor ?? Theme.of(context).primaryColor,
      size: size ?? 25.0,
    );
  } else if (rating.toInt() >= index + 1) {
    icon = Icon(
      Icons.star,
      color: color ?? Theme.of(context).primaryColor,
      size: size ?? 25.0,
    );
  } else {
    icon = Icon(
      Icons.star_half,
      color: color ?? Theme.of(context).primaryColor,
      size: size ?? 25.0,
    );
  }

  return GestureDetector(
    onTap: () {
      if (this.onChanged != null) onChanged(index + 1.0);
    },
    onHorizontalDragUpdate: (dragDetails) {
      RenderBox box = context.findRenderObject();
      var _position = box.globalToLocal(dragDetails.globalPosition);
      var i = _position.dx / size;
      var newRating = i.round().toDouble();
      if (useHalfRating) {
        newRating = _customRound(i);
      }
      if (newRating > starCount) {
        newRating = starCount.toDouble();
      }
      if (newRating < 0) {
        newRating = 0.0;
      }
      if (this.onChanged != null) onChanged(newRating);
    },
    child: icon,
  );
}