my_tools.dart 9.9 KB

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