conva_ai_copilot 0.1.3 conva_ai_copilot: ^0.1.3 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 = '';
String _utteranceText = '';
@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: _utteranceText),
readOnly: true,
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),
child: FloatingActionButton(
onPressed: () {
_startConversation();
},
backgroundColor: Colors.blue,
mini: true,
child: const Icon(Icons.mic, color: Colors.white),
),
),
],
),
Container(height: 30),
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),
),
),
))))
],
))));
}
void _startConversation() {
ConvaAICopilot.startConversation();
}
@override
void onCopilotInitFailure(String description) {
// Calls when copilot failed to initialize
}
@override
void onCopilotInitSuccess() {
// 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();
_utteranceText = suggestion.displayText;
} 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();
_utteranceText = response.input;
} catch (e) {
// Handle Exception
}
});
}
}