conva_ai_copilot 0.1.2 conva_ai_copilot: ^0.1.2 copied to clipboard
The client library for adding and interacting with ConvaAI Copilot.
import 'dart:convert';
import 'package:conva_ai_copilot/conva_ai_copilot.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MaterialApp(
home: MyApp(),
debugShowCheckedModeBanner: false,
));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp>
implements
ConvaAIHandler,
ConvaAISuggestionHandler,
ConvaAILifeCycleObserver {
String _displayText = '';
@override
void initState() {
super.initState();
initConvaAICopilot();
}
void initConvaAICopilot() {
ConvaAI.init(id: "id", key: "key", version: "version");
var assistantConfig = ConvaAIOptions()
..capabilityHandler = this
..suggestionHandler = this
..observer = this;
// Uncomment it to directly handle response and suggestions inline
// ..onCapability = (response, interactionData, isFinal) {
// // Handle the response from the assistant
// }
// ..onSuggestion = (suggestion) {
// // Handle the selected suggestion
// };
ConvaAICopilot.setup(assistantConfig);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('ConvaAI Assistant'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(height: 25),
Row(
children: [
Expanded(
child: Container(
height: 45.0,
margin:
const EdgeInsets.fromLTRB(17.0, 0.0, 10.0, 0.0),
child: TextField(
controller: TextEditingController(text: ""),
decoration: InputDecoration(
labelText: 'Utterance Text',
suffixIcon: GestureDetector(
onTap: () {
Clipboard.setData(
ClipboardData(text: _displayText));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Utterance text copied')),
);
},
child: const Icon(Icons.copy),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: const BorderSide(
color: Colors.blue,
width: 2.0,
),
),
),
)),
),
Container(
height: 60,
width: 60,
margin: const EdgeInsets.only(right: 10.0),
),
],
),
Container(height: 30), // set height
Flexible(
child: FractionallySizedBox(
widthFactor: 0.9,
heightFactor: 0.98,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: Container(
height: MediaQuery.of(context).size.height,
decoration: const BoxDecoration(
shape: BoxShape.rectangle,
color: Colors.black,
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
'$_displayText\n',
style: const TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
))))
],
))));
}
@override
void onCopilotInitFailure(String description) {
// Calls when copilot failed to initialize
}
@override
void onCopilotInitSuccess() {
ConvaAICopilot.setGlobalTrigger();
ConvaAICopilot.attach();
// Calls when copilot initialized successfully
}
@override
void onCopilotInteractionBegin(bool isVoice) {
// Calls when copilot interaction begin
}
@override
void onCopilotInteractionEnd(bool isCanceled) {
// Calls when copilot interaction end
}
@override
void onCopilotSurfaceDismissed() {
// Calls when copilot surface dismissed
}
@override
void onSuggestion(ConvaAISuggestion suggestion) {
setState(() {
try {
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
String suggestionString = encoder.convert(suggestion);
_displayText = suggestionString.toString();
} catch (e) {
// Handle Exception
}
});
}
@override
void onCapability(ConvaAIResponse response,
ConvaAIInteraction interactionData, bool isFinal) {
setState(() {
try {
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
String responseString = encoder.convert(response);
_displayText = responseString.toString();
} catch (e) {
// Handle Exception
}
});
}
}