In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you how to write the sample code that Flutter implements the sharing function of imitating Wechat. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Flutter is used to quickly develop Android iOS platform applications. In Flutter, Wechat sharing function is realized through fluwx or fluwx_no_pay plug-ins.
Mainly depends on their own needs, in this example I will follow the implementation of non-payment. As for why, the main reason is that ios packaging is more troublesome.
Then let's take a look at how to achieve it.
1. First, go to the official website of pub.
Https://pub.flutter-io.cn/
Find these two bags
Fluwx_no_pay
Or
Fluwx
There are two ways to install:
Method 1:
Flutter pub add fluwx_no_pay
Method 2:
Dependencies: fluwx_no_pay: ^ 3.6.1
Then import it when you use it
Import 'package:fluwx_no_pay/fluwx_no_pay.dart'
Although it integrates a lot of functions
But we only do shared demos.
2 sign up for a developer account and create your application on the Wechat open platform
Links to Wechat Open platform
Development platform documentation
After the creation of the application, fill in the basic application information, and submit it to the Wechat platform for review and approval.
Get the AppID from here, and then get the Universal Links of the configured iOS platform. As for how to get it, please check the relevant information.
3 initialize @ override void initState () {super.initState (); _ initFluwx ();} Future _ initFluwx () async {await WxSdk.init ();} 3.2 on the sharing page 3.1 to check whether Wechat is installed.
If you share when you click the button, check before sharing.
Bool _ wxIsInstalled = false; void _ checkWx () async {_ wxIsInstalled = await WxSdk.wxIsInstalled (); refreshUI ();} 3.3 share Wechat messages String imagePath;imagePath = await LocalImageCache.instance .download (context, widget.cjinfo.cover, ext: ".jpg") / / compress the picture Flutter_image_compress Uint8List image = await FlutterImageCompress.compressWithFile (imagePath, minHeight: 128, minWidth: 128, quality: 20) / / rotate: 135,) WxSdk.ShareUrl (/ / share link "your link", scene: 1, thumbFile: imagePath, desc: "description" Title: "title",)
Encapsulated tool class
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: "your appid", doOnAndroid: true, doOnIOS: true, universalLink: "your universalLink");} static Future wxIsInstalled () async {return await fluwx.isWeChatInstalled } / * share pictures to Wechat, * file= local path * url= network address * asset= built-in app resource picture * scene= sharing scene, 1 friend session, 2 moments 3 Collection * / 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 ("missing picture resource information") } fluwx.shareToWeChat (model);} / * * sharing text * content= sharing content * scene= sharing scene, 1 friend conversation, 2 moments, 3 favorites * / 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) } / * sharing video * videoUrl= video online address * thumbFile= thumbnail local path * scene= sharing scene, 1 friend conversation, 2 moments, 3 favorites * / 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) } / * sharing link * url= link * thumbFile= thumbnail local path * scene= sharing scene, 1 friend session, 2 moments, 3 favorites * / 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
/ string is not empty bool strNoEmpty (String value) {if (value = = null) return false; return value.trim (). IsNotEmpty;} / / string is not empty bool mapNoEmpty (Map value) {if (value = = null) return false; return value.isNotEmpty;} / determine whether List is empty bool listNoEmpty (List list) {if (list = = null) return false; if (list.length = = 0) return false; return true;}
/ / download pictures
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 } / * download pictures directly to the local temporary directory * ios must have a suffix, otherwise exists will always = false * / Future download (BuildContext context, String url, {String ext = "}) async {try {var dio = await Dio () .get (url, options: Options (responseType: ResponseType.bytes)); Uint8List image = Uint8List.fromList (dio.data); return save (image, url,ext: ext) } catch (ex) {print ("image download error:$ {ex.toString ()}"); return null;} main problem
Wechat is not installed
Ios is not configured with whitelist
The picture is too big (so I used compression technology) 32k
Development platform documentation
The above is the editor for you to share the Flutter implementation of imitation Wechat sharing function sample code how to write, if you happen to have similar doubts, you may wish to refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.