select_guard_time_dialog.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  5. import 'package:location/utils/common_expand.dart';
  6. import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';
  7. import 'package:location/utils/toast_util.dart';
  8. import '../resource/colors.gen.dart';
  9. import '../resource/string.gen.dart';
  10. typedef DateSureCallback = void Function(DateTime startDate, DateTime endDate);
  11. class SelectGuardTimeDialog {
  12. static const String _tag = 'SelectGuardTimeDialog';
  13. static void show({
  14. DateTime? startDate,
  15. DateTime? endDate,
  16. required DateSureCallback sureCallback,
  17. }) {
  18. SmartDialog.show(
  19. tag: _tag,
  20. animationType: SmartAnimationType.centerFade_otherSlide,
  21. alignment: Alignment.bottomCenter,
  22. keepSingle: true,
  23. builder: (_) {
  24. final now = DateTime.now();
  25. return _SelectGuardTimeView(
  26. startDate ?? now, endDate ?? now, sureCallback);
  27. });
  28. }
  29. static void hide() {
  30. SmartDialog.dismiss(tag: _tag);
  31. }
  32. }
  33. class _SelectGuardTimeView extends StatelessWidget {
  34. DateTime startDate;
  35. DateTime endDate;
  36. final DateSureCallback sureCallback;
  37. _SelectGuardTimeView(this.startDate, this.endDate, this.sureCallback);
  38. @override
  39. Widget build(BuildContext context) {
  40. return IntrinsicHeight(
  41. child: Container(
  42. width: double.infinity,
  43. decoration: BoxDecoration(
  44. borderRadius: BorderRadius.only(
  45. topLeft: Radius.circular(16.w),
  46. topRight: Radius.circular(16.w)),
  47. gradient: LinearGradient(
  48. begin: Alignment.topCenter,
  49. end: Alignment.bottomCenter,
  50. colors: ['#E4E4FF'.color, ColorName.white],
  51. stops: [0.0, 0.3])),
  52. child: Column(
  53. children: [
  54. SizedBox(height: 24.w),
  55. buildTimeHeaderView(),
  56. SizedBox(height: 6.w),
  57. buildSelectDateView(),
  58. SizedBox(height: 6.w),
  59. buildSelectBtnView(),
  60. SizedBox(height: 12.w),
  61. ],
  62. ),
  63. ),
  64. );
  65. }
  66. Widget buildSelectBtnView() {
  67. return Row(
  68. children: [
  69. SizedBox(width: 16.w),
  70. Expanded(
  71. child: GestureDetector(
  72. onTap: () {
  73. SelectGuardTimeDialog.hide();
  74. },
  75. child: Container(
  76. height: 46.w,
  77. decoration: BoxDecoration(
  78. color: ColorName.colorPrimary.withOpacity(0.08),
  79. borderRadius: BorderRadius.circular(100.w)),
  80. child: Center(
  81. child: Text(
  82. StringName.dialogCancel,
  83. style: TextStyle(
  84. fontSize: 14.sp,
  85. color: ColorName.colorPrimary,
  86. fontWeight: FontWeight.bold),
  87. ),
  88. ),
  89. ),
  90. )),
  91. SizedBox(width: 12.w),
  92. Expanded(
  93. child: GestureDetector(
  94. onTap: onSureClick,
  95. child: Container(
  96. height: 46.w,
  97. decoration: BoxDecoration(
  98. color: ColorName.colorPrimary,
  99. borderRadius: BorderRadius.circular(100.w)),
  100. child: Center(
  101. child: Text(
  102. StringName.dialogConfirm,
  103. style: TextStyle(
  104. fontSize: 14.sp,
  105. color: ColorName.white,
  106. fontWeight: FontWeight.bold),
  107. ),
  108. ),
  109. ),
  110. )),
  111. SizedBox(width: 16.w),
  112. ],
  113. );
  114. }
  115. onSureClick() {
  116. //endDate 必须要大于 startDate
  117. if (!endDate.isAfter(startDate)) {
  118. ToastUtil.show(StringName.dialogMustThanStartDate);
  119. return;
  120. }
  121. SelectGuardTimeDialog.hide();
  122. sureCallback.call(startDate, endDate);
  123. }
  124. Widget buildSelectDateView() {
  125. return Stack(
  126. alignment: Alignment.center,
  127. children: [
  128. Container(
  129. width: 336.w,
  130. height: 48.w,
  131. decoration: BoxDecoration(
  132. color: '#F6F6F8'.color,
  133. borderRadius: BorderRadius.all(Radius.circular(14.w))),
  134. child: Center(
  135. child: Text('-',
  136. style: TextStyle(fontSize: 14.sp, color: ColorName.black90)),
  137. ),
  138. ),
  139. Row(
  140. children: [
  141. SizedBox(width: 18.w),
  142. Expanded(
  143. child: DateTimePickerWidget(
  144. initDateTime: startDate,
  145. onChange: (dateTime, _) {
  146. startDate = dateTime;
  147. },
  148. dateFormat: 'HH时 mm分',
  149. locale: DateTimePickerLocale.zh_cn,
  150. pickerTheme: DateTimePickerTheme(
  151. itemHeight: 48.w,
  152. pickerHeight: 250.w,
  153. itemTextStyle: TextStyle(
  154. fontSize: 16.sp,
  155. color: ColorName.black90,
  156. fontWeight: FontWeight.normal),
  157. backgroundColor: ColorName.transparent,
  158. showTitle: false,
  159. selectionOverlay: SizedBox.shrink()),
  160. )),
  161. SizedBox(width: 18.w),
  162. Expanded(
  163. child: DateTimePickerWidget(
  164. initDateTime: endDate,
  165. onChange: (dateTime, _) {
  166. endDate = dateTime;
  167. },
  168. dateFormat: 'HH时 mm分',
  169. locale: DateTimePickerLocale.zh_cn,
  170. pickerTheme: DateTimePickerTheme(
  171. itemHeight: 48.w,
  172. pickerHeight: 250.w,
  173. itemTextStyle: TextStyle(
  174. fontSize: 16.sp,
  175. color: ColorName.black90,
  176. fontWeight: FontWeight.normal),
  177. backgroundColor: ColorName.transparent,
  178. showTitle: false,
  179. selectionOverlay: SizedBox.shrink()),
  180. )),
  181. SizedBox(width: 18.w),
  182. ],
  183. ),
  184. ],
  185. );
  186. }
  187. Widget buildTimeHeaderView() {
  188. return Row(
  189. children: [
  190. Expanded(
  191. child: Center(
  192. child: Text(
  193. StringName.commonPointSelectTimeStart,
  194. style: TextStyle(fontSize: 14.sp, color: ColorName.black60),
  195. ),
  196. ),
  197. ),
  198. Text('-', style: TextStyle(fontSize: 14.sp, color: ColorName.black30)),
  199. Expanded(
  200. child: Center(
  201. child: Text(
  202. StringName.commonPointSelectTimeEnd,
  203. style: TextStyle(fontSize: 14.sp, color: ColorName.black60),
  204. ),
  205. ),
  206. ),
  207. ],
  208. );
  209. }
  210. }