pikapika/lib/screens/CleanScreen.dart

109 lines
2.5 KiB
Dart
Raw Permalink 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 {
2022-03-17 03:31:25 +00:00
const CleanScreen({Key? key}) : super(key: key);
2021-09-29 23:57:09 +00:00
@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(
2022-03-17 03:31:25 +00:00
title: const Text('清理'),
2021-09-29 23:57:09 +00:00
),
body: ListView(
children: [
Container(
2022-03-17 03:31:25 +00:00
padding: const EdgeInsets.all(8),
2021-09-29 23:57:09 +00:00
child: _cleanResult != "" ? Text(_cleanResult) : Container(),
2021-11-08 05:38:27 +00:00
),
2022-03-17 03:31:25 +00:00
SizedBox(
2021-11-08 05:38:27 +00:00
height: 50,
child: FitButton(
text: '清理网络缓存',
onPressed: () {
processCleanAction(method.cleanNetworkCache);
},
),
),
2022-03-17 03:31:25 +00:00
SizedBox(
2021-11-08 05:38:27 +00:00
height: 50,
child: FitButton(
text: '清理图片缓存',
onPressed: () {
processCleanAction(method.cleanImageCache);
},
),
),
2022-03-17 03:31:25 +00:00
SizedBox(
2021-11-08 05:38:27 +00:00
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
}