Skip to content

Commit

Permalink
feat: simple reproduce for web issues #242
Browse files Browse the repository at this point in the history
  • Loading branch information
sowens-csd committed Dec 10, 2021
1 parent 2a0ab3b commit 78f05f4
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions speech_to_text/example/lib/repro.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import 'package:flutter/material.dart';
import 'dart:html' as html;
import 'dart:js_util' as js_util;

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String _recognized = '';
html.SpeechRecognition? _webSpeech;

@override
void initState() {
super.initState();
if (html.SpeechRecognition.supported) {
_webSpeech = html.SpeechRecognition();
}
}

void _incrementCounter() {
_webSpeech?.stop();
_webSpeech?.onResult.listen((speechEvent) => _onResult(speechEvent));
_webSpeech?.interimResults = true;
_webSpeech?.continuous = true;
_webSpeech?.start();
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
Text(
'You said: $_recognized',
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}

void _onResult(html.SpeechRecognitionEvent event) {
var results = event.results;
if (null == results) return;
for (html.SpeechRecognitionResult recognitionResult in results) {
if (null == recognitionResult.length || recognitionResult.length == 0) {
continue;
}
for (var altIndex = 0; altIndex < recognitionResult.length!; ++altIndex) {
html.SpeechRecognitionAlternative alt =
js_util.callMethod(recognitionResult, 'item', [altIndex]);

if (null != alt.transcript && null != alt.confidence) {
setState(() {
_recognized = alt.transcript!;
});
}
}
}
}
}

0 comments on commit 78f05f4

Please sign in to comment.