permission_dialog.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import 'dart:async';
  2. import 'package:flutter/Material.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  6. import 'package:get/get.dart';
  7. import '../resource/assets.gen.dart';
  8. import '../resource/colors.gen.dart';
  9. import '../utils/styles.dart';
  10. class PermissionDialog {
  11. static Future<bool> showRequestDialog(
  12. String titleTxt,
  13. Widget desc,
  14. String sureTxt,
  15. String permissionDesc, {
  16. Future<bool> Function()? sureClick,
  17. }) async {
  18. const tag = 'PermissionDialog';
  19. final completer = Completer<bool>();
  20. SmartDialog.show(
  21. tag: tag,
  22. alignment: const Alignment(0, 0),
  23. builder: (_) {
  24. return Container(
  25. width: 300.w,
  26. decoration: BoxDecoration(
  27. color: Colors.white,
  28. borderRadius: BorderRadius.circular(12),
  29. ),
  30. child: Stack(
  31. children: [
  32. Container(
  33. padding: EdgeInsets.symmetric(horizontal: 19.w),
  34. child: IntrinsicHeight(
  35. child: Column(
  36. children: [
  37. SizedBox(height: 20.h),
  38. Text(
  39. titleTxt,
  40. style: TextStyle(
  41. fontWeight: FontWeight.bold,
  42. fontSize: 18.sp,
  43. color: Colors.black,
  44. ),
  45. ),
  46. SizedBox(height: 14.h),
  47. desc,
  48. SizedBox(height: 30.h),
  49. GestureDetector(
  50. onTap: () async {
  51. if (sureClick != null) {
  52. // 显示权限说明
  53. Get.snackbar(
  54. '权限请求',
  55. permissionDesc,
  56. animationDuration: Duration(milliseconds: 0),
  57. snackPosition: SnackPosition.TOP,
  58. backgroundColor: Colors.black.withOpacity(0.8),
  59. colorText: Colors.white,
  60. duration: null,
  61. );
  62. // 权限请求逻辑
  63. bool shouldDismiss = await sureClick();
  64. Get.closeAllSnackbars();
  65. if (shouldDismiss) {
  66. SmartDialog.dismiss(tag: tag);
  67. completer.complete(true);
  68. } else {
  69. // 保持弹窗,等待用户操作
  70. completer.complete(false);
  71. }
  72. } else {
  73. SmartDialog.dismiss(tag: tag);
  74. completer.complete(true);
  75. }
  76. },
  77. child: Container(
  78. decoration: Styles.getActivateButtonDecoration(50.r),
  79. width: 130.w,
  80. height: 40.w,
  81. child: Center(
  82. child: Text(
  83. sureTxt,
  84. style: TextStyle(
  85. fontSize: 16.sp,
  86. color: Colors.white,
  87. ),
  88. ),
  89. ),
  90. ),
  91. ),
  92. SizedBox(height: 24.h),
  93. ],
  94. ),
  95. ),
  96. ),
  97. Positioned(
  98. top: 17.w,
  99. right: 18.w,
  100. child: GestureDetector(
  101. onTap: () {
  102. SmartDialog.dismiss(tag: tag);
  103. completer.complete(false); // 用户主动关闭
  104. },
  105. child: Assets.images.iconCustomDialogClose.image(
  106. width: 24.w,
  107. height: 24.w,
  108. ),
  109. ),
  110. ),
  111. ],
  112. ),
  113. );
  114. },
  115. );
  116. return completer.future;
  117. }
  118. static Widget buildStorageView() {
  119. return RichText(
  120. textAlign: TextAlign.center,
  121. text: TextSpan(
  122. style: TextStyle(fontSize: 15.sp, color: ColorName.black80),
  123. children: const <TextSpan>[
  124. TextSpan(text: '使用该功能App需要访问您设备权限'),
  125. TextSpan(
  126. text: '“照片和媒体”',
  127. style: TextStyle(
  128. fontWeight: FontWeight.bold,
  129. color: ColorName.black90,
  130. ),
  131. ),
  132. TextSpan(text: ',开启权限后,您即可上传图片并体验相关服务。'),
  133. ],
  134. ),
  135. );
  136. }
  137. }