states.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:flutter/material.dart';
  2. import 'package:package_info/package_info.dart';
  3. import 'package:provider/provider.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. /// 状态储存工具类
  6. class States {
  7. // 用于首页初始化时从本地持久化存储或应用配置中读取全局状态
  8. static init(BuildContext context) {
  9. _readStates(context);
  10. }
  11. // 读取状态,异步操作
  12. static _readStates(BuildContext context) async {
  13. // 从本地储存读缓存状态
  14. SharedPreferences prefs = await SharedPreferences.getInstance();
  15. var sc = prefs.getBool(StateKey.ShowContact);
  16. Provider.of<UserStatus>(context, listen: false).userName = prefs.getString(StateKey.UserName) ?? '';
  17. Provider.of<UserStatus>(context, listen: false).userToken = prefs.getString(StateKey.UserToken) ?? '';
  18. Provider.of<AppVersion>(context, listen: false).showContact = (null == sc ? true : sc);
  19. // 当前APP版本不从缓存读,直接从应用包信息获取(实际是在pubspec.yaml文件中配置的version配置项)
  20. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  21. Provider.of<AppVersion>(context, listen: false).appVersion = packageInfo.version.split('+')[0]; // 不管+号后的构建号
  22. }
  23. // 重置缓存状态
  24. static reset(BuildContext context) async {
  25. Provider.of<UserStatus>(context, listen: false).userName = '';
  26. Provider.of<UserStatus>(context, listen: false).userToken = '';
  27. Provider.of<AppVersion>(context, listen: false).showContact = true;
  28. }
  29. // 本地持久化保存布尔值
  30. static _saveBool(String key, bool value) async {
  31. SharedPreferences prefs = await SharedPreferences.getInstance();
  32. await prefs.setBool(key, value);
  33. }
  34. // 本地持久化保存整型值
  35. static _saveInt(String key, int value) async {
  36. SharedPreferences prefs = await SharedPreferences.getInstance();
  37. await prefs.setInt(key, value);
  38. }
  39. // 本地持久化保存双精度浮点数值
  40. static _saveDouble(String key, double value) async {
  41. SharedPreferences prefs = await SharedPreferences.getInstance();
  42. await prefs.setDouble(key, value);
  43. }
  44. // 本地持久化保存字符串值
  45. static _saveString(String key, String value) async {
  46. SharedPreferences prefs = await SharedPreferences.getInstance();
  47. await prefs.setString(key, value);
  48. }
  49. }
  50. /// 用于全局状态持久化存储的键名
  51. class StateKey {
  52. static const UserName = 'userName'; // 用户名
  53. static const UserToken = 'userToken'; // 用户登录凭据
  54. static const ShowContact = 'showContact'; //
  55. }
  56. /// 用户登录全局状态,供Provider用,并带持久化存储
  57. class UserStatus with ChangeNotifier {
  58. String _userName = '';
  59. String _userToken = '';
  60. String get userName => _userName;
  61. set userName(String value) {
  62. _userName = value;
  63. States._saveString(StateKey.UserName, value);
  64. notifyListeners();
  65. }
  66. String get userToken => _userToken;
  67. set userToken(String value) {
  68. _userToken = value;
  69. States._saveString(StateKey.UserToken, value);
  70. notifyListeners();
  71. }
  72. }
  73. /// APP版本全局状态,供Provider用,并带持久化存储
  74. class AppVersion with ChangeNotifier {
  75. String _appVersion = '';
  76. bool _showContact = true;
  77. String get appVersion => _appVersion;
  78. set appVersion(String value) {
  79. _appVersion = value;
  80. notifyListeners();
  81. }
  82. bool get showContact => _showContact;
  83. set showContact(bool value) {
  84. _showContact = value;
  85. States._saveBool(StateKey.ShowContact, value);
  86. notifyListeners();
  87. }
  88. }
  89. /// APP版下载进度状态,供Provider用
  90. class AppDownloadProcess with ChangeNotifier {
  91. double _process = 0.0;
  92. double get process => _process;
  93. set process(double value) {
  94. _process = value;
  95. notifyListeners();
  96. }
  97. }