import 'dart:convert' as convert; import 'dart:convert'; import 'dart:io'; import 'package:bbyyy/beans/address_bean_entity.dart'; import 'package:bbyyy/beans/new_msg_bean_entity.dart'; import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart'; import 'package:bbyyy/my_tools/my_cookie.dart'; import 'package:bbyyy/my_tools/providers.dart'; import 'package:convert/convert.dart'; import 'package:crypto/crypto.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_image_compress/flutter_image_compress.dart'; import 'package:image_cropper/image_cropper.dart'; import 'package:image_picker/image_picker.dart'; import 'package:provider/provider.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:uuid/uuid.dart'; import 'event_bus.dart'; import 'month_day.dart'; import 'my_colors.dart'; class MyTools { toPage(context, page, then, {bool noBack = false}) { ImageCache imageCache = PaintingBinding.instance.imageCache; imageCache.clear(); if (noBack) { Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => page), (route) => route == null); } else { Navigator.push(context, MaterialPageRoute(builder: (context) => page)) .then(then); } } hideKeyboard(BuildContext context) { FocusScope.of(context).requestFocus(FocusNode()); } static String base64Encode(String data) { var content = convert.utf8.encode(data); var digest = convert.base64Encode(content); return digest; } } showToast( String status, ) { EasyLoading.showToast(status); } const _regExp = r"^[ZA-ZZa-z0-9_]+$"; class onlyInputNumberAndWorkFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { if (newValue.text.length > 0) { if (RegExp(_regExp).firstMatch(newValue.text) != null) { return newValue; } return oldValue; } return newValue; } } final picker = ImagePicker(); selectImage({bool isCover = true}) async { final pickedFile = await picker.getImage(source: ImageSource.gallery); if (pickedFile != null) { File cropperImg; if (isCover) { cropperImg = await ImageCropper.cropImage( sourcePath: pickedFile.path, cropStyle: CropStyle.rectangle, aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1), aspectRatioPresets: [ isCover ? CropAspectRatioPreset.square : CropAspectRatioPreset.original, ], androidUiSettings: AndroidUiSettings( toolbarTitle: '图片剪裁', toolbarColor: MyColors.cFF4233, toolbarWidgetColor: Colors.white, initAspectRatio: isCover ? CropAspectRatioPreset.square : CropAspectRatioPreset.original, lockAspectRatio: isCover), iosUiSettings: IOSUiSettings( minimumAspectRatio: 1.0, )); } else { cropperImg = await ImageCropper.cropImage( sourcePath: pickedFile.path, aspectRatioPresets: [ isCover ? CropAspectRatioPreset.square : CropAspectRatioPreset.original, ], androidUiSettings: AndroidUiSettings( toolbarTitle: '图片剪裁', toolbarColor: MyColors.cFF4233, toolbarWidgetColor: Colors.white, initAspectRatio: isCover ? CropAspectRatioPreset.square : CropAspectRatioPreset.original, lockAspectRatio: isCover), iosUiSettings: IOSUiSettings( minimumAspectRatio: 1.0, )); } if (cropperImg != null) { cropperImg = await compression(cropperImg, 1000000); return cropperImg; } else { return null; } } } int quality = 90; Future compression(File cropperImg, int maxSize) async { int l = await cropperImg.length(); print('$l'); if (l > maxSize) { List s = cropperImg.path.split('.'); String type = s[s.length - 1]; print(type); File f = await FlutterImageCompress.compressAndGetFile( cropperImg.path, cropperImg.path.replaceAll('.$type', '1.$type'), quality: quality); int l1 = await f.length(); print('$l1'); if (l > maxSize) { quality -= 10; return await compression(f, maxSize); } else { quality = 90; return f; } } else { return cropperImg; } } selectPicturesIndividually() async { final pickedFile = await picker.getImage(source: ImageSource.gallery); return await compression(File(pickedFile.path), 1000000); } endRe(RefreshController reController) { reController.refreshCompleted(); reController.loadComplete(); } String generateMd5(String data) { var content = new Utf8Encoder().convert(data); var digest = md5.convert(content); // 这里其实就是 digest.toString() return hex.encode(digest.bytes); } String reOS(String orderString) { orderString = orderString.replaceAll('\n', ''); orderString = orderString.replaceAll(' ', ''); orderString = orderString.replaceAll('\r', ''); orderString = orderString.replaceAll('\t', ''); orderString = orderString.replaceAll('\s', ''); return orderString; } String getUUID() { var uuid = Uuid(); var uuID = uuid.v4(); return uuID.replaceAll('-', ''); } getMessageContent(String c) { print(c); NewMsgBeanEntity entity = NewMsgBeanEntity().fromJson(json.decode(c)); if (entity.content.type == '文字') { return entity.content.content; } else { return '[${entity.content.type}]'; } } getTime(int timeIndex) { List paidAt = []; String sTime = '00'; String eTime = '23'; var dateTime = DateTime.now(); if (timeIndex == 0) { if (sTime != '00') { if (dateTime.hour >= int.parse(sTime)) { paidAt.add( '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $sTime:00:00'); paidAt.add( '${dateTime.add(Duration(days: 1)).year}-${numberComplement('${dateTime.add(Duration(days: 1)).month}')}-${numberComplement('${dateTime.add(Duration(days: 1)).day}')} $eTime:59:59'); } else { paidAt.add( '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00'); paidAt.add( '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59'); } } else { paidAt.add( '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $sTime:00:00'); paidAt.add( '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59'); } } else if (timeIndex == 1) { if (sTime == '00') { paidAt.add( '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00'); paidAt.add( '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $eTime:59:59'); } else { if (dateTime.hour >= int.parse(sTime)) { paidAt.add( '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00'); paidAt.add( '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59'); } else { paidAt.add( '${dateTime.subtract(Duration(days: 2)).year}-${numberComplement('${dateTime.subtract(Duration(days: 2)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 2)).day}')} $sTime:00:00'); paidAt.add( '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $eTime:59:59'); } } } else if (timeIndex == 2) { paidAt.add( '${dateTime.subtract(Duration(days: dateTime.weekday - 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: dateTime.weekday - 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: dateTime.weekday - 1)).day}')} 00:00:00'); paidAt.add( '${dateTime.add(Duration(days: 7 - dateTime.weekday)).year}-${numberComplement('${dateTime.add(Duration(days: 7 - dateTime.weekday)).month}')}-${numberComplement('${dateTime.add(Duration(days: 7 - dateTime.weekday)).day}')} 23:59:59'); } else if (timeIndex == 3) { DateTime lastWeek = dateTime.subtract(Duration(days: 7)); paidAt.add( '${lastWeek.subtract(Duration(days: lastWeek.weekday - 1)).year}-${numberComplement('${lastWeek.subtract(Duration(days: lastWeek.weekday - 1)).month}')}-${numberComplement('${lastWeek.subtract(Duration(days: lastWeek.weekday - 1)).day}')} 00:00:00'); paidAt.add( '${lastWeek.add(Duration(days: 7 - lastWeek.weekday)).year}-${numberComplement('${lastWeek.add(Duration(days: 7 - lastWeek.weekday)).month}')}-${numberComplement('${lastWeek.add(Duration(days: 7 - lastWeek.weekday)).day}')} 23:59:59'); } else if (timeIndex == 4) { paidAt.add( '${dateTime.year}-${numberComplement('${dateTime.month}')}-01 00:00:00'); paidAt.add( '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${MonthDay().getDays(dateTime)}')} 23:59:59'); } else if (timeIndex == 5) { DateTime firstMonth = dateTime.subtract(Duration(days: dateTime.day)); paidAt.add('${firstMonth.year}-${firstMonth.month}-01 00:00:00'); paidAt.add( '${firstMonth.year}-${numberComplement('${firstMonth.month}')}-${numberComplement('${MonthDay().getDays(firstMonth)}')} 23:59:59'); } print(json.encode(paidAt)); return paidAt; } numberComplement(String s) { if (s.length == 1) { return '0$s'; } else { return '$s'; } } String hideUID(String uid) { if (uid.isEmpty || uid.length <= 4) { return ''; } return uid.replaceRange(0, 4, '****'); } getAddressByLatitudeAndLongitude( double latitude, double longitude, BuildContext context) { print('getAddressByLatitudeAndLongitude'); Dio() .get( 'https://restapi.amap.com/v3/geocode/regeo?output=json&location=$longitude,$latitude&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&radius=10&extensions=all') .then((value) { print('value.toString()------------${value.toString()}'); AddressBeanEntity addressBeanEntity = AddressBeanEntity().fromJson(json.decode(value.toString())); String city; String province; if(addressBeanEntity.regeocode.addressComponent.province.isEmpty||addressBeanEntity.regeocode.addressComponent.province=='[]'){ return; }else{ province = addressBeanEntity.regeocode.addressComponent.province; } if( addressBeanEntity.regeocode.addressComponent.city.isEmpty||addressBeanEntity.regeocode.addressComponent.city=='[]'){ city = province; }else{ city = addressBeanEntity.regeocode.addressComponent.city; } print('city$city'); print('province$province'); MyCookie().location = MyLocation( province: province, city: city, formattedAddress: addressBeanEntity.regeocode.formattedAddress, district: addressBeanEntity.regeocode.addressComponent.district, latitude: latitude, longitude: longitude); context.read().getLocation(latitude, longitude); EventBus().emit('getLocation'); }); }