Modal.dart 5.0KB

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