alert_dialog.dart 5.4 KB

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