TxUtils.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'package:flutter/material.dart';
  2. import 'package:fluttertoast/fluttertoast.dart';
  3. import 'package:flutter_styled_toast/flutter_styled_toast.dart' as StyledToast;
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'dart:math';
  6. const USERID_KEY = "UserId";
  7. class TxUtils {
  8. static String _loginUserId = '';
  9. static() {
  10. if (_loginUserId == '') {
  11. getStorageByKey(USERID_KEY).then((value) {
  12. _loginUserId = value;
  13. });
  14. }
  15. }
  16. static showErrorToast(text, context) {
  17. Fluttertoast.showToast(
  18. msg: text,
  19. toastLength: Toast.LENGTH_SHORT,
  20. gravity: ToastGravity.BOTTOM,
  21. timeInSecForIosWeb: 3,
  22. backgroundColor: Colors.red,
  23. textColor: Colors.white,
  24. fontSize: 16.0,
  25. );
  26. }
  27. static getRandomNumber() {
  28. Random rng = new Random();
  29. //2147483647
  30. String numStr = '';
  31. for (var i = 0; i < 9; i++) {
  32. numStr += rng.nextInt(9).toString();
  33. }
  34. return int.tryParse(numStr);
  35. }
  36. static List<String> _defaltUrlList = [
  37. 'https://imgcache.qq.com/operation/dianshi/other/7.157d962fa53be4107d6258af6e6d83f33d45fba4.png',
  38. 'https://imgcache.qq.com/operation/dianshi/other/5.ca48acfebc4dfb68c6c463c9f33e60cb8d7c9565.png',
  39. 'https://imgcache.qq.com/operation/dianshi/other/1.724142271f4e811457eee00763e63f454af52d13.png',
  40. 'https://imgcache.qq.com/operation/dianshi/other/4.67f22bd6d283d942d06e69c6b8a2c819c0e11af5.png',
  41. 'https://imgcache.qq.com/operation/dianshi/other/6.1b984e741cc2275cda3451fa44515e018cc49cb5.png',
  42. //先不用这种图片,或者和白色字体不搭配
  43. //'https://imgcache.qq.com/operation/dianshi/other/2.4c958e11852b2caa75da6c2726f9248108d6ec8a.png',
  44. ];
  45. static getRandoAvatarUrl() {
  46. Random rng = new Random();
  47. return _defaltUrlList[rng.nextInt(_defaltUrlList.length)];
  48. }
  49. static getDefaltAvatarUrl() {
  50. return _defaltUrlList[0];
  51. }
  52. static showToast(String text, context) {
  53. Fluttertoast.cancel();
  54. Fluttertoast.showToast(
  55. msg: text,
  56. toastLength: Toast.LENGTH_SHORT,
  57. gravity: ToastGravity.BOTTOM,
  58. timeInSecForIosWeb: 3,
  59. backgroundColor: Color.fromRGBO(192, 192, 192, 0.3),
  60. textColor: Colors.black,
  61. fontSize: 16.0,
  62. );
  63. }
  64. static showStyledToast(String text, BuildContext context) {
  65. StyledToast.showToast(
  66. text,
  67. context: context,
  68. position: StyledToast.StyledToastPosition.center,
  69. );
  70. }
  71. static setStorageByKey(key, value) async {
  72. if (key == USERID_KEY) {
  73. _loginUserId = value;
  74. }
  75. SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  76. sharedPreferences.setString(key, value);
  77. }
  78. static Future<String> getStorageByKey(key) async {
  79. SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  80. String? rStr = sharedPreferences.getString(key);
  81. return rStr == null ? Future.value('') : Future.value(rStr);
  82. }
  83. static Future<String> getLoginUserId() {
  84. if (_loginUserId == '') {
  85. return getStorageByKey(USERID_KEY);
  86. }
  87. return Future.value(_loginUserId);
  88. }
  89. }