Last active
June 24, 2021 18:44
-
-
Save arulwastaken/b700c824d120655bf435bb6f05b833e5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import 'package:myapp/src/values/colors.dart'; | |
import 'package:flutter/material.dart'; | |
class RatingBar extends StatefulWidget { | |
final double rating; | |
final bool makeRating; | |
final double? size; | |
final Function(double rating)? makeRatingCallback; | |
const RatingBar( | |
{Key? key, | |
required this.rating, | |
this.makeRating = false, | |
this.makeRatingCallback, this.size}) | |
: super(key: key); | |
@override | |
_RatingBarState createState() => _RatingBarState(); | |
} | |
class _RatingBarState extends State<RatingBar> { | |
late double selectedRating = widget.rating; | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
mainAxisAlignment: MainAxisAlignment.start, | |
children: new List<Widget>.generate(5, (index) { | |
if (widget.makeRating) { | |
return IconButton( | |
padding: EdgeInsets.zero, | |
iconSize: 30, | |
splashRadius: 2, | |
onPressed: () { | |
double newRating = index.toDouble() + 1; | |
setState(() { | |
selectedRating = newRating; | |
}); | |
widget.makeRatingCallback?.call(selectedRating); | |
}, | |
icon: _buildIcon(index, selectedRating)); | |
} else { | |
return _buildIcon(index, widget.rating); | |
} | |
}), | |
); | |
} | |
Widget _buildIcon(int index, double rating) { | |
return Icon( | |
rating.ceil() == (index + 1) && rating != rating.ceil() | |
? Icons.star_half_outlined | |
: Icons.star, | |
color: rating > index ? MyColors.starSelected : MyColors.starUnSelected, | |
size: widget.size != null ? widget.size : widget.makeRating ? 30 : 20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment