utils.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import 'dart:convert';
  2. //import 'package:barcode_scan/barcode_scan.dart';
  3. import 'package:crypto/crypto.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:liftmanager/utils/theme_utils.dart';
  7. import 'package:liftmanager/utils/toast.dart';
  8. import 'package:keyboard_actions/keyboard_actions.dart';
  9. import 'package:url_launcher/url_launcher.dart';
  10. import "package:intl/intl.dart";
  11. class Utils {
  12. // md5 加密
  13. static String generateMd5(String str) {
  14. return md5.convert(utf8.encode(str)).toString();
  15. }
  16. /// 调起拨号页
  17. static void launchTelURL(String phone) async {
  18. String url = 'tel:'+ phone;
  19. if (await canLaunch(url)) {
  20. await launch(url);
  21. } else {
  22. toasts('拨号失败!');
  23. }
  24. }
  25. /// 调起二维码扫描页
  26. // static Future<String> scan() async {
  27. // try {
  28. // return await BarcodeScanner.scan();
  29. // } catch (e) {
  30. // if (e is PlatformException){
  31. // if (e.code == BarcodeScanner.CameraAccessDenied) {
  32. // toasts("没有相机权限!");
  33. // }
  34. // }
  35. // }
  36. // return null;
  37. // }
  38. static KeyboardActionsConfig getKeyboardActionsConfig(BuildContext context, List<FocusNode> list){
  39. return KeyboardActionsConfig(
  40. keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
  41. keyboardBarColor: ThemeUtils.getKeyboardActionsColor(context),
  42. nextFocus: true,
  43. actions: List.generate(list.length, (i) => KeyboardAction(
  44. focusNode: list[i],
  45. closeWidget: const Padding(
  46. padding: const EdgeInsets.all(5.0),
  47. child: const Text("关闭"),
  48. ),
  49. )),
  50. );
  51. }
  52. static final DateFormat _monthFormat = new DateFormat("yyyy年MM月");
  53. static final DateFormat _dayFormat = new DateFormat("dd");
  54. static final DateFormat _firstDayFormat = new DateFormat("MMM dd");
  55. static final DateFormat _fullDayFormat = new DateFormat("EEE MMM dd, yyyy");
  56. static final DateFormat _apiDayFormat = new DateFormat("yyyy-MM-dd");
  57. static String formatMonth(DateTime d) => _monthFormat.format(d);
  58. static String formatDay(DateTime d) => _dayFormat.format(d);
  59. static String formatFirstDay(DateTime d) => _firstDayFormat.format(d);
  60. static String fullDayFormat(DateTime d) => _fullDayFormat.format(d);
  61. static String apiDayFormat(DateTime d) => _apiDayFormat.format(d);
  62. static const List<String> weekdays = const [
  63. "日",
  64. "一",
  65. "二",
  66. "三",
  67. "四",
  68. "五",
  69. "六"
  70. ];
  71. static List<DateTime> daysInMonth(DateTime month) {
  72. var first = firstDayOfMonth(month);
  73. var daysBefore = first.weekday;
  74. var firstToDisplay = first.subtract(new Duration(days: daysBefore));
  75. var last = Utils.lastDayOfMonth(month);
  76. var daysAfter = 7 - last.weekday;
  77. // If the last day is sunday (7) the entire week must be rendered
  78. if (daysAfter == 0) {
  79. daysAfter = 7;
  80. }
  81. var lastToDisplay = last.add(new Duration(days: daysAfter));
  82. return daysInRange(firstToDisplay, lastToDisplay).toList();
  83. }
  84. static bool isFirstDayOfMonth(DateTime day) {
  85. return isSameDay(firstDayOfMonth(day), day);
  86. }
  87. static bool isLastDayOfMonth(DateTime day) {
  88. return isSameDay(lastDayOfMonth(day), day);
  89. }
  90. static DateTime firstDayOfMonth(DateTime month) {
  91. return new DateTime(month.year, month.month);
  92. }
  93. static DateTime firstDayOfWeek(DateTime day) {
  94. /// Handle Daylight Savings by setting hour to 12:00 Noon
  95. /// rather than the default of Midnight
  96. day = new DateTime.utc(day.year, day.month, day.day, 12);
  97. /// Weekday is on a 1-7 scale Monday - Sunday,
  98. /// This Calendar works from Sunday - Monday
  99. var decreaseNum = day.weekday % 7;
  100. return day.subtract(new Duration(days: decreaseNum));
  101. }
  102. static DateTime lastDayOfWeek(DateTime day) {
  103. /// Handle Daylight Savings by setting hour to 12:00 Noon
  104. /// rather than the default of Midnight
  105. day = new DateTime.utc(day.year, day.month, day.day, 12);
  106. /// Weekday is on a 1-7 scale Monday - Sunday,
  107. /// This Calendar's Week starts on Sunday
  108. var increaseNum = day.weekday % 7;
  109. return day.add(new Duration(days: 7 - increaseNum));
  110. }
  111. /// The last day of a given month
  112. static DateTime lastDayOfMonth(DateTime month) {
  113. var beginningNextMonth = (month.month < 12)
  114. ? new DateTime(month.year, month.month + 1, 1)
  115. : new DateTime(month.year + 1, 1, 1);
  116. return beginningNextMonth.subtract(new Duration(days: 1));
  117. }
  118. /// Returns a [DateTime] for each day the given range.
  119. ///
  120. /// [start] inclusive
  121. /// [end] exclusive
  122. static Iterable<DateTime> daysInRange(DateTime start, DateTime end) sync* {
  123. var i = start;
  124. var offset = start.timeZoneOffset;
  125. while (i.isBefore(end)) {
  126. yield i;
  127. i = i.add(new Duration(days: 1));
  128. var timeZoneDiff = i.timeZoneOffset - offset;
  129. if (timeZoneDiff.inSeconds != 0) {
  130. offset = i.timeZoneOffset;
  131. i = i.subtract(new Duration(seconds: timeZoneDiff.inSeconds));
  132. }
  133. }
  134. }
  135. /// Whether or not two times are on the same day.
  136. static bool isSameDay(DateTime a, DateTime b) {
  137. return a.year == b.year && a.month == b.month && a.day == b.day;
  138. }
  139. static bool isSameWeek(DateTime a, DateTime b) {
  140. /// Handle Daylight Savings by setting hour to 12:00 Noon
  141. /// rather than the default of Midnight
  142. a = new DateTime.utc(a.year, a.month, a.day);
  143. b = new DateTime.utc(b.year, b.month, b.day);
  144. var diff = a.toUtc().difference(b.toUtc()).inDays;
  145. if (diff.abs() >= 7) {
  146. return false;
  147. }
  148. var min = a.isBefore(b) ? a : b;
  149. var max = a.isBefore(b) ? b : a;
  150. var result = max.weekday % 7 - min.weekday % 7 >= 0;
  151. return result;
  152. }
  153. static DateTime previousMonth(DateTime m) {
  154. var year = m.year;
  155. var month = m.month;
  156. if (month == 1) {
  157. year--;
  158. month = 12;
  159. } else {
  160. month--;
  161. }
  162. return new DateTime(year, month);
  163. }
  164. static DateTime nextMonth(DateTime m) {
  165. var year = m.year;
  166. var month = m.month;
  167. if (month == 12) {
  168. year++;
  169. month = 1;
  170. } else {
  171. month++;
  172. }
  173. return new DateTime(year, month);
  174. }
  175. static DateTime previousWeek(DateTime w) {
  176. return w.subtract(new Duration(days: 7));
  177. }
  178. static DateTime nextWeek(DateTime w) {
  179. return w.add(new Duration(days: 7));
  180. }
  181. }
  182. /// 默认dialog背景色为半透明黑色,这里修改源码改为透明
  183. Future<T> showTransparentDialog<T>({
  184. @required BuildContext context,
  185. bool barrierDismissible = true,
  186. WidgetBuilder builder,
  187. }) {
  188. final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  189. return showGeneralDialog(
  190. context: context,
  191. pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
  192. final Widget pageChild = Builder(builder: builder);
  193. return SafeArea(
  194. child: Builder(
  195. builder: (BuildContext context) {
  196. return theme != null
  197. ? Theme(data: theme, child: pageChild)
  198. : pageChild;
  199. }
  200. ),
  201. );
  202. },
  203. barrierDismissible: barrierDismissible,
  204. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  205. barrierColor: const Color(0x00FFFFFF),
  206. transitionDuration: const Duration(milliseconds: 150),
  207. transitionBuilder: _buildMaterialDialogTransitions,
  208. );
  209. }
  210. Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
  211. return FadeTransition(
  212. opacity: CurvedAnimation(
  213. parent: animation,
  214. curve: Curves.easeOut,
  215. ),
  216. child: child,
  217. );
  218. }
  219. Future<T> showElasticDialog<T>({
  220. @required BuildContext context,
  221. bool barrierDismissible = true,
  222. WidgetBuilder builder,
  223. }) {
  224. final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  225. return showGeneralDialog(
  226. context: context,
  227. pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
  228. final Widget pageChild = Builder(builder: builder);
  229. return SafeArea(
  230. child: Builder(
  231. builder: (BuildContext context) {
  232. return theme != null
  233. ? Theme(data: theme, child: pageChild)
  234. : pageChild;
  235. }
  236. ),
  237. );
  238. },
  239. barrierDismissible: barrierDismissible,
  240. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  241. barrierColor: Colors.black54,
  242. transitionDuration: const Duration(milliseconds: 550),
  243. transitionBuilder: _buildDialogTransitions,
  244. );
  245. }
  246. Widget _buildDialogTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
  247. return FadeTransition(
  248. opacity: CurvedAnimation(
  249. parent: animation,
  250. curve: Curves.easeOut,
  251. ),
  252. child: SlideTransition(
  253. position: Tween<Offset>(
  254. begin: const Offset(0.0, 0.3),
  255. end: Offset.zero
  256. ).animate(CurvedAnimation(
  257. parent: animation,
  258. curve: animation.status != AnimationStatus.forward ? Curves.easeOutBack: ElasticOutCurve(0.85),
  259. )),
  260. child: child,
  261. ),
  262. );
  263. }