Modal.dart 4.8KB

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