-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate_bar.dart
420 lines (379 loc) · 10.9 KB
/
rate_bar.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import 'package:flutter/material.dart';
class RatingWidget {
final Widget full;
final Widget half;
final Widget empty;
RatingWidget({
required this.full,
required this.half,
required this.empty,
});
}
class _HalfRatingWidget extends StatelessWidget {
final Widget? child;
final double? size;
final int? alpha;
final bool enableMask;
final bool rtlMode;
final Color unratedColor;
_HalfRatingWidget({
this.size,
this.child,
this.alpha,
this.enableMask = true,
this.rtlMode = false,
this.unratedColor = Colors.white,
});
@override
Widget build(BuildContext context) {
return SizedBox(
height: size,
width: size,
child: enableMask
? Stack(
fit: StackFit.expand,
children: [
FittedBox(
fit: BoxFit.contain,
child: _NoRatingWidget(
child: child,
size: size,
alpha: alpha,
unratedColor: unratedColor,
),
),
FittedBox(
fit: BoxFit.contain,
child: ClipRect(
clipper: _HalfClipper(
rtlMode: rtlMode,
),
child: child,
),
),
],
)
: FittedBox(
child: child,
fit: BoxFit.contain,
),
);
}
}
class _HalfClipper extends CustomClipper<Rect> {
final bool rtlMode;
_HalfClipper({
this.rtlMode = false,
});
@override
Rect getClip(Size size) => rtlMode
? Rect.fromLTRB(
size.width / 2,
0.0,
size.width,
size.height,
)
: Rect.fromLTRB(
0.0,
0.0,
size.width / 2,
size.height,
);
@override
bool shouldReclip(CustomClipper<Rect> oldClipper) => true;
}
class _NoRatingWidget extends StatelessWidget {
final double? size;
final Widget? child;
final int? alpha;
final bool enableMask;
final Color unratedColor;
_NoRatingWidget({
this.size,
this.child,
this.alpha,
this.enableMask = true,
this.unratedColor = Colors.white,
});
@override
Widget build(BuildContext context) {
return SizedBox(
height: size,
width: size,
child: FittedBox(
fit: BoxFit.contain,
child: enableMask
? _ColorMask(
color: unratedColor.withAlpha(255 - alpha!),
child: child,
)
: child,
),
);
}
}
class _ColorMask extends StatelessWidget {
final Color? color;
final Widget? child;
_ColorMask({
this.color,
this.child,
});
@override
Widget build(BuildContext context) {
return ShaderMask(
shaderCallback: (bounds) => LinearGradient(colors: [
color!,
color!,
]).createShader(bounds),
blendMode: BlendMode.srcATop,
child: child,
);
}
}
class RatingBar extends StatefulWidget {
///[itemCount] is the count of rating bar items.
final int itemCount;
///[initialRating] is initial rating to be set to the rating bar.
final double initialRating;
///[onRatingUpdate] is a callback which return current rating.
final ValueChanged<double> onRatingUpdate;
/// [itemSize] of each rating item in the bar.
final double itemSize;
///Default [allowHalfRating] = false. Setting true enables half rating support.
final bool allowHalfRating;
/// [itemPadding] gives padding to each rating item.
final EdgeInsets itemPadding;
/// [ignoreGestures]=false, if set to true will disable any gestures over the rating bar.
final bool ignoreGestures;
/// [tapOnlyMode]=false, if set to true will disable drag to rate feature. Note: Enabling this mode will disable half rating capability.
final bool tapOnlyMode;
/// The text flows from right to left if [textDirection] = TextDirection.rtl
final TextDirection? textDirection;
/// Widget for each rating bar indicator item.
final IndexedWidgetBuilder? itemBuilder;
/// Defines the color opacity for unrated portion.
///
/// Default = 80
final int alpha;
/// Customizes the Rating Bar item with [RatingWidget].
final RatingWidget? ratingWidget;
/// if set to true, Rating Bar item will glow when being touched.
///
/// Default = true
final bool glow;
/// Defines the radius of glow.
///
/// Default = 2
final double glowRadius;
/// Defines color for glow.
///
/// Default = theme's accent color
final Color? glowColor;
/// Direction or rating bar indicator. Either can be vertical or horizontal.
///
/// Default = Axis.horizontal
final Axis direction;
/// Defines color for unrated portion.
final Color unratedColor;
RatingBar({
this.itemCount = 5,
this.initialRating = 0.0,
required this.onRatingUpdate,
this.alpha = 80,
this.itemSize = 40.0,
this.allowHalfRating = false,
this.itemBuilder,
this.itemPadding = const EdgeInsets.all(0.0),
this.ignoreGestures = false,
this.tapOnlyMode = false,
this.textDirection,
this.ratingWidget,
this.glow = true,
this.glowRadius = 2,
this.direction = Axis.horizontal,
this.glowColor,
this.unratedColor = Colors.white,
}) : assert(
(itemBuilder == null && ratingWidget != null) ||
(itemBuilder != null && ratingWidget == null),
'itemBuilder and ratingWidget can\'t be initialized at the same time.'
'Either remove ratingWidget or itembuilder.',
);
@override
_RatingBarState createState() => _RatingBarState();
}
class _RatingBarState extends State<RatingBar> {
double _rating = 0.0;
double _ratingHistory = 0.0;
double iconRating = 0.0;
bool _isRTL = false;
ValueNotifier<bool> _glow = ValueNotifier(false);
@override
void initState() {
super.initState();
_rating = widget.initialRating;
_ratingHistory = widget.initialRating;
}
@override
void dispose() {
_glow.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
_isRTL = (widget.textDirection ?? Directionality.of(context)) ==
TextDirection.rtl;
if (_ratingHistory != widget.initialRating) {
_rating = widget.initialRating;
_ratingHistory = widget.initialRating;
}
iconRating = 0.0;
return Material(
color: Colors.transparent,
child: Wrap(
alignment: WrapAlignment.start,
textDirection: _isRTL ? TextDirection.rtl : TextDirection.ltr,
direction: widget.direction,
children: List.generate(
widget.itemCount,
(index) {
return _buildRating(context, index);
},
),
),
);
}
Widget _buildRating(BuildContext context, int index) {
Widget ratingWidget;
if (index >= _rating) {
ratingWidget = _NoRatingWidget(
size: widget.itemSize,
child:
widget.ratingWidget?.empty ?? widget.itemBuilder!(context, index),
enableMask: widget.ratingWidget == null,
alpha: widget.alpha,
unratedColor: widget.unratedColor,
);
} else if (index >= _rating - (widget.allowHalfRating ? 0.5 : 1.0) &&
index < _rating &&
widget.allowHalfRating) {
ratingWidget = _HalfRatingWidget(
size: widget.itemSize,
child: widget.ratingWidget?.half ?? widget.itemBuilder!(context, index),
enableMask: widget.ratingWidget == null,
alpha: widget.alpha,
rtlMode: _isRTL,
unratedColor: widget.unratedColor,
);
iconRating += 0.5;
} else {
ratingWidget = SizedBox(
width: widget.itemSize,
height: widget.itemSize,
child: FittedBox(
fit: BoxFit.contain,
child:
widget.ratingWidget?.full ?? widget.itemBuilder!(context, index),
),
);
iconRating += 1.0;
}
return IgnorePointer(
ignoring: widget.ignoreGestures,
child: GestureDetector(
onTap: () {
widget.onRatingUpdate(index + 1.0);
setState(() {
_rating = index + 1.0;
});
},
onHorizontalDragStart: (_) {
if (widget.direction == Axis.horizontal) _glow.value = true;
},
onHorizontalDragEnd: (_) {
if (widget.direction == Axis.horizontal) {
_glow.value = false;
widget.onRatingUpdate(iconRating);
iconRating = 0.0;
}
},
onHorizontalDragUpdate: (dragUpdates) {
if (widget.direction == Axis.horizontal)
_dragOperation(dragUpdates, widget.direction);
},
onVerticalDragStart: (_) {
if (widget.direction == Axis.vertical) _glow.value = true;
},
onVerticalDragEnd: (_) {
if (widget.direction == Axis.vertical) {
_glow.value = false;
widget.onRatingUpdate(iconRating);
iconRating = 0.0;
}
},
onVerticalDragUpdate: (dragUpdates) {
if (widget.direction == Axis.vertical)
_dragOperation(dragUpdates, widget.direction);
},
child: Padding(
padding: widget.itemPadding,
child: ValueListenableBuilder(
valueListenable: _glow,
builder: (context, dynamic glow, _) {
if (glow && widget.glow) {
Color glowColor =
widget.glowColor ?? Theme.of(context).accentColor;
return DecoratedBox(
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: glowColor.withAlpha(30),
blurRadius: 10,
spreadRadius: widget.glowRadius,
),
BoxShadow(
color: glowColor.withAlpha(20),
blurRadius: 10,
spreadRadius: widget.glowRadius,
),
],
),
child: ratingWidget,
);
} else {
return ratingWidget;
}
},
),
),
),
);
}
void _dragOperation(DragUpdateDetails dragDetails, Axis direction) {
if (!widget.tapOnlyMode) {
RenderBox box = context.findRenderObject() as RenderBox;
var _pos = box.globalToLocal(dragDetails.globalPosition);
double i;
if (direction == Axis.horizontal) {
i = _pos.dx / (widget.itemSize + widget.itemPadding.horizontal);
} else {
i = _pos.dy / (widget.itemSize + widget.itemPadding.vertical);
}
var currentRating = widget.allowHalfRating ? i : i.round().toDouble();
if (currentRating > widget.itemCount) {
currentRating = widget.itemCount.toDouble();
}
if (currentRating < 0) {
currentRating = 0.0;
}
if (_isRTL && widget.direction == Axis.horizontal) {
currentRating = widget.itemCount - currentRating;
}
setState(() {
_rating = currentRating;
});
}
}
}