Modal.dart 5.2KB

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