my_tools.dart 10 KB

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