-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_screen.dart
More file actions
320 lines (293 loc) · 9.53 KB
/
upload_screen.dart
File metadata and controls
320 lines (293 loc) · 9.53 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import 'dart:io';
import 'package:fastpix_resumable_uploader/fastpix_resumable_uploader.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'signed_url_service.dart';
/// Replace these with your own FastPix credentials before running the example.
/// See https://docs.fastpix.com/docs/basic-authentication
const String _kFastPixTokenId = '3d319224-5592-433f-9ab6-e5180e3f3197';
const String _kFastPixSecretKey = 'b6ab7dad-e1f2-4701-a847-ae4f40f12f1d';
class UploadScreen extends StatefulWidget {
const UploadScreen({super.key});
@override
State<UploadScreen> createState() => _UploadScreenState();
}
class _UploadScreenState extends State<UploadScreen> {
final ImagePicker _picker = ImagePicker();
final SignedUrlService _signedUrlService = SignedUrlService(
tokenId: _kFastPixTokenId,
secretKey: _kFastPixSecretKey,
);
FlutterResumableUploads? _uploader;
File? _selectedFile;
String _status = 'Pick a video to get started.';
double _progress = 0.0;
int _currentChunk = 0;
int _totalChunks = 0;
bool _isBusy = false;
bool _isPaused = false;
bool _isCompleted = false;
String? _errorMessage;
@override
void dispose() {
_uploader?.dispose();
super.dispose();
}
Future<void> _pickVideo() async {
final picked = await _picker.pickVideo(source: ImageSource.gallery);
if (picked == null) return;
setState(() {
_selectedFile = File(picked.path);
_status = 'Selected: ${picked.name}';
_progress = 0.0;
_currentChunk = 0;
_totalChunks = 0;
_isCompleted = false;
_errorMessage = null;
});
}
Future<void> _startUpload() async {
final file = _selectedFile;
if (file == null) return;
setState(() {
_isBusy = true;
_isPaused = false;
_isCompleted = false;
_errorMessage = null;
_status = 'Requesting signed URL…';
});
try {
final signedUrl = await _signedUrlService.generateSignedUrl(
metadata: {
'fileName': file.uri.pathSegments.last,
'uploadedBy': 'flutter_example_app',
},
);
// Dispose any previous run before starting a new one.
_uploader?.dispose();
_uploader = FlutterResumableUploads.builder()
.file(file)
.signedUrl(signedUrl)
.chunkSize(16 * 1024 * 1024) // 16 MB
.maxRetries(5)
.retryDelay(const Duration(seconds: 2))
.enableLoggingWithLevel(LogLevel.info)
.onProgress(_handleProgress)
.onError(_handleError)
.onPause(() => _updateStatus('Upload paused'))
.onAbort(() => _updateStatus('Upload aborted'))
.build();
// uploadVideo() now resolves only when the upload reaches a terminal
// state (completed or permanently failed). Any transient errors
// surface through onError but the future stays alive.
try {
await _uploader!.uploadVideo();
} catch (e) {
// Terminal failure — set busy=false here, not in onError.
if (mounted) {
setState(() {
_errorMessage = e is UploadError ? e.message : e.toString();
_isBusy = false;
_isPaused = false;
});
}
return;
}
// Terminal success.
if (mounted) {
setState(() {
_isBusy = false;
_isPaused = false;
});
}
} catch (e) {
// Synchronous setup failure (signed-URL fetch, builder validation).
if (mounted) {
setState(() {
_errorMessage = e.toString();
_isBusy = false;
_isPaused = false;
});
}
}
}
void _handleProgress(ProgressModel progress) {
if (!mounted) return;
setState(() {
_status = progress.status;
_progress = progress.uploadPercentage;
_currentChunk = progress.currentChunkIndex;
_totalChunks = progress.totalChunks;
_isCompleted = progress.isCompleted;
if (_isCompleted) {
_isBusy = false;
_progress = 100.0;
}
});
}
void _handleError(UploadError error) {
if (!mounted) return;
// The error stream emits both transient ("retrying chunk 3…") and
// terminal errors. Only update the displayed message here — the
// session's busy/paused state is driven by the uploadVideo() future
// (terminal) and the pause/resume buttons (user-initiated).
setState(() {
_errorMessage = error.message;
});
}
void _updateStatus(String status) {
if (!mounted) return;
setState(() => _status = status);
}
void _pauseOrResume() {
final uploader = _uploader;
if (uploader == null) return;
if (_isPaused) {
uploader.resumeUpload();
setState(() => _isPaused = false);
} else {
uploader.pauseUpload();
setState(() => _isPaused = true);
}
}
void _abort() {
_uploader?.abortUpload();
setState(() {
_isBusy = false;
_isPaused = false;
_progress = 0.0;
});
}
@override
Widget build(BuildContext context) {
final hasFile = _selectedFile != null;
final canControl = _isBusy && !_isCompleted;
return Scaffold(
appBar: AppBar(
title: const Text('FastPix Resumable Uploader'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_FileCard(file: _selectedFile),
const SizedBox(height: 16),
FilledButton.tonalIcon(
onPressed: _isBusy ? null : _pickVideo,
icon: const Icon(Icons.video_library_outlined),
label: const Text('Pick video from gallery'),
),
const SizedBox(height: 8),
FilledButton.icon(
onPressed: (hasFile && !_isBusy) ? _startUpload : null,
icon: const Icon(Icons.cloud_upload_outlined),
label: const Text('Upload to FastPix'),
),
const SizedBox(height: 24),
LinearProgressIndicator(
value: _progress == 0 ? 0 : (_progress / 100).clamp(0.0, 1.0),
minHeight: 8,
),
const SizedBox(height: 8),
Text(
'${_progress.toStringAsFixed(1)}%'
'${_totalChunks > 0 ? ' • chunk $_currentChunk / $_totalChunks' : ''}',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 16),
Text(
_status,
style: Theme.of(context).textTheme.bodyMedium,
),
const Spacer(),
Row(
children: [
Expanded(
child: OutlinedButton.icon(
onPressed: canControl ? _pauseOrResume : null,
icon: Icon(_isPaused ? Icons.play_arrow : Icons.pause),
label: Text(_isPaused ? 'Resume' : 'Pause'),
),
),
const SizedBox(width: 12),
Expanded(
child: OutlinedButton.icon(
onPressed: canControl ? _abort : null,
icon: const Icon(Icons.stop),
label: const Text('Abort'),
style: OutlinedButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error,
),
),
),
],
),
if (_isCompleted) ...[
const SizedBox(height: 16),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.green.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(8),
),
child: const Row(
children: [
Icon(Icons.check_circle, color: Colors.green),
SizedBox(width: 8),
Expanded(child: Text('Upload completed successfully.')),
],
),
),
],
],
),
),
);
}
}
class _FileCard extends StatelessWidget {
const _FileCard({required this.file});
final File? file;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: theme.colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
Icon(
file == null ? Icons.movie_outlined : Icons.movie,
size: 36,
color: theme.colorScheme.primary,
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
file == null ? 'No file selected' : file!.uri.pathSegments.last,
style: theme.textTheme.titleMedium,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 4),
Text(
file == null
? 'Pick a video to upload it to FastPix.'
: '${(file!.lengthSync() / (1024 * 1024)).toStringAsFixed(2)} MB',
style: theme.textTheme.bodySmall,
),
],
),
),
],
),
);
}
}