select_guard_time_dialog.dart 6.6 KB

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