aliyun_oss_ios 0.0.2
aliyun_oss_ios: ^0.0.2 copied to clipboard
A new flutter plugin project.
example/lib/main.dart
import 'package:aliyun_oss_platform_interface/aliyun_oss_platform_interface.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.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;
int bytesSent = 0;
int totalBytesSent = 0;
int totalBytesExpectedToSend = 0;
bool? finish;
String? error;
@override
void initState() {
super.initState();
initOSS();
AliyunOSSPlatform.instance.setMethodCallHandler(nativeMethodCallhandler);
}
Future<dynamic> nativeMethodCallhandler(MethodCall methodCall) async {
switch (methodCall.method) {
case "upload_progress":
final params = methodCall.arguments;
finish = params["finish"];
if (finish == true) {
progress = 1.0;
} else {
bytesSent = params['bytesSent'];
totalBytesSent = params['totalBytesSent'];
totalBytesExpectedToSend = params['totalBytesExpectedToSend'];
progress = totalBytesSent/ totalBytesExpectedToSend;
}
print('flutter: progress: $progress, finish: $finish');
setState(() {});
return progress;
case "upload_fail":
error = methodCall.arguments["error"];
return null;
default:
return "nothing";
}
}
initOSS() async {
initResult = await AliyunOSSPlatform.instance.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, finish: $finish'),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
// runPlayground();
AliyunOSSPlatform.instance.putObject(bucketName);
},
child: const Icon(Icons.send),
),
),
);
}
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;
}
}