Flutter Speech To Text 从底部弹出

问题描述

我能够实现这个 speech to text。但是有一些功能我无法实现。我在应用栏中有一个文本字段作为标题小部件,文本字段右侧有一个 mic 图标堆栈,单击图标后,打开一个底部工作表,语音到文本的逻辑就在其中,在中间显示识别的文本。收到结果后,用识别的单词调用 navigator.pop() 返回,并在文本字段中显示结果。

问题
Android - 只能成功执行一次,第二次开始听,语音转文字后卡在底部显示,没有弹回。
iOS - 开始听后第一次尝试直接显示黑屏,似乎弹出两次,但我无法进行任何调试

嫌疑人
尝试在 Android 上进行调试,发现第二次单击 mic 图标时,语音转文本逻辑中的上下文为空,但我找不到确切原因,在哪个上下文中弄乱。另外,我的项目中只有一个素材应用程序(iOS案例)

感谢任何帮助。


Main.dart

return MaterialApp(
  home: Scaffold(
    appBar: AppBar(
      title: SearchProductBar(),),body: Container()
  ),);

SearchProductBar.dart

Widget build(BuildContext context) {
return Stack(
  children: [
    //text field code
    Positioned.fill(
      child: Align(
        alignment: Alignment.centerRight,child: Padding(
          padding: EdgeInsets.only(right: 8.0),child: IconButton(
            icon: Icon(Icons.check),onpressed: () async {
              String audioText = await showModalBottomSheet(
                  context: context,builder: (BuildContext context) {
                    return SpeechToTextView(
                      duration: 3,);
                  });

              if (audioText != null && audioText.isNotEmpty) {
                _inputController.text = audioText;
                // _focusNode.requestFocus();
                print('text: $audioText');
              }
            },],);
}

SpeechToText.dart

//default fields from link (speech to text)
bool _hasspeech = false;
String lastWords = "Tap on mic to start listening";
String lastError = "";
String _currentLocaleId = 'en_GB'; //default to english
stt.SpeechToText speech = stt.SpeechToText();
bool _listenStart = false;

@override
void initState() {
  super.initState();
  initSpeech().then((result) {
    if (!result) Navigator.pop(context);
  });
}

Future<bool> initSpeech() async {
bool hasspeech = await speech.initialize(
    onError: errorListener,onStatus: statusListener);
if (!mounted) return false;
setState(() {
  _hasspeech = hasspeech;
});
if (hasspeech) {
  return true;
} else {
  return false;
}
}
 
void startListening() {
lastWords = "";
lastError = "";
speech.listen(
    onResult: resultListener,listenFor: Duration(seconds: widget.duration),localeId: _currentLocaleId,onSoundLevelChange: soundLevelListener,cancelOnError: true,listenMode: stt.ListenMode.confirmation);
    _listenStart = true;
setState(() {});
}

void resultListener(SpeechRecognitionResult result) {
setState(() {
  lastWords = "${result.recognizedWords}";
});
}

void statusListener(String status) {
if(_listenStart)
if (status == 'notListening') {
  _listenStart = false;
  Future.delayed(
    Duration(
      seconds: 1,).whenComplete(
    () => Navigator.pop(context,lastWords),);
}
}

@override
Widget build(BuildContext context) {
return Container(
  height: 250,decoration: Boxdecoration(
    color: Theme.of(context).scaffoldBackgroundColor,borderRadius: BorderRadius.only(
      topLeft: Radius.circular(20),topRight: Radius.circular(20),child: Column(
    mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[
      Row(
        mainAxisAlignment: MainAxisAlignment.start,children: <Widget>[
          SizedBox(
            height: 10,width: 10,SizedBox(
            width: 5,Text(
            'en',style: TextStyle(
              fontSize: 12,color: Colors.grey,)
        ],Spacer(),Text(
        lastWords,style: TextStyle(
          fontSize: 16,Container(
        width: 60,height: 60,alignment: Alignment.center,decoration: Boxdecoration(
          borderRadius: BorderRadius.all(Radius.circular(50)),BoxShadow: [
            BoxShadow(
                blurRadius: .26,spreadRadius: level * 1.5,color: Colors.black.withOpacity(.05))
          ],child: IconButton(
          iconSize: 40,icon: speech.isListening ? Icon(Icons.mic_none) : Icon(Icons.mic),onpressed: () => !_hasspeech || speech.isListening
              ? null
              : startListening(),SizedBox(
        height: 10,);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)