global.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // 提供五套可选主题色
  2. import 'dart:convert';
  3. import 'package:e2ee_chat/common/profile.dart';
  4. import 'package:e2ee_chat/network/api.dart';
  5. import 'package:e2ee_chat/models/user_model.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. const _themes = <MaterialColor>[
  9. Colors.blue,
  10. Colors.cyan,
  11. Colors.teal,
  12. Colors.green,
  13. Colors.red,
  14. ];
  15. class Global {
  16. static Profile profile = Profile();
  17. // 可选的主题列表
  18. static List<MaterialColor> get themes => _themes;
  19. // 是否为release版
  20. static bool get isRelease => bool.fromEnvironment("dart.vm.product");
  21. static final String emptyToken = "token";
  22. //初始化全局信息,会在APP启动时执行
  23. static init() async {
  24. var _prefs = await SharedPreferences.getInstance();
  25. var _profile = _prefs.getString("profile");
  26. if (_profile != null) {
  27. try {
  28. profile = Profile.fromJson(jsonDecode(_profile));
  29. profile.isLogin = false;
  30. } catch (e) {
  31. print(e);
  32. }
  33. }
  34. await Api.init();
  35. UserModel().login();
  36. }
  37. // 持久化Profile信息
  38. static saveProfile() async {
  39. var _prefs = await SharedPreferences.getInstance();
  40. _prefs.setString("profile", jsonEncode(profile.toJson()));
  41. }
  42. }