forked from Skyost/RateMyApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed the non-null safe dependency
smoothratingbar
.
- Loading branch information
Showing
5 changed files
with
99 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Credits : https://github.com/thangmam/smoothratingbar. | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
typedef RatingChangeCallback = void Function(double rating); | ||
|
||
class SmoothStarRating extends StatelessWidget { | ||
final int starCount; | ||
final double rating; | ||
final RatingChangeCallback? onRatingChanged; | ||
final Color? color; | ||
final Color? borderColor; | ||
final double size; | ||
final bool allowHalfRating; | ||
final IconData? filledIconData; | ||
final IconData? halfFilledIconData; | ||
final IconData? defaultIconData; //this is needed only when having fullRatedIconData && halfRatedIconData | ||
final double spacing; | ||
|
||
const SmoothStarRating({ | ||
this.starCount = 5, | ||
this.spacing = 0.0, | ||
this.rating = 0.0, | ||
this.defaultIconData, | ||
this.onRatingChanged, | ||
this.color, | ||
this.borderColor, | ||
this.size = 25, | ||
this.filledIconData, | ||
this.halfFilledIconData, | ||
this.allowHalfRating = true, | ||
}); | ||
|
||
Widget buildStar(BuildContext context, int index) { | ||
Icon icon; | ||
if (index >= rating) { | ||
icon = Icon( | ||
defaultIconData ?? Icons.star_border, | ||
color: borderColor ?? Theme.of(context).primaryColor, | ||
size: size, | ||
); | ||
} else if (index > rating - (allowHalfRating ? 0.5 : 1.0) && index < rating) { | ||
icon = Icon( | ||
halfFilledIconData ?? Icons.star_half, | ||
color: color ?? Theme.of(context).primaryColor, | ||
size: size, | ||
); | ||
} else { | ||
icon = Icon( | ||
filledIconData ?? Icons.star, | ||
color: color ?? Theme.of(context).primaryColor, | ||
size: size, | ||
); | ||
} | ||
|
||
return GestureDetector( | ||
onTap: () { | ||
if (onRatingChanged != null) { | ||
onRatingChanged!(index + 1.0); | ||
} | ||
}, | ||
onHorizontalDragUpdate: (dragDetails) { | ||
RenderBox box = context.findRenderObject() as RenderBox; | ||
Offset position = box.globalToLocal(dragDetails.globalPosition); | ||
double i = position.dx / size; | ||
double newRating = allowHalfRating ? i : i.round().toDouble(); | ||
if (newRating > starCount) { | ||
newRating = starCount.toDouble(); | ||
} | ||
if (newRating < 0) { | ||
newRating = 0.0; | ||
} | ||
if (onRatingChanged != null) { | ||
onRatingChanged!(newRating); | ||
} | ||
}, | ||
child: icon, | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) => Material( | ||
color: Colors.transparent, | ||
child: Wrap( | ||
alignment: WrapAlignment.start, | ||
spacing: spacing, | ||
children: List.generate(starCount, (index) => buildStar(context, index)), | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters