aiagent_ohos 0.0.1
aiagent_ohos: ^0.0.1 copied to clipboard
HarmonyOS 小艺智能体 Flutter 插件,通过 PlatformView 嵌入原生 FunctionComponent,在 Flutter 应用中一键唤起智能体对话。仅支持 Flutter OHOS 平台。
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:aiagent_ohos/aiagent_ohos.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _aiagentOhosPlugin = AiagentOhos();
String _platformVersion = 'Unknown';
bool _isAgentSupported = false;
bool _isChecking = false;
// TODO: 替换为你自己的智能体 ID(从华为小艺智能体平台获取)
final String _agentId = 'agentd32f1ff2a2864e1daf50959a51e336cc';
@override
void initState() {
super.initState();
_initPlatformState();
}
Future<void> _initPlatformState() async {
String platformVersion;
try {
platformVersion =
await _aiagentOhosPlugin.getPlatformVersion() ?? 'Unknown';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
// 检查智能体是否可用
_checkAgentSupport();
}
Future<void> _checkAgentSupport() async {
setState(() {
_isChecking = true;
});
try {
final isSupported = await _aiagentOhosPlugin.isAgentSupport(_agentId);
if (mounted) {
setState(() {
_isAgentSupported = isSupported;
_isChecking = false;
});
}
} on PlatformException catch (e) {
debugPrint('检查智能体支持状态失败: ${e.message}');
if (mounted) {
setState(() {
_isAgentSupported = false;
_isChecking = false;
});
}
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorSchemeSeed: const Color(0xFF1AD0F1),
useMaterial3: true,
),
home: Scaffold(
appBar: AppBar(
title: const Text('AI Agent 插件示例'),
centerTitle: true,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 平台信息卡片
_buildInfoCard(),
const SizedBox(height: 20),
// 智能体组件区域
const Text(
'智能体组件',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
if (_isChecking)
const Center(
child: Padding(
padding: EdgeInsets.all(20),
child: CircularProgressIndicator(),
),
)
else if (_isAgentSupported)
_buildAgentSection()
else
_buildNotSupportedCard(),
],
),
),
),
);
}
/// 平台信息卡片
Widget _buildInfoCard() {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'平台信息',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary,
),
),
const SizedBox(height: 8),
Text('运行平台: $_platformVersion'),
const SizedBox(height: 4),
Text('Agent ID: $_agentId'),
const SizedBox(height: 4),
Row(
children: [
const Text('智能体状态: '),
if (_isChecking)
const SizedBox(
width: 14,
height: 14,
child: CircularProgressIndicator(strokeWidth: 2),
)
else
Text(
_isAgentSupported ? '可用' : '不可用',
style: TextStyle(
color: _isAgentSupported ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
],
),
],
),
),
);
}
/// 智能体组件区域
Widget _buildAgentSection() {
return Column(
children: [
// 基础用法
_buildAgentCard(
title: '基础用法',
description: '默认配置的智能体按钮',
options: AiAgentOptions(
agentId: _agentId,
title: '智能助手',
queryText: '你好,有什么可以帮您的?',
),
),
const SizedBox(height: 16),
// 自定义配置
_buildAgentCard(
title: '自定义配置',
description: '自定义标题和默认问题',
options: AiAgentOptions(
agentId: _agentId,
title: '育儿顾问',
queryText: '请问宝宝辅食怎么添加?',
isShowShadow: true,
titleColors: const ['#FF6B6B', '#FFE66D'],
),
),
],
);
}
/// 单个智能体卡片
Widget _buildAgentCard({
required String title,
required String description,
required AiAgentOptions options,
}) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
description,
style: TextStyle(fontSize: 13, color: Colors.grey[600]),
),
const SizedBox(height: 12),
SizedBox(
width: 130,
height: 40,
child: AiAgentView(
options: options,
onAgentDialogOpened: () {
debugPrint('[$title] 智能体对话框已打开');
},
onAgentDialogClosed: () {
debugPrint('[$title] 智能体对话框已关闭');
},
onError: (error) {
debugPrint('[$title] 错误: $error');
},
),
),
],
),
),
);
}
/// 不支持提示卡片
Widget _buildNotSupportedCard() {
return Card(
color: Colors.orange[50],
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Icon(Icons.warning_amber_rounded, color: Colors.orange[700]),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'智能体暂不可用',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.orange[800],
),
),
const SizedBox(height: 4),
Text(
'请确认 Agent ID 是否正确,或当前设备是否支持智能体功能。',
style: TextStyle(fontSize: 13, color: Colors.orange[700]),
),
],
),
),
],
),
),
);
}
}