-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dart
More file actions
134 lines (122 loc) · 3.46 KB
/
main.dart
File metadata and controls
134 lines (122 loc) · 3.46 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import 'package:cached_s5_image/cached_s5_image.dart';
import 'package:cached_s5_image_example/src/s5.dart';
import 'package:cached_s5_manager/cached_s5_manager.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:s5/s5.dart';
void main() {
runApp(const CachedS5ImageDemo());
}
class CachedS5ImageDemo extends StatelessWidget {
const CachedS5ImageDemo({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Cached S5 Image Demo',
home: Demo(),
);
}
}
class Demo extends StatefulWidget {
const Demo({super.key});
@override
State<Demo> createState() => _DemoState();
}
class _DemoState extends State<Demo> {
String? cid;
final TextEditingController _cidController = TextEditingController(
text: "z2H7AJ1Pt8FdqG5UNzt4ffEhMY28c2z3K13TGf9fGcCRRwN7kS5B");
final TextEditingController _thumhashController =
TextEditingController(text: "mtcJLYbGWGaPeYiLl4dndYaAdgdY");
S5? s5;
Logger logger = Logger();
CachedS5Manager? cacheManager;
@override
void initState() {
_initS5();
super.initState();
}
Future<void> _initCache() async {
await cacheManager?.init();
}
void _initS5() async {
// this is an EXAMPLE s5 node, use your own for maximum performance
s5 = await initS5("https://s5.jptr.tech", "hive", null);
cacheManager = CachedS5Manager(s5: s5!);
await _initCache();
setState(() {}); // to update UI
}
void _submitCID() async {
if (s5 != null) {
setState(() {
cid = _cidController.text;
});
}
}
void _clearCache() async {
cacheManager?.clear();
}
void _clearImage() async {
setState(() {
cid = null;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
Row(
children: [
const Text("S5 Status:"),
(s5 == null)
? const CircularProgressIndicator()
: const Icon(Icons.check),
],
),
TextField(
controller: _cidController,
decoration: const InputDecoration(labelText: "CID: z2..."),
),
const SizedBox(
height: 10,
),
TextField(
controller: _thumhashController,
decoration: const InputDecoration(labelText: "Thumbhash: "),
),
const SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _submitCID, child: const Text("Submit CID")),
ElevatedButton(
onPressed: _clearCache, child: const Text("Clear Cache")),
ElevatedButton(
onPressed: _clearImage,
child: const Text("Clear loaded image"))
],
),
const SizedBox(
height: 10,
),
AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: (cid != null && s5 != null)
? CachedS5Image(
cid: cid!,
s5: s5!,
thumbhash: _thumhashController.text,
cacheManager: cacheManager,
)
: Container(),
),
],
),
);
}
}