app_custom_dialog.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:cs_resources/generated/l10n.dart';
  3. import 'package:cs_resources/theme/app_colors_theme.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_hooks/flutter_hooks.dart';
  6. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  7. import 'package:hooks_riverpod/hooks_riverpod.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:widgets/ext/ex_widget.dart';
  10. import 'package:widgets/my_load_image.dart';
  11. import '../my_text_view.dart';
  12. import 'dart:ui';
  13. import 'package:flutter/cupertino.dart';
  14. import 'package:flutter/widgets.dart';
  15. import 'package:widgets/widget_export.dart';
  16. import '../no_shadow_scroll_behavior.dart';
  17. /*
  18. * 自定义的弹窗
  19. * 可自定义设置弹窗的宽度 默认宽度 MediaQuery.of(context).size.width * 0.65
  20. * 弹框内容自定义传入 messageBuilder
  21. */
  22. class AppCustomDialog extends HookConsumerWidget {
  23. String? title;
  24. String message;
  25. double? dialogWidth;
  26. double? contentBoxMaxHeight;
  27. double? contentBoxMinHeight;
  28. Widget Function(BuildContext context)? messageBuilder;
  29. VoidCallback confirmAction;
  30. VoidCallback? cancelAction;
  31. bool isShowCancelBtn;
  32. bool isShowConfirmBtn = true;
  33. String? confirmTxt;
  34. String? cancelTxt;
  35. AppCustomDialog({super.key,
  36. required this.message,
  37. required this.confirmAction,
  38. this.messageBuilder,
  39. this.title,
  40. this.dialogWidth,
  41. this.contentBoxMaxHeight = 500,
  42. this.contentBoxMinHeight = 300,
  43. this.cancelAction,
  44. this.isShowCancelBtn = true,
  45. this.isShowConfirmBtn = true,
  46. this.confirmTxt,
  47. this.cancelTxt,
  48. });
  49. @override
  50. Widget build(BuildContext context, WidgetRef ref) {
  51. final isNewMessageBuilder = useState<bool>(false);
  52. Widget Function(BuildContext context)? _newMessageBuilder;
  53. useEffect((){
  54. Log.d("messageBuilder 发生变化");
  55. isNewMessageBuilder.value = true;
  56. _newMessageBuilder = messageBuilder;
  57. return () {
  58. // print('dispose');
  59. };
  60. }, [messageBuilder]);
  61. //使用一个 Column 为最外层容器,可以方便的视线 wrap_content 的效果
  62. return SizedBox(
  63. width: dialogWidth?? MediaQuery.of(context).size.width * 0.65,
  64. child: Column(
  65. crossAxisAlignment: CrossAxisAlignment.center,
  66. mainAxisAlignment: MainAxisAlignment.center,
  67. mainAxisSize: MainAxisSize.max,
  68. children: [
  69. //Title (如果使用 Container 为最外层容器则默认为 match_parent 的效果,除非我们限制宽度和最大高度最小高度)
  70. Container(
  71. width: double.infinity,
  72. height: 55,
  73. decoration: BoxDecoration(
  74. color: context.appColors.btnBgDefault,
  75. borderRadius: const BorderRadius.only(
  76. topRight: Radius.circular(15),
  77. topLeft: Radius.circular(15),
  78. ),
  79. ),
  80. child: Row(
  81. children: [
  82. const SizedBox(width: 45),
  83. MyTextView(
  84. title ?? S.current.alert,
  85. fontSize: 18,
  86. textAlign: TextAlign.center,
  87. isFontMedium: true,
  88. textColor: Colors.white,
  89. ).expanded(),
  90. const MyAssetImage(Assets.baseServiceDialogDeleteIcon,width: 25,height: 25.5,).onTap((){
  91. onCancel();
  92. },padding: 10)
  93. ],
  94. ),
  95. ),
  96. Container(
  97. width: double.infinity,
  98. padding: const EdgeInsets.only(top: 30),
  99. decoration: BoxDecoration(
  100. color: context.appColors.whiteSecondBG,
  101. borderRadius: const BorderRadius.only(
  102. bottomLeft: Radius.circular(15),
  103. bottomRight: Radius.circular(15),
  104. ),
  105. ),
  106. child:
  107. // 动态的 部分
  108. Column(
  109. children: [
  110. Scrollbar(
  111. child: ScrollConfiguration(
  112. behavior: NoShadowScrollBehavior(),
  113. child: SingleChildScrollView(
  114. child: message.isNotEmpty?MyTextView(
  115. message,
  116. fontSize: 18,
  117. textColor: context.appColors.textBlack,
  118. isFontRegular: true,
  119. textAlign: TextAlign.center,
  120. paddingLeft: 30,
  121. paddingRight: 30,
  122. ): (messageBuilder != null ? (isNewMessageBuilder.value? _newMessageBuilder!(context): messageBuilder!(context)) : Container()),
  123. ),
  124. ),
  125. ).constrained(maxHeight: contentBoxMaxHeight?? 300),
  126. Row(
  127. children: [
  128. const SizedBox(width: 18),
  129. Visibility(
  130. visible: isShowCancelBtn,
  131. child: Expanded(
  132. flex: 1,
  133. child: InkWell(
  134. onTap: () {
  135. onCancel();
  136. cancelAction?.call();
  137. },
  138. child: MyTextView(
  139. cancelTxt ?? S.current.no,
  140. fontSize: 16,
  141. isFontMedium: true,
  142. paddingTop: 13,
  143. marginRight: 15,
  144. paddingBottom: 13,
  145. textAlign: TextAlign.center,
  146. textColor: Colors.white,
  147. backgroundColor: context.appColors.orangeBG,
  148. cornerRadius: 7,
  149. ),
  150. )),
  151. ),
  152. Visibility(
  153. visible: isShowConfirmBtn,
  154. child: Expanded(
  155. flex: 1,
  156. child: InkWell(
  157. onTap: () async {
  158. onCancel();
  159. confirmAction();
  160. },
  161. child: MyTextView(
  162. confirmTxt ?? S.current.yes,
  163. fontSize: 16,
  164. paddingTop: 13,
  165. paddingBottom: 13,
  166. isFontMedium: true,
  167. textAlign: TextAlign.center,
  168. textColor: Colors.white,
  169. backgroundColor: context.appColors.btnBgDefault,
  170. cornerRadius: 7,
  171. ),
  172. )),
  173. ),
  174. const SizedBox(width: 18),
  175. ],
  176. ).marginOnly(bottom: 30, top: 28),
  177. ],
  178. ),
  179. ),
  180. ],
  181. ).constrained(width: 300),
  182. );
  183. }
  184. //取消弹框
  185. void onCancel() async {
  186. SmartDialog.dismiss();
  187. }
  188. }