Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created December 28, 2020 18:23
Show Gist options
  • Save azamsharp/aca657afd452b8e98b771df7ab85f8f6 to your computer and use it in GitHub Desktop.
Save azamsharp/aca657afd452b8e98b771df7ab85f8f6 to your computer and use it in GitHub Desktop.
class Rating extends StatefulWidget {
final int maximumRating;
final Function(int) onRatingSelected;
Rating(this.onRatingSelected, [this.maximumRating = 5]);
@override
_Rating createState() => _Rating();
}
class _Rating extends State<Rating> {
int _currentRating = 0;
Widget _buildRatingStar(int index) {
if (index < _currentRating) {
return Icon(Icons.star, color: Colors.orange);
} else {
return Icon(Icons.star_border_outlined);
}
}
Widget _buildBody() {
final stars = List<Widget>.generate(this.widget.maximumRating, (index) {
return GestureDetector(
child: _buildRatingStar(index),
onTap: () {
setState(() {
_currentRating = index + 1;
});
this.widget.onRatingSelected(_currentRating);
},
);
});
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: stars,
)
],
);
}
@override
Widget build(BuildContext context) {
return _buildBody();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment