augnito_flutter_sdk 0.0.1 augnito_flutter_sdk: ^0.0.1 copied to clipboard
Use the Augnito Flutter SDK to enable Text To Speech and voice commands into a Flutter application.
import 'package:augnito_flutter_sdk/config/augnito_api_server.dart';
import 'package:augnito_flutter_sdk/config/augnito_config.dart';
import 'package:augnito_flutter_sdk/dictation/dictation_manager.dart';
import 'package:augnito_flutter_sdk/dictation/models/action_recipe.dart';
import 'package:augnito_flutter_sdk/dictation/models/speech_to_text_output.dart';
import 'package:augnito_flutter_sdk/dictation/support/dictation_error.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';
Future main() async {
await dotenv.load(fileName: ".env");
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _status = "offline";
late DictationManager _dictationManager;
final AugnitoConfig _config = AugnitoConfig(
AugnitoAPIServer.india,
dotenv.env['ACCOUNT_CODE'] ?? "",
dotenv.env['ACCESS_KEY'] ?? "",
dotenv.env['LMID'] ?? "",
dotenv.env['USER_TAG'] ?? "",
sourceApp: dotenv.env['SOURCE_APP'] ?? "fluttersdkexample");
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
_dictationManager = DictationManager(_config,
onConnected: _onConnected,
onDisconnected: _onDisconnected,
onError: _onError,
onPartialResult: _onPartialResult,
onFinalResult: _onFinalResult,
onCommandResult: _onCommand,
enableLogs: true);
}
void _onPartialResult(String hypothesis) {
print("partial result: $hypothesis");
}
void _onFinalResult(String transcription) {
print("final result: $transcription");
}
void _onCommand(ActionRecipe actionRecipe) {
print("command: ${actionRecipe.name}");
print(actionRecipe.toString());
}
void _onConnected() {
setState(() {
_status = "online";
});
}
void _onDisconnected() {
setState(() {
_status = "offline";
});
}
void _onError(DictationError error) {
print("error ${error.errorMessage}");
}
void _onToggleDictation() {
_dictationManager.toggleDictation();
}
Future<void> _requestMicrophone() async {
Permission.microphone.request();
}
@override
void dispose() {
_dictationManager.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Augnito SDK Test',
),
Text(
'server connection: $_status',
),
ElevatedButton(
onPressed: _requestMicrophone,
child: const Text("Request Microphone permission")),
ElevatedButton(
onPressed: _onToggleDictation,
child: const Text("Toggle Dictation")),
],
),
),
),
);
}
}