升级微博SDK

This commit is contained in:
v7lin 2022-07-26 11:29:33 +08:00
parent 068f5e2ab2
commit f306734e7b

View File

@ -1,8 +1,12 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:image/image.dart' as imglib;
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:weibo_kit/weibo_kit.dart'; import 'package:weibo_kit/weibo_kit.dart';
import 'package:weibo_kit_example/api/model/weibo_api_resp.dart'; import 'package:weibo_kit_example/api/model/weibo_api_resp.dart';
import 'package:weibo_kit_example/api/weibo_api.dart'; import 'package:weibo_kit_example/api/weibo_api.dart';
@ -34,6 +38,7 @@ class Home extends StatefulWidget {
const Home({ const Home({
super.key, super.key,
}); });
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
return _HomeState(); return _HomeState();
@ -90,8 +95,7 @@ class _HomeState extends State<Home> {
ListTile( ListTile(
title: Text('环境检查'), title: Text('环境检查'),
onTap: () async { onTap: () async {
final String content = final String content = 'weibo: ${await Weibo.instance.isInstalled()}';
'weibo: ${await Weibo.instance.isInstalled()}';
_showTips('环境检查', content); _showTips('环境检查', content);
}, },
), ),
@ -108,18 +112,15 @@ class _HomeState extends State<Home> {
title: Text('用户信息'), title: Text('用户信息'),
onTap: () async { onTap: () async {
if (_authResp?.isSuccessful ?? false) { if (_authResp?.isSuccessful ?? false) {
final WeiboUserInfoResp userInfoResp = final WeiboUserInfoResp userInfoResp = await WeiboApi.getUserInfo(
await WeiboApi.getUserInfo(
appkey: _WEIBO_APP_KEY, appkey: _WEIBO_APP_KEY,
userId: _authResp!.userId!, userId: _authResp!.userId!,
accessToken: _authResp!.accessToken!, accessToken: _authResp!.accessToken!,
); );
if (userInfoResp.isSuccessful) { if (userInfoResp.isSuccessful) {
_showTips('用户信息', _showTips('用户信息', '${userInfoResp.screenName}\n${userInfoResp.description}\n${userInfoResp.location}\n${userInfoResp.profileImageUrl}');
'${userInfoResp.screenName}\n${userInfoResp.description}\n${userInfoResp.location}\n${userInfoResp.profileImageUrl}');
} else { } else {
_showTips('用户信息', _showTips('用户信息', '获取用户信息失败\n${userInfoResp.errorCode}:${userInfoResp.error}');
'获取用户信息失败\n${userInfoResp.errorCode}:${userInfoResp.error}');
} }
} }
}, },
@ -135,32 +136,43 @@ class _HomeState extends State<Home> {
ListTile( ListTile(
title: Text('图片分享'), title: Text('图片分享'),
onTap: () async { onTap: () async {
final File file = await DefaultCacheManager().getSingleFile( File file = await DefaultCacheManager().getSingleFile('https://www.baidu.com/img/bd_logo1.png?where=super');
'https://www.baidu.com/img/bd_logo1.png?where=super'); if (Platform.isAndroid) {
await Weibo.instance.shareImage( // Context.getExternalFilesDir(null)/Context.getExternalCacheDirs(null)
// path_provider.getExternalCacheDirectories();
// path_provider.getExternalStorageDirectory();
final Directory temporaryDir = await path_provider.getTemporaryDirectory();
if (path.isWithin(temporaryDir.parent.path, file.path)) {
//
final File copyFile = File(path.join((await path_provider.getExternalStorageDirectory())!.path, path.basename(file.path)));
if (copyFile.existsSync()) {
await copyFile.delete();
}
await copyFile.writeAsBytes(await file.readAsBytes());
file = copyFile;
}
}
await Weibo.instance.shareMultiImage(
text: 'Share Text', text: 'Share Text',
imageUri: Uri.file(file.path), imageUris: <Uri>[Uri.file(file.path)],
); );
}, },
), ),
ListTile( ListTile(
title: Text('网页分享'), title: Text('网页分享'),
onTap: () async { onTap: () async {
// final File file = await DefaultCacheManager().getSingleFile( final File file = await DefaultCacheManager().getSingleFile('https://www.baidu.com/img/bd_logo1.png?where=super');
// 'https://www.baidu.com/img/bd_logo1.png?where=super'); final imglib.Image thumbnail = imglib.decodeImage(file.readAsBytesSync())!;
// final image.Image thumbnail = Uint8List thumbData = thumbnail.getBytes();
// image.decodeImage(file.readAsBytesSync())!; if (thumbData.length > 32 * 1024) {
// Uint8List thumbData = thumbnail.getBytes(); thumbData = Uint8List.fromList(imglib.encodeJpg(thumbnail, quality: 100 * 32 * 1024 ~/ thumbData.length));
// if (thumbData.length > 32 * 1024) { }
// thumbData = Uint8List.fromList(image.encodeJpg(thumbnail, await Weibo.instance.shareWebpage(
// quality: 100 * 32 * 1024 ~/ thumbData.length)); title: 'title',
// } description: 'share webpage',
// await Weibo.instance.shareWebpage( thumbData: thumbData.buffer.asUint8List(),
// title: 'title', webpageUrl: 'https://www.baidu.com',
// description: 'share webpage', );
// thumbData: thumbData.buffer.asUint8List(),
// webpageUrl: 'https://www.baidu.com',
// );
}, },
), ),
], ],