Modal.dart 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:get/get.dart';
  4. typedef ModalFunc = dynamic Function();
  5. /// func 函数如果返回 bool 值, 则只有 true 的情况下才默认关闭弹窗
  6. /// 如果返回 Future , 则只有正常结束的时候 才关闭弹窗
  7. /// 其余情况, 都默认关闭弹窗
  8. void _handleFunc(ModalFunc? func, bool lazy) {
  9. // 是否立即关闭
  10. _close(bool canClose) {
  11. if (canClose) {
  12. _closeModal();
  13. }
  14. }
  15. ;
  16. // 如果 lazy 是 true 则不会立即关闭
  17. _close(!lazy);
  18. if (null != func) {
  19. dynamic res = func();
  20. if (res.runtimeType == bool) {
  21. if (res && lazy) {
  22. _close(lazy);
  23. }
  24. } else if (res is Future) {
  25. res.then((_) => _close(lazy));
  26. } else {
  27. _close(lazy);
  28. }
  29. } else {
  30. _close(lazy);
  31. }
  32. }
  33. /// 打开一个 dialog
  34. void showDialog(
  35. {required String title,
  36. lazy = false,
  37. String? message,
  38. Widget? content,
  39. ModalFunc? onConfirm,
  40. ModalFunc? onCancel}) {
  41. Get.dialog(Modal(
  42. type: 'dialog',
  43. title: title,
  44. message: message,
  45. content: content,
  46. onConfirm: () => _handleFunc(onConfirm, lazy),
  47. onCancel: () => _handleFunc(onCancel, lazy),
  48. ));
  49. }
  50. /// 打开一个 alert
  51. void showAlert(
  52. {required String title,
  53. bool lazy = false,
  54. String? message,
  55. Widget? content,
  56. ModalFunc? onConfirm}) {
  57. Get.dialog(Modal(
  58. type: 'alert',
  59. title: title,
  60. message: message,
  61. content: content,
  62. onConfirm: () => _handleFunc(onConfirm, lazy),
  63. ));
  64. }
  65. void _closeModal() {
  66. // Get.back(closeOverlays: true);
  67. Navigator.of(Get.overlayContext!, rootNavigator: true).pop();
  68. }
  69. class Modal extends StatelessWidget {
  70. String title;
  71. String type;
  72. String? message;
  73. Widget? content;
  74. VoidCallback? onConfirm;
  75. VoidCallback? onCancel;
  76. final _titleStyle = TextStyle(
  77. color: const Color(0xFF222222),
  78. fontSize: 20.sp,
  79. fontWeight: FontWeight.bold);
  80. final _messageStyle =
  81. TextStyle(color: const Color(0xFF666666), fontSize: 18.sp);
  82. final _primaryColor = const Color(0xFFFF703B);
  83. final _btnSize = Size(90.w, 36.w);
  84. final _btnShape = RoundedRectangleBorder(
  85. side: const BorderSide(color: Color(0xFFFF703B), width: 2.0),
  86. borderRadius: BorderRadius.all(Radius.circular(24.5.w)),
  87. );
  88. void _handleConfirm() {
  89. if (null != onConfirm) {
  90. onConfirm!();
  91. }
  92. }
  93. void _handleCancel() {
  94. if (null != onCancel) {
  95. onCancel!();
  96. }
  97. }
  98. Modal({
  99. Key? key,
  100. this.type = 'alert',
  101. required this.title,
  102. this.message,
  103. this.content,
  104. this.onConfirm,
  105. this.onCancel,
  106. }) : super(key: key);
  107. Widget _confirmBtn() {
  108. return ElevatedButton(
  109. child: Text("确定",
  110. style: TextStyle(
  111. color: Colors.white, fontSize: 18.sp, letterSpacing: 5.sp)),
  112. style: ElevatedButton.styleFrom(
  113. primary: _primaryColor,
  114. elevation: 0,
  115. minimumSize: _btnSize,
  116. shape: _btnShape,
  117. ),
  118. onPressed: _handleConfirm,
  119. );
  120. }
  121. Widget _cancelBtn() {
  122. return ElevatedButton(
  123. child: Text("取消",
  124. style: TextStyle(
  125. color: _primaryColor, fontSize: 18.sp, letterSpacing: 5.sp)),
  126. style: ElevatedButton.styleFrom(
  127. primary: Colors.white,
  128. elevation: 0,
  129. minimumSize: _btnSize,
  130. shape: _btnShape,
  131. ),
  132. onPressed: _handleCancel,
  133. );
  134. }
  135. Widget _alert() {
  136. return SimpleDialog(
  137. shape:
  138. RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
  139. titleTextStyle: _titleStyle,
  140. title: Text(title, textAlign: TextAlign.center),
  141. children: [
  142. if (null != message)
  143. Text(message!, textAlign: TextAlign.center, style: _messageStyle),
  144. if (null != content) content!,
  145. SizedBox(height: 16.w),
  146. Center(
  147. child: _confirmBtn(),
  148. ),
  149. SizedBox(height: 10.w),
  150. ]);
  151. }
  152. Widget _dialog() {
  153. Widget hSpacer = SizedBox(width: 16.w);
  154. return SimpleDialog(
  155. shape:
  156. RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
  157. titleTextStyle: _titleStyle,
  158. title: Text(title, textAlign: TextAlign.center),
  159. alignment: Alignment.center,
  160. children: [
  161. if (null != message)
  162. Text(message!, textAlign: TextAlign.center, style: _messageStyle),
  163. if (null != content) content!,
  164. SizedBox(height: 16.w),
  165. Row(
  166. mainAxisAlignment: MainAxisAlignment.center,
  167. children: [
  168. hSpacer,
  169. Center(
  170. child: _confirmBtn(),
  171. ),
  172. hSpacer,
  173. Center(
  174. child: _cancelBtn(),
  175. ),
  176. hSpacer,
  177. ],
  178. ),
  179. SizedBox(height: 10.w),
  180. ]);
  181. }
  182. @override
  183. Widget build(BuildContext context) {
  184. return type == 'alert' ? _alert() : _dialog();
  185. }
  186. }