123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- import 'dart:convert';
- // import 'package:barcode_scan/barcode_scan.dart';
- import 'package:crypto/crypto.dart';
- import 'package:flutter/material.dart';
- import "package:intl/intl.dart";
- import 'package:keyboard_actions/keyboard_actions.dart';
- import 'package:liftmanager/common/user_db.dart';
- import 'package:liftmanager/internal/account/model/user_entity.dart';
- import 'package:liftmanager/internal/wode/wode_router.dart';
- import 'package:liftmanager/routers/fluro_navigator.dart';
- import 'package:liftmanager/utils/theme_utils.dart';
- import 'package:liftmanager/utils/toast.dart';
- import 'package:liftmanager/utils/url.dart';
- import 'package:url_launcher/url_launcher.dart';
- class Utils {
- // 根据关键字获取当前用户角色是否有该功能
- static bool getAuthByRouter(context, String router,
- [bool isAbnormal = true]) {
- List<AppMenus> userAuth = User().getUserAuth();
- bool flag = false;
- for (int i = 0; i < userAuth.length; i++) {
- print(userAuth[i].router);
- if (userAuth[i].router == router) {
- flag = true;
- break;
- }
- }
- // return true;
- if (flag) {
- return true;
- } else {
- if (isAbnormal) {
- // toasts('暂无权限,请先开通会员!');
- showAlert(
- context,
- "提示",
- "暂无权限,请先开通会员!",
- "确定",
- () {
- NavigatorUtils.goBack(context);
- NavigatorUtils.push(context, "${WodeRouter.vip}?id=");
- },
- txt2: "取消",
- onPre2: () {
- NavigatorUtils.goBack(context);
- },
- );
- }
- return false;
- }
- }
- // 图片异常处理
- static String getImagePath(String url, {bool isWater = false}) {
- RegExp exp = RegExp(r"(http|https):\/\/([\w.]+\/?)\S*");
- String endImg =
- "?x-oss-process=image/watermark,size_60,t_40,g_center,rotate_30,color_FFFFFF,text_5Zu-54mH5p2l5rqQIOKAnOeUteair-euoeWutuKAnQ==";
- if (url == null || url == "") {
- return "";
- } else {
- if (exp.hasMatch(url)) {
- if (isWater) {
- return url + endImg;
- } else {
- return url;
- }
- } else {
- if (isWater) {
- return imgFontUrl + url + endImg;
- } else {
- return imgFontUrl + url;
- }
- }
- }
- }
- // md5 加密
- static String generateMd5(String str) {
- return md5.convert(utf8.encode(str)).toString();
- }
- /// 调起拨号页
- static void launchTelURL(context, String phone) async {
- String url = 'tel:' + phone;
- if (await canLaunch(url)) {
- await launch(url);
- } else {
- toasts('拨号失败!');
- }
- }
- /// 调起二维码扫描页
- // static Future<String> scan(context) async {
- // try {
- // return await BarcodeScanner.scan();
- // } catch (e) {
- // if (e is PlatformException) {
- // if (e.code == BarcodeScanner.CameraAccessDenied) {
- // toasts("没有相机权限!");
- // }
- // }
- // }
- // return null;
- // }
- static KeyboardActionsConfig getKeyboardActionsConfig(
- BuildContext context, List<FocusNode> list) {
- return KeyboardActionsConfig(
- keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
- keyboardBarColor: ThemeUtils.getKeyboardActionsColor(context),
- nextFocus: true,
- actions: List.generate(
- list.length,
- (i) => KeyboardAction(
- focusNode: list[i],
- closeWidget: const Padding(
- padding: const EdgeInsets.all(5.0),
- child: const Text("关闭"),
- ),
- )),
- );
- }
- static String getFileType(String fileName) {
- fileName = fileName.toLowerCase();
- var index1 = fileName.lastIndexOf(".");
- var index2 = fileName.length;
- var fileType = fileName.substring(index1, index2);
- switch (fileType) {
- case ".jpg":
- case ".jpeg":
- case ".jpe":
- case ".png":
- case ".bmp":
- case ".gif":
- return "image";
- case ".pdf":
- return "pdf";
- case ".json":
- return "json";
- case ".svg":
- case ".svgz":
- return "svg";
- case ".mp3":
- return "audio";
- case ".mp4":
- return "video";
- case ".mov":
- return "video";
- case ".htm":
- case ".html":
- return "html";
- case ".css":
- return "css";
- case ".csv":
- return "csv";
- case ".txt":
- case ".text":
- case ".conf":
- case ".def":
- case ".log":
- case ".in":
- return "plain";
- }
- return null;
- }
- static final DateFormat _monthFormat = new DateFormat("yyyy年MM月");
- static final DateFormat _dayFormat = new DateFormat("dd");
- static final DateFormat _firstDayFormat = new DateFormat("MMM dd");
- static final DateFormat _fullDayFormat = new DateFormat("EEE MMM dd, yyyy");
- static final DateFormat _apiDayFormat = new DateFormat("yyyy-MM-dd");
- static String formatMonth(DateTime d) => _monthFormat.format(d);
- static String formatDay(DateTime d) => _dayFormat.format(d);
- static String formatFirstDay(DateTime d) => _firstDayFormat.format(d);
- static String fullDayFormat(DateTime d) => _fullDayFormat.format(d);
- static String apiDayFormat(DateTime d) => _apiDayFormat.format(d);
- static const List<String> weekdays = const [
- "日",
- "一",
- "二",
- "三",
- "四",
- "五",
- "六"
- ];
- static List<DateTime> daysInMonth(DateTime month) {
- var first = firstDayOfMonth(month);
- var daysBefore = first.weekday;
- var firstToDisplay = first.subtract(new Duration(days: daysBefore));
- var last = Utils.lastDayOfMonth(month);
- var daysAfter = 7 - last.weekday;
- // If the last day is sunday (7) the entire week must be rendered
- if (daysAfter == 0) {
- daysAfter = 7;
- }
- var lastToDisplay = last.add(new Duration(days: daysAfter));
- return daysInRange(firstToDisplay, lastToDisplay).toList();
- }
- static bool isFirstDayOfMonth(DateTime day) {
- return isSameDay(firstDayOfMonth(day), day);
- }
- static bool isLastDayOfMonth(DateTime day) {
- return isSameDay(lastDayOfMonth(day), day);
- }
- static DateTime firstDayOfMonth(DateTime month) {
- return new DateTime(month.year, month.month);
- }
- static DateTime firstDayOfWeek(DateTime day) {
- /// Handle Daylight Savings by setting hour to 12:00 Noon
- /// rather than the default of Midnight
- day = new DateTime.utc(day.year, day.month, day.day, 12);
- /// Weekday is on a 1-7 scale Monday - Sunday,
- /// This Calendar works from Sunday - Monday
- var decreaseNum = day.weekday % 7;
- return day.subtract(new Duration(days: decreaseNum));
- }
- static DateTime lastDayOfWeek(DateTime day) {
- /// Handle Daylight Savings by setting hour to 12:00 Noon
- /// rather than the default of Midnight
- day = new DateTime.utc(day.year, day.month, day.day, 12);
- /// Weekday is on a 1-7 scale Monday - Sunday,
- /// This Calendar's Week starts on Sunday
- var increaseNum = day.weekday % 7;
- return day.add(new Duration(days: 7 - increaseNum));
- }
- /// The last day of a given month
- static DateTime lastDayOfMonth(DateTime month) {
- var beginningNextMonth = (month.month < 12)
- ? new DateTime(month.year, month.month + 1, 1)
- : new DateTime(month.year + 1, 1, 1);
- return beginningNextMonth.subtract(new Duration(days: 1));
- }
- /// Returns a [DateTime] for each day the given range.
- ///
- /// [start] inclusive
- /// [end] exclusive
- static Iterable<DateTime> daysInRange(DateTime start, DateTime end) sync* {
- var i = start;
- var offset = start.timeZoneOffset;
- while (i.isBefore(end)) {
- yield i;
- i = i.add(new Duration(days: 1));
- var timeZoneDiff = i.timeZoneOffset - offset;
- if (timeZoneDiff.inSeconds != 0) {
- offset = i.timeZoneOffset;
- i = i.subtract(new Duration(seconds: timeZoneDiff.inSeconds));
- }
- }
- }
- /// Whether or not two times are on the same day.
- static bool isSameDay(DateTime a, DateTime b) {
- return a.year == b.year && a.month == b.month && a.day == b.day;
- }
- static bool isSameWeek(DateTime a, DateTime b) {
- /// Handle Daylight Savings by setting hour to 12:00 Noon
- /// rather than the default of Midnight
- a = new DateTime.utc(a.year, a.month, a.day);
- b = new DateTime.utc(b.year, b.month, b.day);
- var diff = a.toUtc().difference(b.toUtc()).inDays;
- if (diff.abs() >= 7) {
- return false;
- }
- var min = a.isBefore(b) ? a : b;
- var max = a.isBefore(b) ? b : a;
- var result = max.weekday % 7 - min.weekday % 7 >= 0;
- return result;
- }
- static DateTime previousMonth(DateTime m) {
- var year = m.year;
- var month = m.month;
- if (month == 1) {
- year--;
- month = 12;
- } else {
- month--;
- }
- return new DateTime(year, month);
- }
- static DateTime nextMonth(DateTime m) {
- var year = m.year;
- var month = m.month;
- if (month == 12) {
- year++;
- month = 1;
- } else {
- month++;
- }
- return new DateTime(year, month);
- }
- static DateTime previousWeek(DateTime w) {
- return w.subtract(new Duration(days: 7));
- }
- static DateTime nextWeek(DateTime w) {
- return w.add(new Duration(days: 7));
- }
- }
- /// 默认dialog背景色为半透明黑色,这里修改源码改为透明
- Future<T> showTransparentDialog<T>({
- @required BuildContext context,
- bool barrierDismissible = true,
- WidgetBuilder builder,
- }) {
- final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
- return showGeneralDialog(
- context: context,
- pageBuilder: (BuildContext buildContext, Animation<double> animation,
- Animation<double> secondaryAnimation) {
- final Widget pageChild = Builder(builder: builder);
- return SafeArea(
- child: Builder(builder: (BuildContext context) {
- return theme != null
- ? Theme(data: theme, child: pageChild)
- : pageChild;
- }),
- );
- },
- barrierDismissible: barrierDismissible,
- barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
- barrierColor: const Color(0x00FFFFFF),
- transitionDuration: const Duration(milliseconds: 150),
- transitionBuilder: _buildMaterialDialogTransitions,
- );
- }
- Widget _buildMaterialDialogTransitions(
- BuildContext context,
- Animation<double> animation,
- Animation<double> secondaryAnimation,
- Widget child) {
- return FadeTransition(
- opacity: CurvedAnimation(
- parent: animation,
- curve: Curves.easeOut,
- ),
- child: child,
- );
- }
- Future<T> showElasticDialog<T>({
- @required BuildContext context,
- bool barrierDismissible = true,
- WidgetBuilder builder,
- }) {
- final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
- return showGeneralDialog(
- context: context,
- pageBuilder: (BuildContext buildContext, Animation<double> animation,
- Animation<double> secondaryAnimation) {
- final Widget pageChild = Builder(builder: builder);
- return SafeArea(
- child: Builder(builder: (BuildContext context) {
- return theme != null
- ? Theme(data: theme, child: pageChild)
- : pageChild;
- }),
- );
- },
- barrierDismissible: barrierDismissible,
- barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
- barrierColor: Colors.black54,
- transitionDuration: const Duration(milliseconds: 550),
- transitionBuilder: _buildDialogTransitions,
- );
- }
- Widget _buildDialogTransitions(
- BuildContext context,
- Animation<double> animation,
- Animation<double> secondaryAnimation,
- Widget child) {
- return FadeTransition(
- opacity: CurvedAnimation(
- parent: animation,
- curve: Curves.easeOut,
- ),
- child: SlideTransition(
- position: Tween<Offset>(begin: const Offset(0.0, 0.3), end: Offset.zero)
- .animate(CurvedAnimation(
- parent: animation,
- curve: animation.status != AnimationStatus.forward
- ? Curves.easeOutBack
- : ElasticOutCurve(0.85),
- )),
- child: child,
- ),
- );
- }
- // Color string2Color(String colorString){
- // int value = 0x00000000;
- // if(isNotEmpty(colorString)){
- // if(colorString[0] == "#"){
- // colorString = colorString.substring(1);
- // }
- // value = int.tryParse(colorString,raidx:16);
- // if(value != null){
- // if(value < 0xFF000000){
- // value += 0xFF000000;
- // }
- // }
- // }
- // return Color(value);
- // }
- // 切大半圆
- class BottomClipper extends CustomClipper<Path> {
- double clipheight;
- BottomClipper({this.clipheight=50});
- @override
- Path getClip(Size size) {
- var path = Path();
- path.lineTo(0, 0); //第1个点
- path.lineTo(0, size.height - clipheight); //第2个点
- var firstControlPoint = Offset(size.width / 2, size.height);
- var firstEdnPoint = Offset(size.width, size.height - clipheight);
- path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy,
- firstEdnPoint.dx, firstEdnPoint.dy);
- path.lineTo(size.width, size.height - clipheight); //第3个点
- path.lineTo(size.width, 0); //第4个点
- return path;
- }
- @override
- bool shouldReclip(CustomClipper<Path> oldClipper) {
- return false;
- }
- }
|