/
main.dart
84 lines (74 loc) · 2.21 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 'package:baseflow_plugin_template/baseflow_plugin_template.dart';
import 'package:example/plugin_example/download_page.dart';
import 'package:example/plugin_example/floating_action_button.dart';
import 'package:flutter/foundation.dart' show kDebugMode;
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
void main() {
runApp(
BaseflowPluginExample(
pluginName: 'Flutter Cache Manager',
githubURL: 'https://github.com/Baseflow/flutter_cache_manager',
pubDevURL: 'https://pub.dev/packages/flutter_cache_manager',
pages: [CacheManagerPage.createPage()],
),
);
CacheManager.logLevel = CacheManagerLogLevel.verbose;
}
const url = 'https://picsum.photos/200/300';
/// Example [Widget] showing the functionalities of flutter_cache_manager
class CacheManagerPage extends StatefulWidget {
const CacheManagerPage({super.key});
static ExamplePage createPage() {
return ExamplePage(Icons.save_alt, (context) => const CacheManagerPage());
}
@override
CacheManagerPageState createState() => CacheManagerPageState();
}
class CacheManagerPageState extends State<CacheManagerPage> {
Stream<FileResponse>? fileStream;
void _downloadFile() {
setState(() {
fileStream = DefaultCacheManager().getFileStream(url, withProgress: true);
});
}
@override
Widget build(BuildContext context) {
if (fileStream == null) {
return Scaffold(
body: const ListTile(
title: Text('Tap the floating action button to download.'),
),
floatingActionButton: Fab(
downloadFile: _downloadFile,
),
);
}
return DownloadPage(
fileStream: fileStream!,
downloadFile: _downloadFile,
clearCache: _clearCache,
removeFile: _removeFile,
);
}
void _clearCache() {
DefaultCacheManager().emptyCache();
setState(() {
fileStream = null;
});
}
void _removeFile() {
DefaultCacheManager().removeFile(url).then((value) {
if (kDebugMode) {
print('File removed');
}
}).onError((error, stackTrace) {
if (kDebugMode) {
print(error);
}
});
setState(() {
fileStream = null;
});
}
}