o2_overlay_dialog.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../style/index.dart';
  4. import 'o2_elevated_button.dart';
  5. class O2OverlayEntryDialog {
  6. factory O2OverlayEntryDialog() => _instance;
  7. static O2OverlayEntryDialog get instance => _instance;
  8. static final O2OverlayEntryDialog _instance =
  9. O2OverlayEntryDialog._internal();
  10. O2OverlayEntryDialog._internal();
  11. OverlayEntry? _overlayEntry;
  12. VoidCallback? _callback;
  13. bool isOpening = false; // 是否有弹窗显示中
  14. /// 考勤 极速打卡 弹出提示
  15. void openFastCheckInDialog(String title, String content) {
  16. final context = Get.context;
  17. if (context == null) {
  18. return;
  19. }
  20. if (isOpening) {
  21. return;
  22. }
  23. isOpening = true;
  24. _openDialog(context, _buildAlertDialog(context, title, content, showCheckIcon: true, showFooter: true));
  25. }
  26. /// 提示弹窗
  27. void openAlertDialog(String title, String content, {VoidCallback? callback}) {
  28. final context = Get.context;
  29. if (context == null) {
  30. return;
  31. }
  32. if (isOpening) {
  33. return;
  34. }
  35. isOpening = true;
  36. bool showFooter = false;
  37. if (callback != null) {
  38. _callback = callback;
  39. showFooter = true;
  40. }
  41. _openDialog(context, _buildAlertDialog(context, title, content, showFooter: showFooter));
  42. }
  43. void _openDialog(BuildContext context, Widget dialog) {
  44. _overlayEntry = OverlayEntry(builder: ((context) => dialog));
  45. //插入到 Overlay中显示 OverlayEntry
  46. Overlay.of(context).insert(_overlayEntry!);
  47. }
  48. void closeDiloag() {
  49. if (_overlayEntry != null) {
  50. _overlayEntry!.remove();
  51. _overlayEntry = null;
  52. }
  53. _callback = null;
  54. isOpening = false;
  55. }
  56. Widget _buildAlertDialog(
  57. BuildContext context, String title, String content, {bool showCheckIcon = false, bool showFooter = false}) {
  58. return _dialogBox(
  59. Card(
  60. shape: const RoundedRectangleBorder(
  61. borderRadius: BorderRadius.all(Radius.circular(10))),
  62. child: Padding(
  63. padding:
  64. const EdgeInsets.only(left: 14, right: 14, bottom: 14),
  65. child: Column(
  66. crossAxisAlignment: CrossAxisAlignment.start,
  67. children: [
  68. Row(
  69. children: [
  70. if (showCheckIcon)const Icon(O2IconFont.ok ,size: 24, color: Colors.green),
  71. const Spacer(),
  72. IconButton(
  73. onPressed: () => closeDiloag(),
  74. icon: const Icon(Icons.close),
  75. padding: const EdgeInsets.only(left: 8.0, top: 8.0, bottom: 8.0))
  76. ],
  77. ),
  78. Text(title,
  79. style: Theme.of(context)
  80. .textTheme
  81. .bodyLarge
  82. ?.copyWith(fontWeight: FontWeight.bold)),
  83. const SizedBox(height: 10),
  84. Expanded(
  85. flex: 1,
  86. child: Padding(
  87. padding:
  88. const EdgeInsets.only(left: 10, right: 10),
  89. child: Text(content,
  90. style: Theme.of(context).textTheme.bodyMedium))),
  91. const SizedBox(height: 10),
  92. if(showFooter) SizedBox(
  93. width: double.infinity,
  94. height: 36,
  95. child: O2ElevatedButton(() {
  96. if (_callback != null) {
  97. _callback!();
  98. }
  99. closeDiloag();
  100. },
  101. Text(
  102. 'positive'.tr,
  103. style: const TextStyle(fontSize: 18),
  104. ))),
  105. ],
  106. )),
  107. ),
  108. );
  109. }
  110. Widget _dialogBox(Widget child) {
  111. return Stack(
  112. children: [
  113. Container(
  114. width: double.infinity,
  115. height: double.infinity,
  116. color: Colors.black.withOpacity(0.6),
  117. ),
  118. Positioned(
  119. top: 44, left: 10, right: 10, height: 200, child: child)
  120. ],
  121. );
  122. }
  123. }