40 lines
1 KiB
Dart
40 lines
1 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
class UploadProgress {
|
|
final double progress;
|
|
final bool isUploading;
|
|
final String? error;
|
|
|
|
UploadProgress({this.progress = 0, this.isUploading = false, this.error});
|
|
}
|
|
|
|
class UploadNotifier extends StateNotifier<UploadProgress> {
|
|
UploadNotifier() : super(UploadProgress());
|
|
|
|
void setProgress(double progress) {
|
|
state = UploadProgress(progress: progress, isUploading = true);
|
|
}
|
|
|
|
void start() {
|
|
state = UploadProgress(progress: 0, isUploading: true);
|
|
}
|
|
|
|
void complete() {
|
|
state = UploadProgress(progress: 1, isUploading: false);
|
|
// Reset after success
|
|
Future.delayed(const Duration(seconds: 2), () {
|
|
if (state.progress == 1 && !state.isUploading) {
|
|
state = UploadProgress();
|
|
}
|
|
});
|
|
}
|
|
|
|
void fail(String error) {
|
|
state = UploadProgress(progress: 0, isUploading: false, error: error);
|
|
}
|
|
}
|
|
|
|
final uploadProvider = StateNotifierProvider<UploadNotifier, UploadProgress>((ref) {
|
|
return UploadNotifier();
|
|
});
|