pikapika/lib/screens/CleanScreen.dart

107 lines
2.4 KiB
Dart
Raw Normal View History

2021-09-29 23:57:09 +00:00
import 'dart:async';
import 'package:flutter/material.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Channels.dart';
import 'package:pikapika/basic/Method.dart';
import 'package:pikapika/screens/components/FitButton.dart';
2021-09-29 23:57:09 +00:00
import 'components/ContentLoading.dart';
// 清理
class CleanScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => _CleanScreenState();
}
class _CleanScreenState extends State<CleanScreen> {
late bool _cleaning = false;
late String _cleaningMessage = "清理中";
late String _cleanResult = "";
@override
void initState() {
registerEvent(_onMessageChange, "EXPORT");
super.initState();
}
@override
void dispose() {
unregisterEvent(_onMessageChange);
super.dispose();
}
void _onMessageChange(String event) {
setState(() {
_cleaningMessage = event;
});
}
@override
Widget build(BuildContext context) {
if (_cleaning) {
return Scaffold(
body: ContentLoading(label: _cleaningMessage),
);
}
return Scaffold(
appBar: AppBar(
title: Text('清理'),
),
body: ListView(
children: [
Container(
padding: EdgeInsets.all(8),
child: _cleanResult != "" ? Text(_cleanResult) : Container(),
2021-11-08 05:38:27 +00:00
),
Container(
height: 50,
child: FitButton(
text: '清理网络缓存',
onPressed: () {
processCleanAction(method.cleanNetworkCache);
},
),
),
Container(
height: 50,
child: FitButton(
text: '清理图片缓存',
onPressed: () {
processCleanAction(method.cleanImageCache);
},
),
),
Container(
height: 50,
child: FitButton(
text: '清理全部缓存',
onPressed: () {
processCleanAction(method.clean);
},
),
),
2021-09-29 23:57:09 +00:00
],
),
);
}
2021-11-08 05:38:27 +00:00
Future processCleanAction(Future Function() action) async {
try {
setState(() {
_cleaning = true;
});
await action();
setState(() {
_cleanResult = "清理成功";
});
} catch (e) {
setState(() {
_cleanResult = "清理失败 $e";
});
} finally {
setState(() {
_cleaning = false;
});
}
}
2021-09-29 23:57:09 +00:00
}