Skip to content

Commit b9d9b2a

Browse files
committed
basic rating bar completed
1 parent 0ace154 commit b9d9b2a

2 files changed

Lines changed: 95 additions & 108 deletions

File tree

example/lib/main.dart

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class _MyHomePageState extends State<MyHomePage>
6666
'Xamarin2',
6767
];
6868

69-
var rating = 0.0;
69+
var rating = 3.5;
7070

7171
@override
7272
Widget build(BuildContext context) => Scaffold(
@@ -173,10 +173,10 @@ class _MyHomePageState extends State<MyHomePage>
173173
// Container(color: Colors.blue)
174174
// ]),
175175

176-
SingleChildScrollView(
177-
child:
176+
// SingleChildScrollView(
177+
// child:
178178
Column(
179-
mainAxisAlignment: MainAxisAlignment.center,
179+
mainAxisAlignment: MainAxisAlignment.start,
180180
crossAxisAlignment: CrossAxisAlignment.center,
181181
children: <Widget>[
182182

@@ -214,49 +214,49 @@ class _MyHomePageState extends State<MyHomePage>
214214
}
215215
),
216216

217-
// SmoothStarRating(
218-
// rating: rating,
219-
//// size: 65,
220-
//// filledIconData: Icons.star,
221-
//// halfFilledIconData: Icons.star_half,
222-
//// defaultIconData: Icons.star_border,
223-
//// starCount: 5,
224-
//// allowHalfRating: false,
225-
//// spacing: 2.0,
226-
// onRatingChanged: (value) {
227-
// setState(() {
228-
// rating = value;
229-
// });
230-
// },
231-
// ),
217+
GFRating(
218+
rating: rating,
219+
// size: 65,
220+
filledIconData: Icons.star,
221+
halfFilledIconData: Icons.star_half,
222+
defaultIconData: Icons.star_border,
223+
itemCount: 5,
224+
allowHalfRating: false,
225+
// spacing: 2.0,
226+
onRatingChanged: (value) {
227+
setState(() {
228+
rating = value;
229+
});
230+
},
231+
),
232232

233-
// GFCard(
234-
// content: Column(
235-
// children: <Widget>[
236-
// GFTypography(
237-
// text: 'Toast',
238-
// type: GFTypographyType.typo6,
239-
// ),
240-
// SizedBox(
241-
// height: 20,
242-
// ),
243-
// SizedBox(
244-
// height: 20,
245-
// ),
246-
// GFToast(
247-
// text: 'Happy New Year',
248-
// button: GFButton(
249-
// onPressed: () {
250-
// print("dfr");
251-
// },
252-
// text: 'OK',
253-
// type: GFButtonType.outline,
254-
// color: GFColor.warning,
255-
// ),
256-
// ),
257-
// ],
258-
// ),
259-
// ),
233+
GFCard(
234+
content: Column(
235+
children: <Widget>[
236+
GFTypography(
237+
text: 'Toast',
238+
type: GFTypographyType.typo6,
239+
),
240+
SizedBox(
241+
height: 20,
242+
),
243+
SizedBox(
244+
height: 20,
245+
),
246+
GFToast(
247+
text: 'Happy New Year',
248+
button: GFButton(
249+
onPressed: () {
250+
print("dfr");
251+
},
252+
text: 'OK',
253+
type: GFButtonType.outline,
254+
color: GFColor.warning,
255+
),
256+
),
257+
],
258+
),
259+
),
260260
//
261261
// GFCard(
262262
// content: Column(
@@ -303,20 +303,20 @@ class _MyHomePageState extends State<MyHomePage>
303303
// ),
304304
// ),
305305

306-
Container(
307-
height: 130,
308-
width: 105,
309-
decoration: BoxDecoration(
310-
borderRadius: BorderRadius.circular(8),
311-
gradient: LinearGradient(
312-
begin: FractionalOffset.bottomLeft,
313-
end: FractionalOffset.topRight,
314-
colors: const [
315-
Color(0x5a0b486b),
316-
Color(0xFFF56217),
317-
]),
318-
),
319-
),
306+
// Container(
307+
// height: 130,
308+
// width: 105,
309+
// decoration: BoxDecoration(
310+
// borderRadius: BorderRadius.circular(8),
311+
// gradient: LinearGradient(
312+
// begin: FractionalOffset.bottomLeft,
313+
// end: FractionalOffset.topRight,
314+
// colors: const [
315+
// Color(0x5a0b486b),
316+
// Color(0xFFF56217),
317+
// ]),
318+
// ),
319+
// ),
320320

321321
// GFCard(
322322
// content: Column(
@@ -1168,7 +1168,7 @@ class _MyHomePageState extends State<MyHomePage>
11681168
// ),
11691169
],
11701170
),
1171-
),
1171+
// ),
11721172
// bottomNavigationBar: GFTabBar(
11731173
// initialIndex: 0,
11741174
// length: 3,

lib/components/rating/gf_rating.dart

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,75 @@ import 'package:flutter/material.dart';
22

33
typedef 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

Comments
 (0)