alert_dialog.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import 'package:electronic_assistant/popup/talk_popup.dart';
  2. import 'package:electronic_assistant/utils/expand.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/widgets.dart';
  6. import 'package:flutter_screenutil/flutter_screenutil.dart';
  7. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  8. class EAAlertDialog {
  9. static void show({
  10. String? title,
  11. String? contentText,
  12. Widget? contentWidget,
  13. String? confirmText,
  14. String? cancelText,
  15. VoidCallback? cancelOnTap,
  16. VoidCallback? confirmOnTap,
  17. VoidCallback? onDismiss,
  18. }) {
  19. SmartDialog.show(
  20. builder: (_) {
  21. return _EAAlertDialog(
  22. title: title,
  23. content: contentText,
  24. contentWidget: contentWidget,
  25. confirmText: confirmText,
  26. cancelText: cancelText,
  27. confirmOnTap: confirmOnTap,
  28. cancelOnTap: cancelOnTap);
  29. },
  30. tag: "EAAlertDialog",
  31. onDismiss: onDismiss,
  32. clickMaskDismiss: false,
  33. );
  34. }
  35. static void dismiss() {
  36. SmartDialog.dismiss(tag: "EAAlertDialog");
  37. }
  38. }
  39. class _EAAlertDialog extends Dialog {
  40. // 标题
  41. final String? title;
  42. final Widget? contentWidget;
  43. // 内容
  44. final String? content;
  45. // 取消按钮文字
  46. final String? cancelText;
  47. // 取消按钮回调
  48. final VoidCallback? cancelOnTap;
  49. // 确认按钮文字
  50. final String? confirmText;
  51. // 确认按钮文字回调
  52. final VoidCallback? confirmOnTap;
  53. const _EAAlertDialog({
  54. this.title,
  55. this.content,
  56. this.contentWidget,
  57. required this.cancelText,
  58. required this.confirmText,
  59. this.cancelOnTap,
  60. this.confirmOnTap,
  61. });
  62. @override
  63. Widget build(BuildContext context) {
  64. return Center(
  65. child: Column(
  66. children: [
  67. const Spacer(),
  68. Container(
  69. alignment: Alignment.center,
  70. padding: EdgeInsets.symmetric(vertical: 24.h, horizontal: 16.w),
  71. width: ScreenUtil().screenWidth - 80.w,
  72. decoration: BoxDecoration(
  73. borderRadius: BorderRadius.all(Radius.circular(12.w)),
  74. color: "#FFFFFF".toColor(),
  75. ),
  76. child: Column(
  77. mainAxisAlignment: MainAxisAlignment.center,
  78. children: [
  79. /// 标题
  80. if (title != null)
  81. Text(
  82. title!,
  83. style: TextStyle(
  84. color: "#25262A".toColor(),
  85. fontSize: 15.sp,
  86. fontWeight: FontWeight.w500,
  87. decoration: TextDecoration.none,
  88. ),
  89. ),
  90. if (title != null) SizedBox(height: 12.h),
  91. if (content != null)
  92. /// 内容
  93. Text(
  94. content ?? "",
  95. textAlign: TextAlign.center,
  96. style: TextStyle(
  97. color: "#5F5F61".toColor(),
  98. fontWeight: FontWeight.w500,
  99. fontSize: 14.sp,
  100. decoration: TextDecoration.none,
  101. ),
  102. ),
  103. if (contentWidget != null) contentWidget!,
  104. SizedBox(height: 34.h),
  105. ///
  106. _buttonWidget(context),
  107. ],
  108. ),
  109. ),
  110. //
  111. const Spacer(),
  112. ],
  113. ),
  114. );
  115. }
  116. Widget _buttonWidget(BuildContext context) {
  117. if (cancelText == null) {
  118. return Row(
  119. children: [
  120. const Spacer(),
  121. ///确认按钮
  122. GestureDetector(
  123. onTap: () {
  124. _clickConfirm();
  125. },
  126. child: _button(text: confirmText ?? "", color: "#6177F2".toColor()),
  127. ),
  128. //
  129. const Spacer(),
  130. ],
  131. );
  132. } else {
  133. return Row(
  134. children: [
  135. /// 取消按钮
  136. Expanded(
  137. child: InkWell(
  138. onTap: () {
  139. _clickCancel();
  140. },
  141. child: _button(
  142. text: cancelText ?? "",
  143. color: "#F0F0F0".toColor(),
  144. textColor: "#5F5F61".toColor(),
  145. ),
  146. ),
  147. ),
  148. ///
  149. const SizedBox(width: 8),
  150. /// 确认按钮
  151. Expanded(
  152. child: InkWell(
  153. onTap: () {
  154. _clickConfirm();
  155. },
  156. child:
  157. _button(text: confirmText ?? "", color: "#6177F2".toColor()),
  158. ),
  159. ),
  160. ],
  161. );
  162. }
  163. }
  164. Widget _button({
  165. required String text,
  166. Color? color,
  167. BoxBorder? border,
  168. Color? textColor = Colors.white,
  169. }) {
  170. return Container(
  171. height: 36.w,
  172. decoration: BoxDecoration(
  173. borderRadius: BorderRadius.all(Radius.circular(8.w)),
  174. color: color,
  175. border: border,
  176. ),
  177. alignment: Alignment.center,
  178. child: Text(
  179. text,
  180. style: TextStyle(
  181. color: textColor,
  182. fontSize: 14.sp,
  183. fontWeight: FontWeight.w500,
  184. ),
  185. ),
  186. );
  187. }
  188. void _clickConfirm() {
  189. EAAlertDialog.dismiss();
  190. if (confirmOnTap != null) {
  191. confirmOnTap!();
  192. }
  193. }
  194. void _clickCancel() {
  195. EAAlertDialog.dismiss();
  196. if (cancelOnTap != null) {
  197. cancelOnTap!();
  198. }
  199. }
  200. }