0

I'm trying to make an icon in the textfield that is supposed to stop and convert into mic_none when the user has finished saying the text , but that it doesn't happen.

What happens is that the text reception stops, but the icon does not return to its outliend form, but rather I have to click on it to convert it into mic_none.

I would appreciate any help from you

TextEditingController? _directionController;
  String text = "";
  bool isListening = false;
  bool isListening1 = false;
  @required
  Function(String text)? onResult;
  @required
  ValueChanged<bool>? onListening;
  static final _speech = SpeechToText();



void toggleRecording() async {
    if (!isListening1) {
      bool isAval = await _speech.initialize(
        onStatus: (status) => onListening!(_speech.isListening),
        onError: (e) => print('Error: $e'),
      );

      if (isAval) {
        setState(() {
          isListening1 = true;
        });
        // it is for recognaization
        _speech.listen(
            onResult: (value) => setState(() {
                  _directionController!.text = value.recognizedWords;
                  addRecipe.userDirections[widget.index] =
                      value.recognizedWords;
                  // onResult!(value.recognizedWords);
                }));
      }
    } else {
      setState(() {
        isListening1 = false;
        _speech.stop();
      });
    }
  }

Here is the build of my code:

Widget build(BuildContext context) {
    // run this method when the interface has been loaded
    WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
      _directionController!.text = addRecipe.userDirections[widget.index] ?? '';
    });

    return TextFormField(
      controller: _directionController,
      decoration: InputDecoration(
          suffixIcon: IconButton(
            onPressed: toggleRecording,
            icon: Icon(
              isListening1 ? Icons.mic : Icons.mic_none,
              color: Color(0xFFeb6d44),
            ),
          ),
          hintText: 'Enter a direction'), // errorText: _errorText
      onChanged: (value) {
        addRecipe.userDirections[widget.index] = _directionController!.text;
        // setState(() {}); //used to refresh the screen //OLD
      },
      validator: (value) {
        if (value!.trim().isEmpty) return 'Please enter a direction';

        return null;
      },
    );
  }
John
  • 11
  • 3

1 Answers1

0

You can use _speech.isListening instead of your isListening1

 Icon(
    _speech.isListening? Icons.mic : Icons.mic_none,
    color: Color(0xFFeb6d44),
  ),
Kishan Busa
  • 821
  • 6
  • 14