byteplus_bd_file_upload 1.45.303
byteplus_bd_file_upload: ^1.45.303 copied to clipboard
BytePlus file upload Flutter plugin
example/lib/main.dart
import 'dart:convert';
import 'dart:io';
import 'package:byteplus_bd_file_upload/core/env.dart';
import 'package:byteplus_bd_file_upload/core/upload.dart';
import 'package:byteplus_bd_file_upload_example/settings_page.dart';
import 'package:byteplus_bd_file_upload_example/widget/uploader_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'model/SettingParams.dart';
void main() async {
await dotenv.load(fileName: ".env");
FlutterError.onError = (details) {
FlutterError.presentError(details);
print("ttmn --- flutter error: ${details.exceptionAsString()}");
};
runApp(const UploadDemoPage());
}
class UploadDemoPage extends StatefulWidget {
const UploadDemoPage({super.key});
@override
State<UploadDemoPage> createState() => _UploadDemoPage();
}
class _UploadDemoPage extends State<UploadDemoPage> {
AsyncCallback? asyncCallback;
@override
void initState() {
super.initState();
initPlatformState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_checkPermission(); // 异步检查权限
});
}
/// 获取存储权限
Future<bool> _getStoragePermission() async {
late PermissionStatus myPermission;
/// 读取系统权限
if (Platform.isAndroid) {
myPermission = await Permission.storage.request();
}
if (myPermission != PermissionStatus.granted) {
return false;
} else {
if (Platform.isAndroid) {
return true;
}
}
return false;
}
void _checkPermission() async {
// 请求存储权限
final permissionState = await _getStoragePermission();
if (permissionState) {
// 权限被授予
print("ttmn --- flutter android read and write permissions are granted");
} else {
// 权限被拒绝 打开手机上该App的权限设置页面
openAppSettings();
}
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
asyncCallback = () async {
bool enableLogCallback =
SettingParamsSingleton.instance.uploadLogCallback;
bool enableLocalLogPrint = SettingParamsSingleton.instance.localLogPrint;
bool enableImageXCloudControl =
SettingParamsSingleton.instance.imageXCloudControl;
String deviceId = SettingParamsSingleton.instance.deviceID;
EnvOptions options = EnvOptions(
appID: '512559',
appChannel: 'appChannel',
appVersion: '1.0.0',
appName: "FlutterUploadDemo",
openAppLog: true,
autoStartAppLog: true,
debugLogLevel: true,
enableLocalLogPrint: enableLocalLogPrint,
enableLogCallback: enableLogCallback,
useImageXCloudControl: enableImageXCloudControl,
deviceID: deviceId.isEmpty ? 'bd_flutter_upload_demo' : deviceId,
);
await setLogCallback((msg, level) {
print('log callback------------>: $msg, $level');
});
print("ttmn --- flutter initOptions = ${jsonEncode(options)}");
try {
await initEnv(options);
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
};
SettingParamsSingleton.instance.addLoadingCallback(asyncCallback!);
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
// _platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3, // 标签数量
child: Scaffold(
floatingActionButton: Builder(
// ✅ 包裹按钮获取有效上下文
builder:
(innerContext) => FloatingActionButton(
onPressed: () {
Navigator.push(
innerContext,
MaterialPageRoute(builder: (_) => SettingsPage()),
);
},
child: const Icon(Icons.settings),
),
),
appBar: AppBar(
title: const Text('Upload SDK Demo'),
bottom: TabBar(
tabs: PageType.values.map((e) => Tab(text: e.name)).toList(),
),
),
body: TabBarView(
children:
PageType.values.map((e) {
return UploaderPage(type: e);
}).toList(),
),
),
),
);
}
@override
void dispose() {
if (asyncCallback != null) {
SettingParamsSingleton.instance.removeLoadingCallback(asyncCallback!);
}
super.dispose();
}
}