Flutter钉钉集成插件flu_dingtalk的使用
Flutter钉钉集成插件flu_dingtalk的使用
Getting Started(开始使用)
此项目是一个新的Flutter插件项目。它包含了针对Android和/或iOS的平台特定实现代码。
对于如何开始Flutter开发的帮助,可以查看在线文档,其中提供了教程、示例、移动开发指导以及完整的API引用。
使用示例
以下是flu_dingtalk
插件的基本用法示例:
import 'package:flu_dingtalk/flu_dingtalk.dart';
import 'package:flu_dingtalk/flu_dingtalk_plugin_api.dart';
import 'package:flu_dingtalk/response/dingtalk_response.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({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: const HomeWidget(),
),
);
}
}
class HomeWidget extends StatefulWidget {
const HomeWidget({super.key});
[@override](/user/override)
State<HomeWidget> createState() => _HomeWidgetState();
}
class _HomeWidgetState extends State<HomeWidget> {
String _platformVersion = 'Unknown';
String _registerApp = 'Unknown';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
FluDingtalk.ddResponseEventHandler.listen(dingtalkResponseEvent);
}
[@override](/user/override)
void dispose() {
super.dispose();
FluDingtalk.dispose();
}
// 初始化平台状态
Future<void> initPlatformState() async {
bool registerApp = await FluDingtalk.registerApp(
'dingcxbptovsyu1lmth0', 'com.example.flu_dingtalk_example');
String platformVersion;
// 处理平台消息可能失败的情况
try {
platformVersion = await FluDingtalk.openAPIVersion();
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// 如果小部件在异步平台消息飞行时从树中移除,则丢弃回复
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
_registerApp = 'registerApp: $registerApp';
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Text('版本号: $_platformVersion'),
Text(_registerApp),
TextButton(
onPressed: () {
FluDingtalk.isDingDingInstalled().then((value) {
_showDialog("是否安装了钉钉: $value");
});
},
child: const Text('检查钉钉是否安装'),
),
TextButton(
onPressed: () {
FluDingtalk.isDingTalkSupportSSO().then((value) {
_showDialog("是否支持登录: $value");
});
},
child: const Text('检查是否支持SSO登录'),
),
TextButton(
onPressed: () {
FluDingtalk.openDDApp().then((value) {
_showDialog("打开钉钉App: $value");
});
},
child: const Text('打开钉钉App'),
),
TextButton(
onPressed: () {
FluDingtalk.sendAuth(
DTAuthReq(redirectURI: "https://www.yymimang.cn"),
).then((value) {
// _showDialog("打开钉钉登录: $value");
}).catchError((error) {
_showDialog(error.toString());
});
},
child: const Text('发送钉钉授权请求'),
),
TextButton(
onPressed: () {
FluDingtalk.sendTextMessage('分享文本');
},
child: const Text('分享文本'),
),
TextButton(
onPressed: () {
FluDingtalk.sendImageMessage(
picUrl:
'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png');
},
child: const Text('分享图片'),
),
TextButton(
onPressed: () {
FluDingtalk.sendWebPageMessage(
'https://www.baidu.com/',
title: '标题',
content: '描述2333',
thumbUrl:
'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
);
},
child: const Text('分享网页'),
),
],
),
);
}
// 显示对话框
void _showDialog(String msg) {
showDialog(
context: context,
builder: (BuildContext context) {
return _buildDialog(msg);
},
);
}
// 构建对话框
Widget _buildDialog(String content) {
return AlertDialog(
content: Row(
children: [
Expanded(
child: Text(content),
),
const CloseButton(),
],
),
);
}
// 钉钉回调监听
void dingtalkResponseEvent(BaseDDResponse response) {
if (response is DDShareResponse) {
_showDialog("DDShareResponse: ${response.shareResult}");
} else if (response is DDAuthResponse) {
_showDialog("DDAuthResponse: ${response.accessCode}");
} else {
_showDialog("BaseDDResponse: ${response.mErrCode} - ${response.mErrStr}");
}
}
}
更多关于Flutter钉钉集成插件flu_dingtalk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter钉钉集成插件flu_dingtalk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flu_dingtalk
是一个用于在 Flutter 应用中集成钉钉登录和分享功能的插件。通过这个插件,开发者可以轻松地在 Flutter 应用中实现钉钉的授权登录、分享内容到钉钉等功能。
以下是使用 flu_dingtalk
插件的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 flu_dingtalk
插件的依赖:
dependencies:
flutter:
sdk: flutter
flu_dingtalk: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 配置钉钉开发者账号
在使用 flu_dingtalk
之前,你需要在钉钉开放平台注册一个应用,并获取应用的 AppKey
和 AppSecret
。
- 登录 钉钉开放平台。
- 创建一个应用并获取
AppKey
和AppSecret
。 - 配置应用的
回调地址
,用于接收授权后的回调信息。
3. 初始化插件
在 main.dart
或其他合适的地方初始化 flu_dingtalk
插件:
import 'package:flu_dingtalk/flu_dingtalk.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化钉钉插件
await FluDingtalk.init(
appKey: 'your_app_key', // 替换为你的 AppKey
appSecret: 'your_app_secret', // 替换为你的 AppSecret
);
runApp(MyApp());
}
4. 实现钉钉登录
使用 FluDingtalk
提供的 API 实现钉钉登录功能:
import 'package:flu_dingtalk/flu_dingtalk.dart';
void loginWithDingtalk() async {
try {
final authResult = await FluDingtalk.login();
print('登录成功: ${authResult.toJson()}');
// 处理登录成功后的逻辑
} catch (e) {
print('登录失败: $e');
// 处理登录失败的逻辑
}
}
5. 实现钉钉分享
使用 FluDingtalk
提供的 API 实现分享功能:
import 'package:flu_dingtalk/flu_dingtalk.dart';
void shareToDingtalk() async {
try {
final shareResult = await FluDingtalk.share(
title: '分享标题',
content: '分享内容',
url: 'https://example.com',
);
print('分享成功: $shareResult');
// 处理分享成功后的逻辑
} catch (e) {
print('分享失败: $e');
// 处理分享失败的逻辑
}
}
6. 处理回调
在 Android 平台上,你需要在 AndroidManifest.xml
中配置钉钉的回调 Activity:
<activity
android:name="com.dingtalk.open.app.demo.DDShareActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="dingtalk" />
</intent-filter>
</activity>
在 iOS 平台上,你需要在 Info.plist
中添加钉钉的 URL Scheme:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>dingtalk</string>
</array>
</dict>
</array>