aliyun_oss_plugin 0.0.4+4 copy "aliyun_oss_plugin: ^0.0.4+4" to clipboard
aliyun_oss_plugin: ^0.0.4+4 copied to clipboard

outdated

Flutter plugin for aliyun oss.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:aliyun_oss_plugin/aliyun_oss_plugin.dart' as AliyunOSSPlugin;
import 'package:aliyun_oss_platform_interface/aliyun_oss_platform_interface.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  dynamic initResult;
  double? progress;
  String? error;
  bool pause = false;
  String objectKey = "";
  @override
  void initState() {
    super.initState();
    initOSS();
    AliyunOSSPlugin.addMethodCallListener(MethodCallListener(
        initListener: (initData) {},
        progressListener: (progressData) {
          progress = progressData.totalBytesSent /
              progressData.totalBytesExpectedToSend;
          objectKey = progressData.objectKey;
          setState(() {});
        },
        successListener: (successData) {
          objectKey = "";
        },
        failListener: (failData) {
          // error = failData.error;
          setState(() {});
        }));
  }

  initOSS() async {
    initResult = await AliyunOSSPlugin.init(
      endpoint,
      // OSSStsTokenCredentialProvider(
      //     accessKeyId: stsAccessKeyId,
      //     secretKeyId: stsAccessKeySecret,
      //     securityToken: stsSecurityToken),
      OSSPlainTextAKSKCredentialProvider(
        accessKeyId: accessKeyId,
        accessKeySecret: accessKeySecret,
      ),
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Center(
            child: Text(error != null
                ? "上传错误: $error"
                : 'Running on: $initResult, progress: $progress'),
          ),
          floatingActionButton: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              FloatingActionButton(
                onPressed: () async {
                  // runPlayground();
                  AliyunOSSPlugin.upload(bucketName);
                },
                child: const Icon(Icons.send),
              ),
              const SizedBox(
                height: 10,
              ),
              FloatingActionButton(
                  onPressed: () async {
                    final result = pause
                        ? await AliyunOSSPlugin.resumeUpload(objectKey)
                        : await AliyunOSSPlugin.pauseUpload(objectKey);
                    if (result != null) {
                      print("反转成功");
                      setState(() {
                        pause = !pause;
                      });
                    }
                  },
                  child: pause ? Icon(Icons.play_arrow) : Icon(Icons.pause)),
            ],
          )),
    );
  }

  bool running = false;
  void runPlayground() async {
    if (running) return;
    running = true;
    var cancel = startListening((msg) {
      setState(() {
        print('事件频道: $msg');
      });
    });
    await Future.delayed(const Duration(seconds: 4));
    cancel();
    running = false;
  }
}