my_tools.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import 'dart:convert' as convert;
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:bbyyy/beans/address_bean_entity.dart';
  5. import 'package:bbyyy/beans/new_msg_bean_entity.dart';
  6. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  7. import 'package:bbyyy/my_tools/my_cookie.dart';
  8. import 'package:bbyyy/my_tools/providers.dart';
  9. import 'package:convert/convert.dart';
  10. import 'package:crypto/crypto.dart';
  11. import 'package:dio/dio.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter/services.dart';
  14. import 'package:flutter_image_compress/flutter_image_compress.dart';
  15. import 'package:image_cropper/image_cropper.dart';
  16. import 'package:image_picker/image_picker.dart';
  17. import 'package:provider/provider.dart';
  18. import 'package:pull_to_refresh/pull_to_refresh.dart';
  19. import 'package:uuid/uuid.dart';
  20. import 'event_bus.dart';
  21. import 'month_day.dart';
  22. import 'my_colors.dart';
  23. class MyTools {
  24. toPage(context, page, then, {bool noBack = false}) {
  25. ImageCache imageCache = PaintingBinding.instance.imageCache;
  26. imageCache.clear();
  27. if (noBack) {
  28. Navigator.pushAndRemoveUntil(
  29. context,
  30. MaterialPageRoute(builder: (context) => page),
  31. (route) => route == null);
  32. } else {
  33. Navigator.push(context, MaterialPageRoute(builder: (context) => page))
  34. .then(then);
  35. }
  36. }
  37. hideKeyboard(BuildContext context) {
  38. FocusScope.of(context).requestFocus(FocusNode());
  39. }
  40. static String base64Encode(String data) {
  41. var content = convert.utf8.encode(data);
  42. var digest = convert.base64Encode(content);
  43. return digest;
  44. }
  45. }
  46. showToast(
  47. String status,
  48. ) {
  49. EasyLoading.showToast(status);
  50. }
  51. const _regExp = r"^[ZA-ZZa-z0-9_]+$";
  52. class onlyInputNumberAndWorkFormatter extends TextInputFormatter {
  53. @override
  54. TextEditingValue formatEditUpdate(
  55. TextEditingValue oldValue, TextEditingValue newValue) {
  56. if (newValue.text.length > 0) {
  57. if (RegExp(_regExp).firstMatch(newValue.text) != null) {
  58. return newValue;
  59. }
  60. return oldValue;
  61. }
  62. return newValue;
  63. }
  64. }
  65. final picker = ImagePicker();
  66. selectImage({bool isCover = true}) async {
  67. final pickedFile = await picker.getImage(source: ImageSource.gallery);
  68. if (pickedFile != null) {
  69. File cropperImg;
  70. if (isCover) {
  71. cropperImg = await ImageCropper.cropImage(
  72. sourcePath: pickedFile.path,
  73. cropStyle: CropStyle.rectangle,
  74. aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
  75. aspectRatioPresets: [
  76. isCover
  77. ? CropAspectRatioPreset.square
  78. : CropAspectRatioPreset.original,
  79. ],
  80. androidUiSettings: AndroidUiSettings(
  81. toolbarTitle: '图片剪裁',
  82. toolbarColor: MyColors.cFF4233,
  83. toolbarWidgetColor: Colors.white,
  84. initAspectRatio: isCover
  85. ? CropAspectRatioPreset.square
  86. : CropAspectRatioPreset.original,
  87. lockAspectRatio: isCover),
  88. iosUiSettings: IOSUiSettings(
  89. minimumAspectRatio: 1.0,
  90. ));
  91. } else {
  92. cropperImg = await ImageCropper.cropImage(
  93. sourcePath: pickedFile.path,
  94. aspectRatioPresets: [
  95. isCover
  96. ? CropAspectRatioPreset.square
  97. : CropAspectRatioPreset.original,
  98. ],
  99. androidUiSettings: AndroidUiSettings(
  100. toolbarTitle: '图片剪裁',
  101. toolbarColor: MyColors.cFF4233,
  102. toolbarWidgetColor: Colors.white,
  103. initAspectRatio: isCover
  104. ? CropAspectRatioPreset.square
  105. : CropAspectRatioPreset.original,
  106. lockAspectRatio: isCover),
  107. iosUiSettings: IOSUiSettings(
  108. minimumAspectRatio: 1.0,
  109. ));
  110. }
  111. if (cropperImg != null) {
  112. cropperImg = await compression(cropperImg, 1000000);
  113. return cropperImg;
  114. } else {
  115. return null;
  116. }
  117. }
  118. }
  119. int quality = 90;
  120. Future<File> compression(File cropperImg, int maxSize) async {
  121. int l = await cropperImg.length();
  122. print('$l');
  123. if (l > maxSize) {
  124. List<String> s = cropperImg.path.split('.');
  125. String type = s[s.length - 1];
  126. print(type);
  127. File f = await FlutterImageCompress.compressAndGetFile(
  128. cropperImg.path, cropperImg.path.replaceAll('.$type', '1.$type'),
  129. quality: quality);
  130. int l1 = await f.length();
  131. print('$l1');
  132. if (l > maxSize) {
  133. quality -= 10;
  134. return await compression(f, maxSize);
  135. } else {
  136. quality = 90;
  137. return f;
  138. }
  139. } else {
  140. return cropperImg;
  141. }
  142. }
  143. selectPicturesIndividually() async {
  144. final pickedFile = await picker.getImage(source: ImageSource.gallery);
  145. return await compression(File(pickedFile.path), 1000000);
  146. }
  147. endRe(RefreshController reController) {
  148. reController.refreshCompleted();
  149. reController.loadComplete();
  150. }
  151. String generateMd5(String data) {
  152. var content = new Utf8Encoder().convert(data);
  153. var digest = md5.convert(content);
  154. // 这里其实就是 digest.toString()
  155. return hex.encode(digest.bytes);
  156. }
  157. String reOS(String orderString) {
  158. orderString = orderString.replaceAll('\n', '');
  159. orderString = orderString.replaceAll(' ', '');
  160. orderString = orderString.replaceAll('\r', '');
  161. orderString = orderString.replaceAll('\t', '');
  162. orderString = orderString.replaceAll('\s', '');
  163. return orderString;
  164. }
  165. String getUUID() {
  166. var uuid = Uuid();
  167. var uuID = uuid.v4();
  168. return uuID.replaceAll('-', '');
  169. }
  170. getMessageContent(String c) {
  171. print(c);
  172. NewMsgBeanEntity entity = NewMsgBeanEntity().fromJson(json.decode(c));
  173. if (entity.content.type == '文字') {
  174. return entity.content.content;
  175. } else {
  176. return '[${entity.content.type}]';
  177. }
  178. }
  179. getTime(int timeIndex) {
  180. List<String> paidAt = [];
  181. String sTime = '00';
  182. String eTime = '23';
  183. var dateTime = DateTime.now();
  184. if (timeIndex == 0) {
  185. if (sTime != '00') {
  186. if (dateTime.hour >= int.parse(sTime)) {
  187. paidAt.add(
  188. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $sTime:00:00');
  189. paidAt.add(
  190. '${dateTime.add(Duration(days: 1)).year}-${numberComplement('${dateTime.add(Duration(days: 1)).month}')}-${numberComplement('${dateTime.add(Duration(days: 1)).day}')} $eTime:59:59');
  191. } else {
  192. paidAt.add(
  193. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00');
  194. paidAt.add(
  195. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59');
  196. }
  197. } else {
  198. paidAt.add(
  199. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $sTime:00:00');
  200. paidAt.add(
  201. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59');
  202. }
  203. } else if (timeIndex == 1) {
  204. if (sTime == '00') {
  205. paidAt.add(
  206. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00');
  207. paidAt.add(
  208. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $eTime:59:59');
  209. } else {
  210. if (dateTime.hour >= int.parse(sTime)) {
  211. paidAt.add(
  212. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00');
  213. paidAt.add(
  214. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59');
  215. } else {
  216. paidAt.add(
  217. '${dateTime.subtract(Duration(days: 2)).year}-${numberComplement('${dateTime.subtract(Duration(days: 2)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 2)).day}')} $sTime:00:00');
  218. paidAt.add(
  219. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $eTime:59:59');
  220. }
  221. }
  222. } else if (timeIndex == 2) {
  223. paidAt.add(
  224. '${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');
  225. paidAt.add(
  226. '${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');
  227. } else if (timeIndex == 3) {
  228. DateTime lastWeek = dateTime.subtract(Duration(days: 7));
  229. paidAt.add(
  230. '${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');
  231. paidAt.add(
  232. '${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');
  233. } else if (timeIndex == 4) {
  234. paidAt.add(
  235. '${dateTime.year}-${numberComplement('${dateTime.month}')}-01 00:00:00');
  236. paidAt.add(
  237. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${MonthDay().getDays(dateTime)}')} 23:59:59');
  238. } else if (timeIndex == 5) {
  239. DateTime firstMonth = dateTime.subtract(Duration(days: dateTime.day));
  240. paidAt.add('${firstMonth.year}-${firstMonth.month}-01 00:00:00');
  241. paidAt.add(
  242. '${firstMonth.year}-${numberComplement('${firstMonth.month}')}-${numberComplement('${MonthDay().getDays(firstMonth)}')} 23:59:59');
  243. }
  244. print(json.encode(paidAt));
  245. return paidAt;
  246. }
  247. numberComplement(String s) {
  248. if (s.length == 1) {
  249. return '0$s';
  250. } else {
  251. return '$s';
  252. }
  253. }
  254. String hideUID(String uid) {
  255. if (uid.isEmpty || uid.length <= 4) {
  256. return '';
  257. }
  258. return uid.replaceRange(0, 4, '****');
  259. }
  260. getAddressByLatitudeAndLongitude(
  261. double latitude, double longitude, BuildContext context) {
  262. print('getAddressByLatitudeAndLongitude');
  263. Dio()
  264. .get(
  265. 'https://restapi.amap.com/v3/geocode/regeo?output=json&location=$longitude,$latitude&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&radius=10&extensions=all')
  266. .then((value) {
  267. print('value.toString()------------${value.toString()}');
  268. AddressBeanEntity addressBeanEntity =
  269. AddressBeanEntity().fromJson(json.decode(value.toString()));
  270. String city;
  271. String province;
  272. if(addressBeanEntity.regeocode.addressComponent.province.isEmpty||addressBeanEntity.regeocode.addressComponent.province=='[]'){
  273. return;
  274. }else{
  275. province = addressBeanEntity.regeocode.addressComponent.province;
  276. }
  277. if( addressBeanEntity.regeocode.addressComponent.city.isEmpty||addressBeanEntity.regeocode.addressComponent.city=='[]'){
  278. city = province;
  279. }else{
  280. city = addressBeanEntity.regeocode.addressComponent.city;
  281. }
  282. print('city$city');
  283. print('province$province');
  284. MyCookie().location = MyLocation(
  285. province: province,
  286. city: city,
  287. formattedAddress: addressBeanEntity.regeocode.formattedAddress,
  288. district: addressBeanEntity.regeocode.addressComponent.district,
  289. latitude: latitude,
  290. longitude: longitude);
  291. context.read<MyLocationProvider>().getLocation(latitude, longitude);
  292. EventBus().emit('getLocation');
  293. });
  294. }