import 'package:flutter/material.dart'; typedef void RatingChangeCallback(double rating); class SmoothStarRating extends StatelessWidget { final double paddingLeft; final double paddingRight; final int starCount; final double rating; final RatingChangeCallback onRatingChanged; final Color color; final Color borderColor; final double size; final bool allowHalfRating; Widget iconNormal; Widget iconSelected; SmoothStarRating( { this.starCount = 5, this.paddingLeft = 5.0, this.paddingRight = 5.0, this.rating = 0.0, this.onRatingChanged, this.color, this.borderColor, this.size, this.allowHalfRating = true, this.iconNormal, this.iconSelected}) { assert(this.rating != null); } Widget buildStar(BuildContext context, int index) { Widget iconWidget; if (index >= rating) { // icon = new Icon( // Icons.star_border, // color: borderColor ?? Theme.of(context).primaryColor, // size: size ?? 25.0, // ); iconWidget = iconNormal; } else if (index > rating - (allowHalfRating ? 0.5 : 1.0) && index < rating) { // icon = new Icon( // Icons.star_half, // color: color ?? Theme.of(context).primaryColor, // size: size ?? 25.0, // ); } else { // icon = new Icon( // Icons.star, // color: color ?? Theme.of(context).primaryColor, // size: size ?? 25.0, // ); iconWidget = iconSelected; } return new GestureDetector( onTap: () { if (this.onRatingChanged != null) onRatingChanged(index + 1.0); }, onHorizontalDragUpdate: (dragDetails) { RenderBox box = context.findRenderObject(); 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:Container( padding: EdgeInsets.only(left: paddingLeft,right: paddingRight), child: iconWidget, ), ); } @override Widget build(BuildContext context) { return new Material( color: Colors.transparent, child: new Wrap( alignment: WrapAlignment.start, children: new List.generate( starCount, (index) => buildStar(context, index))), ); } }