Commit 4164aeb6 authored by netyouli's avatar netyouli

添加计算缓存大小和清理缓存功能

parent 025c75fa
......@@ -23,6 +23,8 @@ void main() async {
// WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
// FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
await Global.init();
PaintingBinding.instance.imageCache.maximumSize = 1000000;
PaintingBinding.instance.imageCache.maximumSizeBytes = 300 << 20;
Glassy().setConfig(GlassyConfig(
radius: 16,
backgroundColor: Color.fromARGB(255, 217, 217, 217),
......
......@@ -4,19 +4,21 @@ import 'dart:io';
import 'package:chart/common/apis/apis.dart';
import 'package:chart/common/store/user.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:share_plus/share_plus.dart';
import 'package:in_app_review/in_app_review.dart';
import '../../common/routers/names.dart';
import 'models.dart';
class MyController extends GetxController {
var cacheSize = 16.43.obs;
var cacheSize = "0 B".obs;
var userInfo = Rx<UserInfoData>(UserInfoData(username: "未登录"));
var expireInfo = "--".obs;
@override
void onInit() {
// TODO: implement onInit
......@@ -26,8 +28,31 @@ class MyController extends GetxController {
ever(UserStore.to.loginDidChange, (didChange) {
requestUserInfo();
});
cacheSize.value = getAllSizeOfCacheImages();
}
//读取图片缓存大小
String getAllSizeOfCacheImages(){
ImageCache? imageCache = PaintingBinding.instance.imageCache;
String cacheSizeStr = '0 kb';
int byte = imageCache.currentSizeBytes;
if(byte >= 0 && byte < 1024) {
cacheSizeStr = '$byte B';
} else if(byte >= 1024 && byte < 1024 * 1024) {
double size = (byte * 1.0 / 1024);
String sizeStr = size.toStringAsFixed(2);
cacheSizeStr = '$sizeStr KB';
} else {
double size = (byte * 1.0 / 1024) / 1024;
String sizeStr = size.toStringAsFixed(2);
cacheSizeStr = '$sizeStr MB';
}
return cacheSizeStr;
}
refreshCacheShow() {
cacheSize.value = getAllSizeOfCacheImages();
}
goShare() async {
await Share.shareWithResult(
......@@ -44,9 +69,15 @@ class MyController extends GetxController {
}
goClearCache() {
cacheSize.value = 0.0;
if (cacheSize.value == "0 B") {
EasyLoading.showToast("已经清理");
} else {
ImageCache imageCache = PaintingBinding.instance.imageCache;
imageCache.clear();
cacheSize.value = getAllSizeOfCacheImages();
EasyLoading.showSuccess("清理成功");
}
}
void goPay() {
if (UserStore.to.isLogin) {
......
......@@ -76,7 +76,8 @@ class MyPage extends GetView<MyController> {
Expanded(
child: InkWell(
onTap: () {
Get.toNamed(AppRoutes.MY_WORK, arguments: {"operator": "work"});
Get.toNamed(AppRoutes.MY_WORK, arguments: {"operator": "work"})
?.then((value) => controller.refreshCacheShow());
},
child: Column(
children: [
......@@ -107,7 +108,8 @@ class MyPage extends GetView<MyController> {
Expanded(
child: InkWell(
onTap: () {
Get.toNamed(AppRoutes.MY_WORK, arguments: {"operator": "collect"});
Get.toNamed(AppRoutes.MY_WORK, arguments: {"operator": "collect"})
?.then((value) => controller.refreshCacheShow());
},
child: Column(
children: [
......@@ -306,7 +308,7 @@ class MyPage extends GetView<MyController> {
hasBottomLine: false,
rightItem: Container(
padding: const EdgeInsets.fromLTRB(0, 0, 10, 0),
child: Obx(() => Text("${controller.cacheSize.value}MB")),
child: Obx(() => Text(controller.cacheSize.value)),
),
onClick: () {
controller.goClearCache();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment