import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:sy_flutter_qiniu_storage/sy_flutter_qiniu_storage.dart'; void main() => runApp(new MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => new _MyAppState(); } class _MyAppState extends State { double _process = 0.0; @override void initState() { super.initState(); } _onUpload() async { String token = 'token'; File file = await ImagePicker.pickImage(source: ImageSource.gallery); if (file == null) { return; } final syStorage = new SyFlutterQiniuStorage(); //监听上传进度 syStorage.onChanged().listen((dynamic percent) { double p = percent; setState(() { _process = p; }); print(percent); }); //上传文件 var result = await syStorage.upload(file.path, token, _key(file)); print(result); } String _key(File file) { return DateTime.now().millisecondsSinceEpoch.toString() + '.' + file.path.split('.').last; } //取消上传 _onCancel() { SyFlutterQiniuStorage.cancelUpload(); } @override Widget build(BuildContext context) { return new MaterialApp( home: new Scaffold( appBar: new AppBar( title: const Text('七牛云存储SDK demo'), ), body: Padding( padding: const EdgeInsets.all(8.0), child: new Column( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ LinearProgressIndicator( value: _process, ), RaisedButton( child: Text('上传'), onPressed: _onUpload, ), RaisedButton( child: Text('取消上传'), onPressed: _onCancel, ), ], ), ), ), ); } }