Created
December 28, 2020 18:23
-
-
Save azamsharp/aca657afd452b8e98b771df7ab85f8f6 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
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