common_util.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import 'package:flutter/services.dart';
  2. import 'package:get/get.dart';
  3. import 'package:get/get_core/src/get_main.dart';
  4. import 'package:location/resource/assets.gen.dart';
  5. import '../data/consts/payment_type.dart';
  6. /// 截取后几位
  7. String stringExpand(String phone) {
  8. if (phone.length < 4) {
  9. return phone;
  10. }
  11. return phone.substring(phone.length - 4);
  12. }
  13. ///截取前几位
  14. String stringShort(String phone) {
  15. if (phone.length < 4) {
  16. return phone;
  17. }
  18. return phone.substring(0, 4);
  19. }
  20. String userNickName(String? remark, String phoneNumber, bool isIntercept) {
  21. if (remark != null && remark.isNotEmpty) {
  22. return isIntercept ? stringShort(remark) : remark;
  23. } else {
  24. return isIntercept ? stringExpand(phoneNumber) : phoneNumber;
  25. }
  26. }
  27. String time2TimeDesc(int? timeMillis) {
  28. if (timeMillis == 0 || timeMillis == null) {
  29. return "未知";
  30. }
  31. int currentTimeMillis = DateTime.now().millisecondsSinceEpoch;
  32. int timeDiff = currentTimeMillis - timeMillis;
  33. if (timeDiff < 0) {
  34. return "刚刚";
  35. }
  36. int minute = timeDiff ~/ 60 ~/ 1000;
  37. if (minute < 60) {
  38. if (minute == 0) {
  39. return "刚刚";
  40. }
  41. return "$minute分钟前";
  42. }
  43. int hour = minute ~/ 60;
  44. if (hour < 24) {
  45. return "$hour小时前";
  46. }
  47. int day = hour ~/ 24;
  48. if (day < 30) {
  49. return "$day天前";
  50. }
  51. int month = day ~/ 30;
  52. if (month < 12) {
  53. return "$month月前";
  54. }
  55. int year = month ~/ 12;
  56. return "$year年前";
  57. }
  58. String addressCheck(String? address) {
  59. if (address == null || address.isEmpty) {
  60. return "未开启定位";
  61. }
  62. return address;
  63. }
  64. void copyToClipboard(String content) {
  65. Clipboard.setData(ClipboardData(text: content));
  66. }
  67. void backToSpecificPage(String targetRoute) {
  68. Get.until((route) => Get.currentRoute == targetRoute);
  69. }