@@ -2,88 +2,75 @@ import 'package:flutter/material.dart';
22
33typedef void RatingChangeCallback (double rating);
44
5- class SmoothStarRating extends StatelessWidget {
6- final int starCount;
7- final double rating;
8- final RatingChangeCallback onRatingChanged;
9- final Color color;
10- final Color borderColor;
11- final double size;
12- final bool allowHalfRating;
13- final IconData filledIconData;
14- final IconData halfFilledIconData;
15- final IconData
16- defaultIconData; //this is needed only when having fullRatedIconData && halfRatedIconData
17- final double spacing;
18- SmoothStarRating ({
19- this .starCount = 5 ,
5+ class GFRating extends StatelessWidget {
6+
7+ GFRating ({
8+ this .itemCount = 5 ,
209 this .spacing= 0.0 ,
2110 this .rating = 0.0 ,
22- this .defaultIconData ,
11+ this .defaultIcon ,
2312 this .onRatingChanged,
2413 this .color,
2514 this .borderColor,
2615 this .size = 25 ,
27- this .filledIconData ,
28- this .halfFilledIconData ,
16+ this .filledIcon ,
17+ this .halfFilledIcon ,
2918 this .allowHalfRating = true ,
3019 }) {
3120 assert (this .rating != null );
3221 }
3322
34- Widget buildStar (BuildContext context, int index) {
23+ final int itemCount;
24+ final double rating;
25+ final RatingChangeCallback onRatingChanged;
26+ final Color color;
27+ final Color borderColor;
28+ final double size;
29+ final bool allowHalfRating;
30+ final IconData filledIcon;
31+ final IconData halfFilledIcon;
32+ final IconData defaultIcon; //this is needed only when having fullRatedIconData && halfRatedIconData
33+ final double spacing;
34+
35+
36+ Widget buildRatingBar (BuildContext context, int index) {
3537 Icon icon;
3638 if (index >= rating) {
37- icon = new Icon (
38- defaultIconData != null ? defaultIconData : Icons .star_border,
39+ icon = Icon (
40+ defaultIcon != null ? defaultIcon : Icons .star_border,
3941 color: borderColor ?? Theme .of (context).primaryColor,
4042 size: size,
4143 );
4244 } else if (index > rating - (allowHalfRating ? 0.5 : 1.0 ) &&
4345 index < rating) {
44- icon = new Icon (
45- halfFilledIconData != null ? halfFilledIconData : Icons .star_half,
46+ icon = Icon (
47+ halfFilledIcon != null ? halfFilledIcon : Icons .star_half,
4648 color: color ?? Theme .of (context).primaryColor,
4749 size: size,
4850 );
4951 } else {
50- icon = new Icon (
51- filledIconData != null ? filledIconData : Icons .star,
52+ icon = Icon (
53+ filledIcon != null ? filledIcon : Icons .star,
5254 color: color ?? Theme .of (context).primaryColor,
5355 size: size,
5456 );
5557 }
5658
57- return new GestureDetector (
59+ return GestureDetector (
5860 onTap: () {
59- if (this .onRatingChanged != null ) onRatingChanged (index + 1.0 );
60- },
61- onHorizontalDragUpdate: (dragDetails) {
62- RenderBox box = context.findRenderObject ();
63- var _pos = box.globalToLocal (dragDetails.globalPosition);
64- var i = _pos.dx / size;
65- var newRating = allowHalfRating ? i : i.round ().toDouble ();
66- if (newRating > starCount) {
67- newRating = starCount.toDouble ();
68- }
69- if (newRating < 0 ) {
70- newRating = 0.0 ;
71- }
72- if (this .onRatingChanged != null ) onRatingChanged (newRating);
61+ if (onRatingChanged != null ) onRatingChanged (index + 1.0 );
7362 },
7463 child: icon,
7564 );
7665 }
7766
7867 @override
79- Widget build (BuildContext context) {
80- return new Material (
68+ Widget build (BuildContext context) => Material (
8169 color: Colors .transparent,
82- child: new Wrap (
83- alignment: WrapAlignment .start ,
70+ child: Wrap (
71+ alignment: WrapAlignment .center ,
8472 spacing: spacing,
85- children: new List .generate (
86- starCount , (index) => buildStar (context, index))),
73+ children: List .generate (
74+ itemCount , (index) => buildRatingBar (context, index))),
8775 );
88- }
8976}
0 commit comments