Flutter实时通信插件livecom_android的使用
Flutter实时通信插件livecom_android的使用
livecom_android
livecom_android
是 livecom
的 Android 实现。
Warning!
如果您遇到错误信息“More then one file was found with OS independent path ‘META-INF/xxxxx’”,请查看 /example/android/app/build.gradle
文件。您需要在应用的 build.gradle
文件中添加以下 packagingOptions
块,类似于该文件中的内容。
使用示例
以下是完整的示例代码,展示如何在 Flutter 应用中使用 livecom_android
插件。
示例代码
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:livecom_android/livecom_android.dart';
import 'package:livecom_platform_interface/livecom_delegate.dart';
// 初始化 LiveCom 插件实例
final _liveComPlugin = LiveComAndroid.shared;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget with LiveComDelegate {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
// 配置 LiveCom 插件
_liveComPlugin.configure(
"f400270e-92bf-4df1-966c-9f33301095b3", // 客户端 ID
"0091FF", // 聊天背景颜色
"#EF5DA8", // 按钮颜色
"#0091FF", // 按钮文本颜色
"#00D1FF", // 聊天框背景颜色
"https://website.com/{video_id}", // 自定义产品页面 URL
"https://website.com/{video_id}?p={product_id}" // 自定义结账页面 URL
);
// 设置委托对象
_liveComPlugin.delegate = this;
log("[LiveCom] configure");
return MaterialApp(
title: 'LiveCom Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter screen'),
);
}
// 监听购物车变化事件
[@override](/user/override)
void onCartChange(List<String> productSKUs) {
log("[LiveCom] onCartChange productSKUs: ${productSKUs.join(", ")}");
}
// 监听商品添加事件
[@override](/user/override)
void onProductAdd(String sku, String streamId) {
log("[LiveCom] onProductAdd sku: $sku stream_id: $streamId");
}
// 监听商品删除事件
[@override](/user/override)
void onProductDelete(String productSKU) {
log("[LiveCom] onProductDelete sku: $productSKU");
}
// 监听打开结账屏幕请求
[@override](/user/override)
void onRequestOpenCheckoutScreen(List<String> productSKUs) {
log("[LiveCom] onRequestOpenCheckoutScreen productSKUs: ${productSKUs.join(", ")}");
}
// 监听打开产品屏幕请求
[@override](/user/override)
void onRequestOpenProductScreen(String sku, String streamId) {
log("[LiveCom] onRequestOpenProductScreen sku: $sku stream_id: $streamId");
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String streamId = "qQMqXx2wy"; // 当前视频流 ID
final TextEditingController _textFieldController =
TextEditingController(text: "qQMqXx2wy"); // 输入流 ID 的控制器
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// 显示视频列表按钮
TextButton(
onPressed: () {
_liveComPlugin.presentStreams(); // 打开视频列表
},
child: const Text("Show video list"),
),
// 输入流 ID 并显示视频
TextButton(
onPressed: () => showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Enter stream id'),
content: TextField(
onChanged: (value) => streamId = value, // 更新流 ID
controller: _textFieldController,
decoration: const InputDecoration(hintText: "stream id"),
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(context, 'OK');
_liveComPlugin.presentStream(streamId); // 打开指定视频
},
child: const Text('OK'),
),
],
),
),
child: const Text("Show video"),
),
// 同时显示视频列表和视频
TextButton(
onPressed: () {
_liveComPlugin.presentStreams(); // 打开视频列表
_liveComPlugin.presentStream(streamId); // 打开指定视频
},
child: const Text("Show list and video"),
),
],
),
),
),
);
}
}
更多关于Flutter实时通信插件livecom_android的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复