/
main.dart
167 lines (157 loc) · 5.49 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import 'package:cached_firestorage/lib.dart';
import 'package:cached_firestorage_demo/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
CachedFirestorage.instance.cacheTimeout = 30;
runApp(const Demo());
}
class Demo extends StatelessWidget {
const Demo({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cached Firestorage Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String currentPic = '2-1.jpeg';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Cached Firestorage DEMO'),
),
body: Center(
child: ListView(
children: [
const Text(
'The standard way is using a FutureBuilder:\n'
'All you have to do is providing the Firebase Storage path and a key of your choice.'
'CachedFirestorage will fetch the download url the first time,\n'
'and keep that in cache until the timer expires',
textAlign: TextAlign.center,
),
FutureBuilder<String>(
future: CachedFirestorage.instance.getDownloadURL(
mapKey: '1',
filePath: '1.jpeg',
),
builder: (_, snapshot) => snapshot.connectionState == ConnectionState.waiting
? const CircularProgressIndicator.adaptive()
: snapshot.hasError
? const Text('An error occurred')
: Image.network(
snapshot.data!,
height: 100,
),
),
const Divider(height: 50),
const Text(
'You can invalidate the cache for a specific key every time you want\n'
'This could be useful if you need to update the url associated a specific key.',
textAlign: TextAlign.center,
),
FutureBuilder<String>(
future: CachedFirestorage.instance.getDownloadURL(
mapKey: '2',
filePath: currentPic,
),
builder: (_, snapshot) => snapshot.connectionState == ConnectionState.waiting
? const CircularProgressIndicator.adaptive()
: snapshot.hasError
? const Text('An error occurred')
: Image.network(
snapshot.data!,
height: 100,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: const Text(
'Change URL',
style: TextStyle(fontSize: 12),
textAlign: TextAlign.center,
),
onPressed: () {
setState(() {
currentPic = currentPic == '2-1.jpeg' ? '2-2.jpeg' : '2-1.jpeg';
});
},
),
const SizedBox(width: 5),
ElevatedButton(
child: const Text(
'Remove cache + Change URL',
style: TextStyle(fontSize: 12),
textAlign: TextAlign.center,
),
onPressed: () {
setState(() {
currentPic = currentPic == '2-1.jpeg' ? '2-2.jpeg' : '2-1.jpeg';
CachedFirestorage.instance.removeCacheEntry(mapKey: '2');
});
},
),
],
),
const Divider(height: 50),
const Text(
'CachedFirestorage ships with a utility widget named RemotePicture,\n'
'which is already optimized for fetching and displaying images stored'
'in Firebase Storage',
textAlign: TextAlign.center,
),
const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 100,
child: RemotePicture(
imagePath: '3.jpeg',
mapKey: '3',
),
),
],
),
const Divider(height: 50),
const Text(
'You can also use it for an avatar',
textAlign: TextAlign.center,
),
const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RemotePicture(
imagePath: 'avatar.jpeg',
mapKey: 'avatar',
useAvatarView: true,
avatarViewRadius: 60,
fit: BoxFit.cover,
storageKey: 'pp',
),
],
),
],
),
),
);
}
}