flutter_qiniu_upload 0.0.4 copy "flutter_qiniu_upload: ^0.0.4" to clipboard
flutter_qiniu_upload: ^0.0.4 copied to clipboard

A flutter plugin for Qiniu file upload. Support multiple file uploads, listen progress and cancel uploads.

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_qiniu_upload/flutter_qiniu_upload.dart';
import 'package:flutter_qiniu_upload_example/token.dart';
import 'package:image_picker/image_picker.dart';

/// use yourself qiniu token
/// generated by this link http://jsfiddle.net/gh/get/extjs/4.2/icattlecoder/jsfiddle/tree/master/uptoken
const upToken = uploadToken;

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  QiniuUpload.initialize();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

enum UploadState {
  idle,
  selected,
  uploading,
  error,
  success,
  cancelled,
}

class _MyAppState extends State<MyApp> {
  UploadState uploadState = UploadState.idle;

  String selectedFile;
  String uploadRequestId;
  QiniuProgress progress;
  QiniuFile uploadedFile;
  UploadException uploadError;

  StreamSubscription listeningProgress;

  bool get uploading => uploadRequestId != null;

  String get display {
    switch (uploadState) {
      case UploadState.selected:
        return 'Selected: \n$selectedFile';
      case UploadState.uploading:
        final percent = (progress?.percent ?? 0) * 100;
        return 'Uploading ${percent.toStringAsFixed(2)}%: \n$selectedFile';
      case UploadState.error:
        return 'Upload error: $uploadError';
      case UploadState.success:
        return 'Upload success: \nhash=${uploadedFile?.hash}\nkey=${uploadedFile?.key}';
      case UploadState.cancelled:
        return 'Upload cancelled';
      case UploadState.idle:
      default:
        return 'Select file';
    }
  }

  IconData get actionIcon {
    switch (uploadState) {
      case UploadState.uploading:
        return Icons.cancel;
      case UploadState.selected:
        return Icons.file_upload;
      case UploadState.idle:
      case UploadState.error:
      case UploadState.success:
      case UploadState.cancelled:
      default:
        return Icons.photo_album;
    }
  }

  void action() {
    switch (uploadState) {
      case UploadState.uploading:
        QiniuUpload.cancel(uploadRequestId);
        break;
      case UploadState.selected:
        doUpload(selectedFile);
        break;
      case UploadState.idle:
      case UploadState.error:
      case UploadState.success:
      case UploadState.cancelled:
      default:
        ImagePicker().getImage(source: ImageSource.gallery).then((value) {
          if (value != null) {
            final path = value.path;
            selectedFile = path;
            uploadState = UploadState.selected;
            setState(() {});
          }
        });
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('FlutterQiniuUpload Sample'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(display),
                SizedBox(
                  height: 24,
                ),
                uploading
                    ? LinearProgressIndicator(
                        value: progress?.percent ?? 0,
                        valueColor: AlwaysStoppedAnimation(Colors.lightBlue),
                      )
                    : SizedBox.shrink(),
              ],
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(actionIcon),
          onPressed: () => action(),
        ),
      ),
    );
  }

  void doUpload(String path) async {
    uploadRequestId = await QiniuUpload.upload(File(path), upToken);
    listeningProgress = QiniuUpload.progress(uploadRequestId).listen(
      (event) {
        progress = event;
        if (event.isCompleted) {
          uploadState = UploadState.success;
          uploadedFile = event.file;
          selectedFile = null;
          print('upload complete');
        }
        setState(() {});
      },
      onError: (error) {
        print('error => $error');
        if (error is UploadCancellation) {
          uploadState = UploadState.cancelled;
        } else {
          uploadState = UploadState.error;
          uploadError = error;
        }
        setState(() {});
      },
      cancelOnError: true,
    );
    print('Start upload');
    uploadState = UploadState.uploading;
    setState(() {});
  }

  @override
  void dispose() {
    listeningProgress.cancel();
    super.dispose();
  }
}
1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

A flutter plugin for Qiniu file upload. Support multiple file uploads, listen progress and cancel uploads.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_qiniu_upload