Modal.dart 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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({ required String title, String? message, Widget? content, ModalFunc? onConfirm, ModalFunc? onCancel }) {
  28. Get.dialog(
  29. Modal(
  30. type: 'dialog',
  31. title: title,
  32. message: message,
  33. content: content,
  34. onConfirm: () => _handleFunc(onConfirm),
  35. onCancel: () => _handleFunc(onCancel),
  36. )
  37. );
  38. }
  39. /// 打开一个 alert
  40. void showAlert({ required String title, String? message, Widget? content, ModalFunc? onConfirm }) {
  41. Get.dialog(
  42. Modal(
  43. type: 'alert',
  44. title: title,
  45. message: message,
  46. content: content,
  47. onConfirm: () => _handleFunc(onConfirm),
  48. )
  49. );
  50. }
  51. void _closeModal() {
  52. // Get.back(closeOverlays: true);
  53. Navigator.of(Get.overlayContext!, rootNavigator: true).pop();
  54. }
  55. class Modal extends StatelessWidget {
  56. String title;
  57. String type;
  58. String? message;
  59. Widget? content;
  60. VoidCallback? onConfirm;
  61. VoidCallback? onCancel;
  62. final _titleStyle = TextStyle(color: const Color(0xFF222222), fontSize: 20.sp, fontWeight: FontWeight.bold);
  63. final _messageStyle = TextStyle(color: const Color(0xFF666666), fontSize: 18.sp);
  64. final _primaryColor = const Color(0xFFFF703B);
  65. final _btnSize = Size(90.w, 36.w);
  66. final _btnShape = RoundedRectangleBorder(
  67. side: const BorderSide(color: Color(0xFFFF703B), width: 2.0),
  68. borderRadius: BorderRadius.all(Radius.circular(24.5.w)),
  69. );
  70. void _handleConfirm() {
  71. if (null != onConfirm) {
  72. onConfirm!();
  73. }
  74. }
  75. void _handleCancel() {
  76. if (null != onCancel) {
  77. onCancel!();
  78. }
  79. }
  80. Modal({Key? key,
  81. this.type = 'alert',
  82. required this.title,
  83. this.message,
  84. this.content,
  85. this.onConfirm,
  86. this.onCancel,
  87. }): super(key: key);
  88. Widget _confirmBtn() {
  89. return ElevatedButton(
  90. child: Text("确定", style: TextStyle(color: Colors.white, fontSize: 18.sp, letterSpacing: 5.sp)),
  91. style: ElevatedButton.styleFrom(
  92. primary: _primaryColor,
  93. elevation: 0,
  94. minimumSize: _btnSize,
  95. shape: _btnShape,
  96. ),
  97. onPressed: _handleConfirm,
  98. );
  99. }
  100. Widget _cancelBtn() {
  101. return ElevatedButton(
  102. child: Text("取消", style: TextStyle(color: _primaryColor, fontSize: 18.sp, letterSpacing: 5.sp)),
  103. style: ElevatedButton.styleFrom(
  104. primary: Colors.white,
  105. elevation: 0,
  106. minimumSize: _btnSize,
  107. shape: _btnShape,
  108. ),
  109. onPressed: _handleCancel,
  110. );
  111. }
  112. Widget _alert() {
  113. return SimpleDialog(
  114. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
  115. titleTextStyle: _titleStyle,
  116. title: Text(title, textAlign: TextAlign.center),
  117. children: [
  118. if (null != message) Text(message!, textAlign: TextAlign.center, style: _messageStyle),
  119. if (null != content) content!,
  120. SizedBox(height: 16.w),
  121. Center(
  122. child: _confirmBtn(),
  123. ),
  124. SizedBox(height: 10.w),
  125. ]
  126. );
  127. }
  128. Widget _dialog() {
  129. Widget hSpacer = SizedBox(width: 16.w);
  130. return SimpleDialog(
  131. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
  132. titleTextStyle: _titleStyle,
  133. title: Text(title, textAlign: TextAlign.center),
  134. alignment: Alignment.center,
  135. children: [
  136. if (null != message) Text(message!, textAlign: TextAlign.center, style: _messageStyle),
  137. if (null != content) content!,
  138. SizedBox(height: 16.w),
  139. Row(
  140. mainAxisAlignment: MainAxisAlignment.center,
  141. children: [
  142. hSpacer,
  143. Center(
  144. child: _confirmBtn(),
  145. ),
  146. hSpacer,
  147. Center(
  148. child: _cancelBtn(),
  149. ),
  150. hSpacer,
  151. ],
  152. ),
  153. SizedBox(height: 10.w),
  154. ]
  155. );
  156. }
  157. @override
  158. Widget build(BuildContext context) {
  159. return type == 'alert' ? _alert() : _dialog();
  160. }
  161. }