rating_dialog.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. library rating_dialog;
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_rating_bar/flutter_rating_bar.dart';
  4. import 'package:puzzleweave/skin/skin.dart';
  5. class RatingDialog extends StatefulWidget {
  6. /// The dialog's title
  7. final Text title;
  8. /// The dialog's message/description text
  9. final Text? message;
  10. /// The top image used for the dialog to be displayed
  11. final Widget? image;
  12. /// The rating bar (star icon & glow) color
  13. final Color starColor;
  14. /// The size of the star
  15. final double starSize;
  16. /// Disables the cancel button and forces the user to leave a rating
  17. final bool force;
  18. /// Show or hide the close button
  19. final bool showCloseButton;
  20. /// The initial rating of the rating bar
  21. final double initialRating;
  22. /// Display comment input area
  23. final bool enableComment;
  24. /// The comment's TextField hint text
  25. final String commentHint;
  26. /// The submit button's label/text
  27. final String submitButtonText;
  28. /// The submit button's label/text
  29. final TextStyle submitButtonTextStyle;
  30. /// Returns a RatingDialogResponse with user's rating and comment values
  31. final Function(RatingDialogResponse) onSubmitted;
  32. /// called when user cancels/closes the dialog
  33. final Function? onCancelled;
  34. const RatingDialog({
  35. super.key,
  36. required this.title,
  37. this.message,
  38. this.image,
  39. required this.submitButtonText,
  40. this.submitButtonTextStyle = const TextStyle(fontWeight: FontWeight.bold, fontSize: 17, color: Colors.white),
  41. required this.onSubmitted,
  42. this.starColor = Colors.amber,
  43. this.starSize = 40.0,
  44. this.onCancelled,
  45. this.showCloseButton = true,
  46. this.force = false,
  47. this.initialRating = 0,
  48. this.enableComment = true,
  49. this.commentHint = 'Tell us your comments',
  50. });
  51. @override
  52. State<RatingDialog> createState() => _RatingDialogState();
  53. }
  54. class _RatingDialogState extends State<RatingDialog> {
  55. final _commentController = TextEditingController();
  56. RatingDialogResponse? _response;
  57. @override
  58. void initState() {
  59. super.initState();
  60. _response = RatingDialogResponse(rating: widget.initialRating);
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. final content = Stack(
  65. alignment: Alignment.topRight,
  66. children: <Widget>[
  67. ClipRRect(
  68. borderRadius: BorderRadius.circular(20.0),
  69. child: Padding(
  70. padding: const EdgeInsets.fromLTRB(25, 30, 25, 5),
  71. child: Column(
  72. mainAxisSize: MainAxisSize.min,
  73. crossAxisAlignment: CrossAxisAlignment.stretch,
  74. children: <Widget>[
  75. widget.image != null ? Padding(padding: const EdgeInsets.only(top: 10, bottom: 20), child: widget.image) : Container(),
  76. widget.title,
  77. const SizedBox(height: 15),
  78. widget.message ?? Container(),
  79. const SizedBox(height: 15),
  80. FittedBox(
  81. fit: BoxFit.contain,
  82. child: RatingBar.builder(
  83. initialRating: widget.initialRating,
  84. glowColor: widget.starColor,
  85. minRating: 0,
  86. itemSize: widget.starSize,
  87. direction: Axis.horizontal,
  88. allowHalfRating: true,
  89. itemCount: 5,
  90. itemPadding: const EdgeInsets.symmetric(horizontal: 4.0),
  91. onRatingUpdate: (rating) {
  92. setState(() {
  93. _response!.rating = rating;
  94. });
  95. },
  96. itemBuilder: (context, _) => Icon(Icons.star, color: widget.starColor),
  97. ),
  98. ),
  99. widget.enableComment
  100. ? TextField(
  101. controller: _commentController,
  102. textAlign: TextAlign.center,
  103. textInputAction: TextInputAction.newline,
  104. minLines: 1,
  105. maxLines: 5,
  106. decoration: InputDecoration(hintText: widget.commentHint),
  107. )
  108. : const SizedBox(height: 15),
  109. Padding(
  110. padding: const EdgeInsets.all(20),
  111. child: ElevatedButton(
  112. style: ButtonStyle(
  113. backgroundColor: WidgetStateProperty.all<Color>(SkinHelper.color5),
  114. shape: WidgetStateProperty.all<RoundedRectangleBorder>(RoundedRectangleBorder(borderRadius: BorderRadius.circular(18.0))),
  115. ),
  116. onPressed: _response!.rating == 0
  117. ? null
  118. : () {
  119. if (!widget.force) Navigator.pop(context);
  120. _response!.comment = _commentController.text;
  121. widget.onSubmitted.call(_response!);
  122. },
  123. child: Text(widget.submitButtonText, style: widget.submitButtonTextStyle),
  124. ),
  125. ),
  126. ],
  127. ),
  128. ),
  129. ),
  130. if (!widget.force && widget.onCancelled != null && widget.showCloseButton) ...[
  131. IconButton(
  132. icon: const Icon(Icons.close, size: 18),
  133. onPressed: () {
  134. Navigator.pop(context);
  135. widget.onCancelled!.call();
  136. },
  137. ),
  138. ],
  139. ],
  140. );
  141. return AlertDialog(
  142. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)),
  143. titlePadding: EdgeInsets.zero,
  144. scrollable: true,
  145. title: content,
  146. );
  147. }
  148. }
  149. class RatingDialogResponse {
  150. /// The user's comment response
  151. String comment;
  152. /// The user's rating response
  153. double rating;
  154. RatingDialogResponse({this.rating = 0.0, this.comment = ''});
  155. }