En répondant à cette question: How to create rating star bar properly?
En même temps, je a donné un exemple d'un widget d'évaluation des étoiles qui peut fonctionner avec n'importe quel nombre d'étoiles (5 par défaut).
typedef void RatingChangeCallback(double rating);
class StarRating extends StatelessWidget {
final int starCount;
final double rating;
final RatingChangeCallback onRatingChanged;
final Color color;
StarRating({this.starCount = 5, this.rating = .0, this.onRatingChanged, this.color});
Widget buildStar(BuildContext context, int index) {
Icon icon;
if (index >= rating) {
icon = new Icon(
Icons.star_border,
color: Theme.of(context).buttonColor,
);
}
else if (index > rating - 1 && index < rating) {
icon = new Icon(
Icons.star_half,
color: color ?? Theme.of(context).primaryColor,
);
} else {
icon = new Icon(
Icons.star,
color: color ?? Theme.of(context).primaryColor,
);
}
return new InkResponse(
onTap: onRatingChanged == null ? null :() => onRatingChanged(index + 1.0),
child: icon,
);
}
@override
Widget build(BuildContext context) {
return new Row(children: new List.generate(starCount, (index) => buildStar(context, index)));
}
}
Vous pouvez alors l'utiliser à l'aide
class Test extends StatefulWidget {
@override
_TestState createState() => new _TestState();
}
class _TestState extends State<Test> {
double rating = 3.5;
@override
Widget build(BuildContext context) {
return new StarRating(
rating: rating,
onRatingChanged: (rating) => setState(() => this.rating = rating),
starCount: 2
);
}
}
Je ne sais pas comment cela pertinent à votre question, mais j'ai posé une question similaire ici, peut-être vous aider à: https: // stackoverflow.com/questions/46637566/how-to-create-rating-star-bar-properly/46645766#46645766 – aziza
Va le chercher ..... – SirPaulMuaddib