Skip to content

Commit 45a11f1

Browse files
committed
rating declaration changed
1 parent 011fb04 commit 45a11f1

2 files changed

Lines changed: 60 additions & 65 deletions

File tree

example/lib/main.dart

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class _MyHomePageState extends State<MyHomePage>
4141

4242
TabController tabController;
4343
final _ratingController = TextEditingController();
44-
double _rating = 3.5;
44+
double _rating = 3;
4545
double _userRating = 4.5;
4646

4747
@override
@@ -216,8 +216,8 @@ class _MyHomePageState extends State<MyHomePage>
216216
}),
217217

218218
GFRating(
219-
rating: _rating,
220-
itemSize: 50,
219+
value: _rating,
220+
// size: 50,
221221
// filledIcon: Image.asset(
222222
// 'lib/assets/heart.png',
223223
// height: 50,
@@ -236,50 +236,45 @@ class _MyHomePageState extends State<MyHomePage>
236236
// width: 50,
237237
// color: Colors.amber,
238238
// ),
239+
// spacing: 8,
240+
// color: Colors.teal,
241+
// borderColor: Colors.tealAccent,
239242
// allowHalfRating: false,
240-
spacing: 8,
241-
color: Colors.teal,
242-
borderColor: Colors.tealAccent,
243-
onRatingChanged: (value) {
243+
onChanged: (value) {
244244
setState(() {
245245
_rating = value;
246-
print('user selected $_rating');
246+
print('user selected ${_rating}');
247247
});
248248
},
249249
),
250250

251-
SizedBox(
252-
height: 20,
253-
child: Text('selected rating ${_rating.toStringAsFixed(1)}'),
254-
),
255-
256251
GFRating(
257-
rating: _userRating,
258-
itemSize: 50,
259-
filledIcon: Image.asset(
260-
'lib/assets/heart.png',
261-
height: 50,
262-
width: 50,
263-
color: Colors.amber,
264-
),
265-
halfFilledIcon: Image.asset(
266-
'lib/assets/heart_half.png',
267-
height: 50,
268-
width: 50,
269-
color: Colors.amber,
270-
),
271-
defaultIcon: Image.asset(
272-
'lib/assets/heart_border.png',
273-
height: 50,
274-
width: 50,
275-
color: Colors.amber,
276-
),
277-
ratingController: _ratingController,
278-
textFormRating: true,
279-
spacing: 8,
280-
color: Colors.teal,
281-
borderColor: Colors.tealAccent,
282-
suffixIcon: MaterialButton(
252+
value: _userRating,
253+
// filledIcon: Image.asset(
254+
// 'lib/assets/heart.png',
255+
// height: 50,
256+
// width: 50,
257+
// color: Colors.amber,
258+
// ),
259+
// halfFilledIcon: Image.asset(
260+
// 'lib/assets/heart_half.png',
261+
// height: 50,
262+
// width: 50,
263+
// color: Colors.amber,
264+
// ),
265+
// defaultIcon: Image.asset(
266+
// 'lib/assets/heart_border.png',
267+
// height: 50,
268+
// width: 50,
269+
// color: Colors.amber,
270+
// ),
271+
// spacing: 8,
272+
// color: Colors.teal,
273+
// borderColor: Colors.tealAccent,
274+
controller: _ratingController,
275+
textForm: true,
276+
suffixIcon: GFButton(
277+
type: GFButtonType.transparent,
283278
onPressed: () {
284279
setState(() {
285280
_userRating = double.parse(_ratingController.text ?? '0.0');

lib/components/rating/gf_rating.dart

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ class GFRating extends StatefulWidget {
66
const GFRating({
77
this.itemCount = 5,
88
this.spacing = 0.0,
9-
this.rating = 0.0,
9+
this.value = 0.0,
1010
this.defaultIcon,
11-
this.onRatingChanged,
11+
this.onChanged,
1212
this.color,
1313
this.borderColor,
14-
this.itemSize = 25,
14+
this.size = 30,
1515
this.filledIcon,
1616
this.halfFilledIcon,
1717
this.allowHalfRating = true,
18-
this.textFormRating = false,
18+
this.textForm = false,
1919
this.suffixIcon,
20-
this.ratingController,
20+
this.controller,
2121
this.inputDecorations,
2222
this.margin = const EdgeInsets.symmetric(vertical: 16),
2323
this.padding = const EdgeInsets.symmetric(horizontal: 16),
24-
}) : assert(rating != null);
24+
}) : assert(value != null);
2525

2626
/// defines total number of rating items
2727
final int itemCount;
@@ -33,7 +33,7 @@ class GFRating extends StatefulWidget {
3333
final Color borderColor;
3434

3535
/// defines the size of items
36-
final double itemSize;
36+
final double size;
3737

3838
/// if true, allow half rating of items. Default it will be in true state
3939
final bool allowHalfRating;
@@ -51,19 +51,19 @@ class GFRating extends StatefulWidget {
5151
final double spacing;
5252

5353
/// defines the rating value
54-
final double rating;
54+
final double value;
5555

5656
/// return current rating whenever rating is updated
57-
final RatingChangeCallback onRatingChanged;
57+
final RatingChangeCallback onChanged;
5858

5959
/// if true, shows rating [TextFormField] with the rating bar, that allows the user input to show rating
60-
final bool textFormRating;
60+
final bool textForm;
6161

6262
/// defines the design and funtion of rating [TextFormField]'s suffix icon
6363
final Widget suffixIcon;
6464

6565
/// controls the [TextField] Controller of rating [TextFormField]
66-
final TextEditingController ratingController;
66+
final TextEditingController controller;
6767

6868
/// defines the [InputDecoration] of rating [TextFormField]
6969
final InputDecoration inputDecorations;
@@ -81,68 +81,68 @@ class GFRating extends StatefulWidget {
8181
class _GFRatingState extends State<GFRating> {
8282
Widget buildRatingBar(BuildContext context, int index) {
8383
Widget icon;
84-
if (index >= widget.rating) {
84+
if (index >= widget.value) {
8585
icon = widget.defaultIcon != null
8686
? widget.defaultIcon
8787
: Icon(
8888
Icons.star_border,
8989
color: widget.borderColor ?? Theme.of(context).primaryColor,
90-
size: widget.itemSize,
90+
size: widget.size,
9191
);
92-
} else if (!widget.textFormRating
93-
? index > widget.rating - (widget.allowHalfRating ? 0.5 : 1.0) &&
94-
index < widget.rating
95-
: index + 1 == widget.rating + 0.5) {
92+
} else if (!widget.textForm
93+
? index > widget.value - (widget.allowHalfRating ? 0.5 : 1.0) &&
94+
index < widget.value
95+
: index + 1 == widget.value + 0.5) {
9696
icon = widget.halfFilledIcon != null
9797
? widget.halfFilledIcon
9898
: Icon(
9999
Icons.star_half,
100100
color: widget.color ?? Theme.of(context).primaryColor,
101-
size: widget.itemSize,
101+
size: widget.size,
102102
);
103103
} else {
104104
icon = widget.filledIcon != null
105105
? widget.filledIcon
106106
: Icon(
107107
Icons.star,
108108
color: widget.color ?? Theme.of(context).primaryColor,
109-
size: widget.itemSize,
109+
size: widget.size,
110110
);
111111
}
112112

113113
return GestureDetector(
114114
onTap: () {
115-
if (widget.onRatingChanged != null) {
116-
widget.onRatingChanged(index + 1.0);
115+
if (widget.onChanged != null) {
116+
widget.onChanged(index + 1.0);
117117
}
118118
},
119119
onHorizontalDragUpdate: (dragDetails) {
120120
final RenderBox box = context.findRenderObject();
121121
final _pos = box.globalToLocal(dragDetails.globalPosition);
122-
final i = _pos.dx / widget.itemSize;
122+
final i = _pos.dx / widget.size;
123123
var newRating = widget.allowHalfRating ? i : i.round().toDouble();
124124
if (newRating > widget.itemCount) {
125125
newRating = widget.itemCount.toDouble();
126126
}
127127
if (newRating < 0) {
128128
newRating = 0.0;
129129
}
130-
if (widget.onRatingChanged != null) {
131-
widget.onRatingChanged(newRating);
130+
if (widget.onChanged != null) {
131+
widget.onChanged(newRating);
132132
}
133133
},
134134
child: icon,
135135
);
136136
}
137137

138138
@override
139-
Widget build(BuildContext context) => widget.textFormRating
139+
Widget build(BuildContext context) => widget.textForm
140140
? Column(children: <Widget>[
141141
Container(
142142
margin: widget.margin,
143143
padding: widget.padding,
144144
child: TextFormField(
145-
controller: widget.ratingController,
145+
controller: widget.controller,
146146
keyboardType: TextInputType.number,
147147
decoration: widget.inputDecorations == null
148148
? InputDecoration(

0 commit comments

Comments
 (0)