common_util.dart 1.5 KB

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