utils.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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:intl/intl.dart";
  6. import 'package:keyboard_actions/keyboard_actions.dart';
  7. import 'package:liftmanager/common/user_db.dart';
  8. import 'package:liftmanager/internal/account/model/user_entity.dart';
  9. import 'package:liftmanager/internal/wode/wode_router.dart';
  10. import 'package:liftmanager/routers/fluro_navigator.dart';
  11. import 'package:liftmanager/utils/theme_utils.dart';
  12. import 'package:liftmanager/utils/toast.dart';
  13. import 'package:liftmanager/utils/url.dart';
  14. import 'package:url_launcher/url_launcher.dart';
  15. class Utils {
  16. // 根据关键字获取当前用户角色是否有该功能
  17. static bool getAuthByRouter(context, String router,
  18. [bool isAbnormal = true]) {
  19. List<AppMenus> userAuth = User().getUserAuth();
  20. bool flag = false;
  21. for (int i = 0; i < userAuth.length; i++) {
  22. print(userAuth[i].router);
  23. if (userAuth[i].router == router) {
  24. flag = true;
  25. break;
  26. }
  27. }
  28. // return true;
  29. if (flag) {
  30. return true;
  31. } else {
  32. if (isAbnormal) {
  33. // toasts('暂无权限,请先开通会员!');
  34. showAlert(
  35. context,
  36. "提示",
  37. "暂无权限,请先开通会员!",
  38. "确定",
  39. () {
  40. NavigatorUtils.goBack(context);
  41. NavigatorUtils.push(context, "${WodeRouter.vip}?id=");
  42. },
  43. txt2: "取消",
  44. onPre2: () {
  45. NavigatorUtils.goBack(context);
  46. },
  47. );
  48. }
  49. return false;
  50. }
  51. }
  52. // 图片异常处理
  53. static String getImagePath(String url, {bool isWater = false}) {
  54. RegExp exp = RegExp(r"(http|https):\/\/([\w.]+\/?)\S*");
  55. String endImg =
  56. "?x-oss-process=image/watermark,size_60,t_40,g_center,rotate_30,color_FFFFFF,text_5Zu-54mH5p2l5rqQIOKAnOeUteair-euoeWutuKAnQ==";
  57. if (url == null || url == "") {
  58. return "";
  59. } else {
  60. if (exp.hasMatch(url)) {
  61. if (isWater) {
  62. return url + endImg;
  63. } else {
  64. return url;
  65. }
  66. } else {
  67. if (isWater) {
  68. return imgFontUrl + url + endImg;
  69. } else {
  70. return imgFontUrl + url;
  71. }
  72. }
  73. }
  74. }
  75. // md5 加密
  76. static String generateMd5(String str) {
  77. return md5.convert(utf8.encode(str)).toString();
  78. }
  79. /// 调起拨号页
  80. static void launchTelURL(context, String phone) async {
  81. String url = 'tel:' + phone;
  82. if (await canLaunch(url)) {
  83. await launch(url);
  84. } else {
  85. toasts('拨号失败!');
  86. }
  87. }
  88. /// 调起二维码扫描页
  89. // static Future<String> scan(context) async {
  90. // try {
  91. // return await BarcodeScanner.scan();
  92. // } catch (e) {
  93. // if (e is PlatformException) {
  94. // if (e.code == BarcodeScanner.CameraAccessDenied) {
  95. // toasts("没有相机权限!");
  96. // }
  97. // }
  98. // }
  99. // return null;
  100. // }
  101. static KeyboardActionsConfig getKeyboardActionsConfig(
  102. BuildContext context, List<FocusNode> list) {
  103. return KeyboardActionsConfig(
  104. keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
  105. keyboardBarColor: ThemeUtils.getKeyboardActionsColor(context),
  106. nextFocus: true,
  107. actions: List.generate(
  108. list.length,
  109. (i) => KeyboardAction(
  110. focusNode: list[i],
  111. closeWidget: const Padding(
  112. padding: const EdgeInsets.all(5.0),
  113. child: const Text("关闭"),
  114. ),
  115. )),
  116. );
  117. }
  118. static String getFileType(String fileName) {
  119. fileName = fileName.toLowerCase();
  120. var index1 = fileName.lastIndexOf(".");
  121. var index2 = fileName.length;
  122. var fileType = fileName.substring(index1, index2);
  123. switch (fileType) {
  124. case ".jpg":
  125. case ".jpeg":
  126. case ".jpe":
  127. case ".png":
  128. case ".bmp":
  129. case ".gif":
  130. return "image";
  131. case ".pdf":
  132. return "pdf";
  133. case ".json":
  134. return "json";
  135. case ".svg":
  136. case ".svgz":
  137. return "svg";
  138. case ".mp3":
  139. return "audio";
  140. case ".mp4":
  141. return "video";
  142. case ".mov":
  143. return "video";
  144. case ".htm":
  145. case ".html":
  146. return "html";
  147. case ".css":
  148. return "css";
  149. case ".csv":
  150. return "csv";
  151. case ".txt":
  152. case ".text":
  153. case ".conf":
  154. case ".def":
  155. case ".log":
  156. case ".in":
  157. return "plain";
  158. }
  159. return null;
  160. }
  161. static final DateFormat _monthFormat = new DateFormat("yyyy年MM月");
  162. static final DateFormat _dayFormat = new DateFormat("dd");
  163. static final DateFormat _firstDayFormat = new DateFormat("MMM dd");
  164. static final DateFormat _fullDayFormat = new DateFormat("EEE MMM dd, yyyy");
  165. static final DateFormat _apiDayFormat = new DateFormat("yyyy-MM-dd");
  166. static String formatMonth(DateTime d) => _monthFormat.format(d);
  167. static String formatDay(DateTime d) => _dayFormat.format(d);
  168. static String formatFirstDay(DateTime d) => _firstDayFormat.format(d);
  169. static String fullDayFormat(DateTime d) => _fullDayFormat.format(d);
  170. static String apiDayFormat(DateTime d) => _apiDayFormat.format(d);
  171. static const List<String> weekdays = const [
  172. "日",
  173. "一",
  174. "二",
  175. "三",
  176. "四",
  177. "五",
  178. "六"
  179. ];
  180. static List<DateTime> daysInMonth(DateTime month) {
  181. var first = firstDayOfMonth(month);
  182. var daysBefore = first.weekday;
  183. var firstToDisplay = first.subtract(new Duration(days: daysBefore));
  184. var last = Utils.lastDayOfMonth(month);
  185. var daysAfter = 7 - last.weekday;
  186. // If the last day is sunday (7) the entire week must be rendered
  187. if (daysAfter == 0) {
  188. daysAfter = 7;
  189. }
  190. var lastToDisplay = last.add(new Duration(days: daysAfter));
  191. return daysInRange(firstToDisplay, lastToDisplay).toList();
  192. }
  193. static bool isFirstDayOfMonth(DateTime day) {
  194. return isSameDay(firstDayOfMonth(day), day);
  195. }
  196. static bool isLastDayOfMonth(DateTime day) {
  197. return isSameDay(lastDayOfMonth(day), day);
  198. }
  199. static DateTime firstDayOfMonth(DateTime month) {
  200. return new DateTime(month.year, month.month);
  201. }
  202. static DateTime firstDayOfWeek(DateTime day) {
  203. /// Handle Daylight Savings by setting hour to 12:00 Noon
  204. /// rather than the default of Midnight
  205. day = new DateTime.utc(day.year, day.month, day.day, 12);
  206. /// Weekday is on a 1-7 scale Monday - Sunday,
  207. /// This Calendar works from Sunday - Monday
  208. var decreaseNum = day.weekday % 7;
  209. return day.subtract(new Duration(days: decreaseNum));
  210. }
  211. static DateTime lastDayOfWeek(DateTime day) {
  212. /// Handle Daylight Savings by setting hour to 12:00 Noon
  213. /// rather than the default of Midnight
  214. day = new DateTime.utc(day.year, day.month, day.day, 12);
  215. /// Weekday is on a 1-7 scale Monday - Sunday,
  216. /// This Calendar's Week starts on Sunday
  217. var increaseNum = day.weekday % 7;
  218. return day.add(new Duration(days: 7 - increaseNum));
  219. }
  220. /// The last day of a given month
  221. static DateTime lastDayOfMonth(DateTime month) {
  222. var beginningNextMonth = (month.month < 12)
  223. ? new DateTime(month.year, month.month + 1, 1)
  224. : new DateTime(month.year + 1, 1, 1);
  225. return beginningNextMonth.subtract(new Duration(days: 1));
  226. }
  227. /// Returns a [DateTime] for each day the given range.
  228. ///
  229. /// [start] inclusive
  230. /// [end] exclusive
  231. static Iterable<DateTime> daysInRange(DateTime start, DateTime end) sync* {
  232. var i = start;
  233. var offset = start.timeZoneOffset;
  234. while (i.isBefore(end)) {
  235. yield i;
  236. i = i.add(new Duration(days: 1));
  237. var timeZoneDiff = i.timeZoneOffset - offset;
  238. if (timeZoneDiff.inSeconds != 0) {
  239. offset = i.timeZoneOffset;
  240. i = i.subtract(new Duration(seconds: timeZoneDiff.inSeconds));
  241. }
  242. }
  243. }
  244. /// Whether or not two times are on the same day.
  245. static bool isSameDay(DateTime a, DateTime b) {
  246. return a.year == b.year && a.month == b.month && a.day == b.day;
  247. }
  248. static bool isSameWeek(DateTime a, DateTime b) {
  249. /// Handle Daylight Savings by setting hour to 12:00 Noon
  250. /// rather than the default of Midnight
  251. a = new DateTime.utc(a.year, a.month, a.day);
  252. b = new DateTime.utc(b.year, b.month, b.day);
  253. var diff = a.toUtc().difference(b.toUtc()).inDays;
  254. if (diff.abs() >= 7) {
  255. return false;
  256. }
  257. var min = a.isBefore(b) ? a : b;
  258. var max = a.isBefore(b) ? b : a;
  259. var result = max.weekday % 7 - min.weekday % 7 >= 0;
  260. return result;
  261. }
  262. static DateTime previousMonth(DateTime m) {
  263. var year = m.year;
  264. var month = m.month;
  265. if (month == 1) {
  266. year--;
  267. month = 12;
  268. } else {
  269. month--;
  270. }
  271. return new DateTime(year, month);
  272. }
  273. static DateTime nextMonth(DateTime m) {
  274. var year = m.year;
  275. var month = m.month;
  276. if (month == 12) {
  277. year++;
  278. month = 1;
  279. } else {
  280. month++;
  281. }
  282. return new DateTime(year, month);
  283. }
  284. static DateTime previousWeek(DateTime w) {
  285. return w.subtract(new Duration(days: 7));
  286. }
  287. static DateTime nextWeek(DateTime w) {
  288. return w.add(new Duration(days: 7));
  289. }
  290. }
  291. /// 默认dialog背景色为半透明黑色,这里修改源码改为透明
  292. Future<T> showTransparentDialog<T>({
  293. @required BuildContext context,
  294. bool barrierDismissible = true,
  295. WidgetBuilder builder,
  296. }) {
  297. final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  298. return showGeneralDialog(
  299. context: context,
  300. pageBuilder: (BuildContext buildContext, Animation<double> animation,
  301. Animation<double> secondaryAnimation) {
  302. final Widget pageChild = Builder(builder: builder);
  303. return SafeArea(
  304. child: Builder(builder: (BuildContext context) {
  305. return theme != null
  306. ? Theme(data: theme, child: pageChild)
  307. : pageChild;
  308. }),
  309. );
  310. },
  311. barrierDismissible: barrierDismissible,
  312. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  313. barrierColor: const Color(0x00FFFFFF),
  314. transitionDuration: const Duration(milliseconds: 150),
  315. transitionBuilder: _buildMaterialDialogTransitions,
  316. );
  317. }
  318. Widget _buildMaterialDialogTransitions(
  319. BuildContext context,
  320. Animation<double> animation,
  321. Animation<double> secondaryAnimation,
  322. Widget child) {
  323. return FadeTransition(
  324. opacity: CurvedAnimation(
  325. parent: animation,
  326. curve: Curves.easeOut,
  327. ),
  328. child: child,
  329. );
  330. }
  331. Future<T> showElasticDialog<T>({
  332. @required BuildContext context,
  333. bool barrierDismissible = true,
  334. WidgetBuilder builder,
  335. }) {
  336. final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  337. return showGeneralDialog(
  338. context: context,
  339. pageBuilder: (BuildContext buildContext, Animation<double> animation,
  340. Animation<double> secondaryAnimation) {
  341. final Widget pageChild = Builder(builder: builder);
  342. return SafeArea(
  343. child: Builder(builder: (BuildContext context) {
  344. return theme != null
  345. ? Theme(data: theme, child: pageChild)
  346. : pageChild;
  347. }),
  348. );
  349. },
  350. barrierDismissible: barrierDismissible,
  351. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  352. barrierColor: Colors.black54,
  353. transitionDuration: const Duration(milliseconds: 550),
  354. transitionBuilder: _buildDialogTransitions,
  355. );
  356. }
  357. Widget _buildDialogTransitions(
  358. BuildContext context,
  359. Animation<double> animation,
  360. Animation<double> secondaryAnimation,
  361. Widget child) {
  362. return FadeTransition(
  363. opacity: CurvedAnimation(
  364. parent: animation,
  365. curve: Curves.easeOut,
  366. ),
  367. child: SlideTransition(
  368. position: Tween<Offset>(begin: const Offset(0.0, 0.3), end: Offset.zero)
  369. .animate(CurvedAnimation(
  370. parent: animation,
  371. curve: animation.status != AnimationStatus.forward
  372. ? Curves.easeOutBack
  373. : ElasticOutCurve(0.85),
  374. )),
  375. child: child,
  376. ),
  377. );
  378. }
  379. // Color string2Color(String colorString){
  380. // int value = 0x00000000;
  381. // if(isNotEmpty(colorString)){
  382. // if(colorString[0] == "#"){
  383. // colorString = colorString.substring(1);
  384. // }
  385. // value = int.tryParse(colorString,raidx:16);
  386. // if(value != null){
  387. // if(value < 0xFF000000){
  388. // value += 0xFF000000;
  389. // }
  390. // }
  391. // }
  392. // return Color(value);
  393. // }
  394. // 切大半圆
  395. class BottomClipper extends CustomClipper<Path> {
  396. double clipheight;
  397. BottomClipper({this.clipheight=50});
  398. @override
  399. Path getClip(Size size) {
  400. var path = Path();
  401. path.lineTo(0, 0); //第1个点
  402. path.lineTo(0, size.height - clipheight); //第2个点
  403. var firstControlPoint = Offset(size.width / 2, size.height);
  404. var firstEdnPoint = Offset(size.width, size.height - clipheight);
  405. path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy,
  406. firstEdnPoint.dx, firstEdnPoint.dy);
  407. path.lineTo(size.width, size.height - clipheight); //第3个点
  408. path.lineTo(size.width, 0); //第4个点
  409. return path;
  410. }
  411. @override
  412. bool shouldReclip(CustomClipper<Path> oldClipper) {
  413. return false;
  414. }
  415. }