最新Flutter 微信分享功能实现(1)

文章介绍了如何使用Flutter中的WxSdk库进行微信分享,包括分享图片、文本、视频和链接,以及如何下载图片至本地。内容涵盖了WeChatShare接口的使用方法和下载文件的相关代码示例。
摘要由CSDN通过智能技术生成

minWidth: 128,

quality: 20,

// rotate: 135,

);

WxSdk.ShareUrl(

//分享链接

“你的链接”,

scene: 1,

thumbFile: imagePath,

desc: “描述”,

title: “标题”,

);

封装的工具类

import ‘dart:io’;

import ‘dart:typed_data’;

import ‘check.dart’;

import ‘package:fluwx_no_pay/fluwx_no_pay.dart’ as fluwx;

class WxSdk {

// static bool wxIsInstalled;

static Future init() async {

fluwx.registerWxApi(

appId: “你的appid”,

doOnAndroid: true,

doOnIOS: true,

universalLink: “你的universalLink”);

}

static Future wxIsInstalled() async {

return await fluwx.isWeChatInstalled;

}

/**

  • 分享图片到微信,

  • file=本地路径

  • url=网络地址

  • asset=内置在app的资源图片

  • scene=分享场景,1好友会话,2朋友圈,3收藏

*/

static void ShareImage(

{String title,

String decs,

String file,

String url,

String asset,

int scene = 1}) async {

fluwx.WeChatScene wxScene = fluwx.WeChatScene.SESSION;

if (scene == 2) {

wxScene = fluwx.WeChatScene.TIMELINE;

} else if (scene == 3) {

wxScene = fluwx.WeChatScene.FAVORITE;

}

fluwx.WeChatShareImageModel model = null;

if (file != null) {

model = fluwx.WeChatShareImageModel(fluwx.WeChatImage.file(File(file)),

title: title, description: decs, scene: wxScene);

} else if (url != null) {

model = fluwx.WeChatShareImageModel(fluwx.WeChatImage.network(url),

title: title, description: decs, scene: wxScene);

} else if (asset != null) {

model = fluwx.WeChatShareImageModel(fluwx.WeChatImage.asset(asset),

title: title, description: decs, scene: wxScene);

} else {

throw Exception(“缺少图片资源信息”);

}

fluwx.shareToWeChat(model);

}

/**

  • 分享文本

  • content=分享内容

  • scene=分享场景,1好友会话,2朋友圈,3收藏

*/

static void ShareText(String content, {String title, int scene = 1}) {

fluwx.WeChatScene wxScene = fluwx.WeChatScene.SESSION;

if (scene == 2) {

wxScene = fluwx.WeChatScene.TIMELINE;

} else if (scene == 3) {

wxScene = fluwx.WeChatScene.FAVORITE;

}

fluwx.WeChatShareTextModel model =

fluwx.WeChatShareTextModel(content, title: title, scene: wxScene);

fluwx.shareToWeChat(model);

}

/***

  • 分享视频

  • videoUrl=视频网上地址

  • thumbFile=缩略图本地路径

  • scene=分享场景,1好友会话,2朋友圈,3收藏

*/

static void ShareVideo(String videoUrl,

{String thumbFile, String title, String desc, int scene = 1}) {

fluwx.WeChatScene wxScene = fluwx.WeChatScene.SESSION;

if (scene == 2) {

wxScene = fluwx.WeChatScene.TIMELINE;

} else if (scene == 3) {

wxScene = fluwx.WeChatScene.FAVORITE;

}

fluwx.WeChatImage image = null;

if (thumbFile != null) {

image = fluwx.WeChatImage.file(File(thumbFile));

}

var model = fluwx.WeChatShareVideoModel(

videoUrl: videoUrl,

thumbnail: image,

title: title,

description: desc,

scene: wxScene);

fluwx.shareToWeChat(model);

}

/**

  • 分享链接

  • url=链接

  • thumbFile=缩略图本地路径

  • scene=分享场景,1好友会话,2朋友圈,3收藏

*/

static void ShareUrl(String url,

{String thumbFile,

Uint8List thumbBytes,

String title,

String desc,

int scene = 1,

String networkThumb,

String assetThumb}) {

desc = desc ?? “”;

title = title ?? “”;

if (desc.length > 54) {

desc = desc.substring(0, 54) + “…”;

}

if (title.length > 20) {

title = title.substring(0, 20) + “…”;

}

fluwx.WeChatScene wxScene = fluwx.WeChatScene.SESSION;

if (scene == 2) {

wxScene = fluwx.WeChatScene.TIMELINE;

} else if (scene == 3) {

wxScene = fluwx.WeChatScene.FAVORITE;

}

fluwx.WeChatImage image = null;

if (thumbFile != null) {

image = fluwx.WeChatImage.file(File(thumbFile));

} else if (thumbBytes != null) {

image = fluwx.WeChatImage.binary(thumbBytes);

} else if (strNoEmpty(networkThumb)) {

image = fluwx.WeChatImage.network(Uri.encodeFull(networkThumb));

} else if (strNoEmpty(assetThumb)) {

image = fluwx.WeChatImage.asset(assetThumb, suffix: “.png”);

}

var model = fluwx.WeChatShareWebPageModel(

url,

thumbnail: image,

title: title,

description: desc,

scene: wxScene,

);

fluwx.shareToWeChat(model);

}

}

check.dart

/// 字符串不为空

bool strNoEmpty(String value) {

if (value == null) return false;

return value.trim().isNotEmpty;

}

/// 字符串不为空

bool mapNoEmpty(Map value) {

if (value == null) return false;

return value.isNotEmpty;

}

///判断List是否为空

bool listNoEmpty(List list) {

if (list == null) return false;

if (list.length == 0) return false;

return true;

}

//下载图片

import ‘dart:convert’;

import ‘dart:io’;

import ‘dart:typed_data’;

import ‘package:dio/dio.dart’;

import ‘package:flutter/cupertino.dart’;

import ‘package:path_provider/path_provider.dart’;

class LocalImageCache {

static LocalImageCache instance = LocalImageCache();

String _tmepPath = “”;

void init() async {

var tempDir = await getTemporaryDirectory();

_tmepPath = tempDir.path;

}

/**

  • 直接下载图片到本地临时目录

  • ios必须带有后缀,不然exists会永远=false

*/

Future download(BuildContext context, String url,{String ext = “”}) async {

try {

var dio = await Dio()

.get(url, options: Options(responseType: ResponseType.bytes));

最后的最后

对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的

最后,互联网不存在所谓的寒冬,只是你没有努力罢了!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

pe: ResponseType.bytes));

最后的最后

对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的

[外链图片转存中…(img-nVou5VzV-1714655507916)]

最后,互联网不存在所谓的寒冬,只是你没有努力罢了!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值