modal.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * @Author: XianKaiQun
  3. * @Date: 2020-09-08 16:50:55
  4. * @LastEditors : WuWei
  5. * @LastEditTime : 2023-08-18 15:03:22
  6. * @Description:
  7. */
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter/widgets.dart';
  10. import 'package:wisdom_cli/wisdom_cli.dart';
  11. ///[builder] 构造视图
  12. ///[popTrigger] 触发弹窗关闭,传入回调的参数,
  13. ///使用了[WillPopScope],必须通过此函数才能关闭弹窗
  14. typedef WModalBuider<T> = Widget Function(
  15. BuildContext context,
  16. void Function(T argument) popTrigger,
  17. );
  18. ///Modal弹窗
  19. class WModalUtil {
  20. WModalUtil._();
  21. ///需要自己构造Modal视图,包括朦胧层。
  22. ///
  23. ///[builder] 构造视图,
  24. ///[popTrigger] 触发弹窗关闭,传入回调的参数,
  25. ///使用了[WillPopScope],必须通过此函数才能关闭弹窗
  26. static Future<T?> custom<T>({
  27. WModalBuider? builder,
  28. }) async {
  29. bool willPop = false;
  30. void popTrigger<T>(T argument) {
  31. willPop = true;
  32. WNavUtil.instance!.pop<T>(argument);
  33. }
  34. return await WNavUtil.instance!.push<T>(
  35. PageRouteBuilder<T>(
  36. opaque: false,
  37. transitionDuration: const Duration(milliseconds: 150),
  38. pageBuilder: (context, animation, animation2) {
  39. return WillPopScope(
  40. onWillPop: () async => willPop,
  41. child: builder!(context, popTrigger),
  42. );
  43. },
  44. ),
  45. );
  46. }
  47. ///显示modal弹窗
  48. ///
  49. ///路由关闭为同步事件,
  50. ///构造内容中均为[Widget],
  51. ///用此类函数可以添加一些自定义的图标、自定义文本样式等
  52. static Future<bool?> showFormWidgetSync({
  53. bool? canCancel = true,
  54. Widget? title,
  55. Widget? content,
  56. Widget? cancelText = const Text('取消'),
  57. Widget? confirmText = const Text('确认'),
  58. }) {
  59. canCancel ??= false;
  60. cancelText ??= const Text('取消');
  61. confirmText ??= const Text('确认');
  62. return custom(
  63. builder: (context, popTrigger) {
  64. final colorScheme = WTheme.of(context).colorScheme;
  65. void onConfirm() => popTrigger(true);
  66. void onCancel() => popTrigger(false);
  67. return WModal(
  68. header: title,
  69. content: content,
  70. footers: [
  71. if (canCancel == true)
  72. WButton(
  73. height: 44.pt,
  74. color: colorScheme.background,
  75. textColor: colorScheme.text,
  76. onPressed: onCancel,
  77. child: cancelText,
  78. shape: RoundedRectangleBorder(
  79. borderRadius: BorderRadius.circular(24.5.pt),
  80. ),
  81. ).nestExpanded(),
  82. if (canCancel == true) SizedBox(width: 15.pt),
  83. WButton(
  84. height: 44.pt,
  85. color: Color(0XFF0F67F8),
  86. onPressed: onConfirm,
  87. child: confirmText,
  88. shape: RoundedRectangleBorder(
  89. borderRadius: BorderRadius.circular(24.5.pt),
  90. ),
  91. ).nestExpanded()
  92. ],
  93. );
  94. },
  95. );
  96. }
  97. ///快捷显示modal弹窗
  98. ///
  99. ///路由关闭为同步事件,
  100. ///相比[showFormWidgetSync],构造参数均为[String]类型
  101. static Future<bool?> showSync(
  102. String content, {
  103. String? title,
  104. bool canCancel = true,
  105. String cancelText = '取消',
  106. String confirmText = '确认',
  107. }) {
  108. return showFormWidgetSync(
  109. canCancel: canCancel,
  110. content: WisText('$content'),
  111. title: title != null ? Text('$title') : null,
  112. cancelText: WisText(cancelText),
  113. confirmText: WisText(confirmText),
  114. );
  115. }
  116. }