buildStar method

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

Implementation

Widget buildStar(BuildContext context, int index) {
  Icon icon;
  if (index >= rating) {
    icon = new Icon(
      defaultIconData != null ? defaultIconData : Icons.star_border,
      color: borderColor ?? Theme.of(context).primaryColor,
      size: size,
    );
  } else if (index > rating - (allowHalfRating ? 0.5 : 1.0) &&
      index < rating) {
    icon = new Icon(
      halfFilledIconData != null ? halfFilledIconData : Icons.star_half,
      color: color ?? Theme.of(context).primaryColor,
      size: size,
    );
  } else {
    icon = new Icon(
      filledIconData != null ? filledIconData : Icons.star,
      color: color ?? Theme.of(context).primaryColor,
      size: size,
    );
  }

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