123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import 'package:flutter/material.dart';
- import 'package:package_info/package_info.dart';
- import 'package:provider/provider.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- /// 状态储存工具类
- class States {
- // 用于首页初始化时从本地持久化存储或应用配置中读取全局状态
- static init(BuildContext context) {
- _readStates(context);
- }
- // 读取状态,异步操作
- static _readStates(BuildContext context) async {
- // 从本地储存读缓存状态
- SharedPreferences prefs = await SharedPreferences.getInstance();
- var sc = prefs.getBool(StateKey.ShowContact);
- Provider.of<UserStatus>(context, listen: false).userName = prefs.getString(StateKey.UserName) ?? '';
- Provider.of<UserStatus>(context, listen: false).userToken = prefs.getString(StateKey.UserToken) ?? '';
- Provider.of<AppVersion>(context, listen: false).showContact = (null == sc ? true : sc);
- // 当前APP版本不从缓存读,直接从应用包信息获取(实际是在pubspec.yaml文件中配置的version配置项)
- PackageInfo packageInfo = await PackageInfo.fromPlatform();
- Provider.of<AppVersion>(context, listen: false).appVersion = packageInfo.version.split('+')[0]; // 不管+号后的构建号
- }
- // 重置缓存状态
- static reset(BuildContext context) async {
- Provider.of<UserStatus>(context, listen: false).userName = '';
- Provider.of<UserStatus>(context, listen: false).userToken = '';
- Provider.of<AppVersion>(context, listen: false).showContact = true;
- }
- // 本地持久化保存布尔值
- static _saveBool(String key, bool value) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- await prefs.setBool(key, value);
- }
- // 本地持久化保存整型值
- static _saveInt(String key, int value) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- await prefs.setInt(key, value);
- }
- // 本地持久化保存双精度浮点数值
- static _saveDouble(String key, double value) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- await prefs.setDouble(key, value);
- }
- // 本地持久化保存字符串值
- static _saveString(String key, String value) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- await prefs.setString(key, value);
- }
- }
- /// 用于全局状态持久化存储的键名
- class StateKey {
- static const UserName = 'userName'; // 用户名
- static const UserToken = 'userToken'; // 用户登录凭据
- static const ShowContact = 'showContact'; //
- }
- /// 用户登录全局状态,供Provider用,并带持久化存储
- class UserStatus with ChangeNotifier {
- String _userName = '';
- String _userToken = '';
- String get userName => _userName;
- set userName(String value) {
- _userName = value;
- States._saveString(StateKey.UserName, value);
- notifyListeners();
- }
- String get userToken => _userToken;
- set userToken(String value) {
- _userToken = value;
- States._saveString(StateKey.UserToken, value);
- notifyListeners();
- }
- }
- /// APP版本全局状态,供Provider用,并带持久化存储
- class AppVersion with ChangeNotifier {
- String _appVersion = '';
- bool _showContact = true;
- String get appVersion => _appVersion;
- set appVersion(String value) {
- _appVersion = value;
- notifyListeners();
- }
- bool get showContact => _showContact;
- set showContact(bool value) {
- _showContact = value;
- States._saveBool(StateKey.ShowContact, value);
- notifyListeners();
- }
- }
- /// APP版下载进度状态,供Provider用
- class AppDownloadProcess with ChangeNotifier {
- double _process = 0.0;
- double get process => _process;
- set process(double value) {
- _process = value;
- notifyListeners();
- }
- }
|