speech 0.0.2 speech: ^0.0.2 copied to clipboard
This plugin provides a method that returns a stream. While listening to it, it records audio and sends it to the Google speech api, which then yields a transcript of the audio input. As argument the f [...]
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:speech/speech.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _text = "";
int _index = 0;
StreamSubscription<dynamic> listener;
var _icon = [
new Icon(Icons.keyboard_voice),
new Icon(Icons.stop),
];
var _color = [
Colors.cyan,
Colors.red
];
void _buttonPressed() async {
if (_index == 0) listener = transcript("test").listen((data) => _append(data));
else listener.cancel();
_switchButton();
}
void _switchButton() => setState(() => _index = (_index + 1) % 2);
void _append(String text) => setState(() => _text += text);
@override
void initState() {
super.initState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
transcript("AIzaSyBoB1RpCSdJVz1MU55zLcBtC69BYrwQu3o");
if (!mounted) return;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Life Transcript'),
backgroundColor: Colors.cyan,
),
body: Center(
child: new Text(_text),
),
floatingActionButton: FloatingActionButton(
child: _icon[_index],
backgroundColor: _color[_index],
onPressed: _buttonPressed,
),
),
);
}
}