main.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:image_picker/image_picker.dart';
  4. import 'package:sy_flutter_qiniu_storage/sy_flutter_qiniu_storage.dart';
  5. void main() => runApp(new MyApp());
  6. class MyApp extends StatefulWidget {
  7. @override
  8. _MyAppState createState() => new _MyAppState();
  9. }
  10. class _MyAppState extends State<MyApp> {
  11. double _process = 0.0;
  12. @override
  13. void initState() {
  14. super.initState();
  15. }
  16. _onUpload() async {
  17. String token = 'token';
  18. File file = await ImagePicker.pickImage(source: ImageSource.gallery);
  19. if (file == null) {
  20. return;
  21. }
  22. final syStorage = new SyFlutterQiniuStorage();
  23. //监听上传进度
  24. syStorage.onChanged().listen((dynamic percent) {
  25. double p = percent;
  26. setState(() {
  27. _process = p;
  28. });
  29. print(percent);
  30. });
  31. //上传文件
  32. var result = await syStorage.upload(file.path, token, _key(file));
  33. print(result);
  34. }
  35. String _key(File file) {
  36. return DateTime.now().millisecondsSinceEpoch.toString() +
  37. '.' +
  38. file.path.split('.').last;
  39. }
  40. //取消上传
  41. _onCancel() {
  42. SyFlutterQiniuStorage.cancelUpload();
  43. }
  44. @override
  45. Widget build(BuildContext context) {
  46. return new MaterialApp(
  47. home: new Scaffold(
  48. appBar: new AppBar(
  49. title: const Text('七牛云存储SDK demo'),
  50. ),
  51. body: Padding(
  52. padding: const EdgeInsets.all(8.0),
  53. child: new Column(
  54. mainAxisAlignment: MainAxisAlignment.spaceAround,
  55. children: <Widget>[
  56. LinearProgressIndicator(
  57. value: _process,
  58. ),
  59. RaisedButton(
  60. child: Text('上传'),
  61. onPressed: _onUpload,
  62. ),
  63. RaisedButton(
  64. child: Text('取消上传'),
  65. onPressed: _onCancel,
  66. ),
  67. ],
  68. ),
  69. ),
  70. ),
  71. );
  72. }
  73. }