Speech to Text 패키지는 . 반드시 그래들 파일에 SDK 버전을 21 로 설정해야합니다.
minSdkVersion 21
아래 부부은 공부하다가 중복된 것이니 제외 하고 사용하면 됩니다.
ValueListenableBuilder
onChanged: (textValue) { //인풋값으로 바뀌지 않음.
setState(() => _controllerText = _speechController.text);
},
https://pub.dev/packages/speech_to_text/install
import 'package:flutter/material.dart';
import 'package:noti_local/a_drawer.dart';
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:speech_to_text/speech_to_text.dart';
// must be SDK 21
class SpeechTextHome extends StatefulWidget {
const SpeechTextHome({super.key});
@override
State<SpeechTextHome> createState() => _SpeechTextHomeState();
}
class _SpeechTextHomeState extends State<SpeechTextHome> {
final ValueNotifier<int> countNum = ValueNotifier(0);
// Value Notifier<List<int>>는 목록의 내용이 변경될 때 수신자에게 통지하지 않습니다.
// 따라서 이 클래스는 불변 데이터 유형만 사용하는 것이 가장 좋습니다.
SpeechToText _speechToText = SpeechToText();
String _words = " Click Hold for Speech to Text";
bool _speechEnable = false;
//
TextEditingController _speechController = TextEditingController();
String _controllerText = '';
@override
void initState() {
// TODO: implement initState
super.initState();
_initSpeech();
}
@override
void dispose() {
_speechToText.cancel();
_speechController.dispose();
super.dispose();
}
void _initSpeech() async {
//음성초기화
_speechEnable = await _speechToText.initialize();
setState(() {});
}
void _startListening() async {
await _speechToText.listen(onResult: _onSpeechResult);
setState(() {});
}
void _stopListening() async {
await _speechToText.stop();
setState(() {});
}
void _onSpeechResult(SpeechRecognitionResult result) async {
_speechController.text = result.recognizedWords;
setState(() {
_words = result.recognizedWords;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Speech to Text')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("TextField : $_controllerText"),
Divider(),
TextField(
controller: _speechController,
maxLines: 3,
decoration: InputDecoration(
border: const OutlineInputBorder(),
suffix: IconButton(
onPressed: _speechToText.isListening ? _stopListening : _startListening,
icon: Icon(_speechToText.isListening ? Icons.mic_off : Icons.mic),
),
hintText: 'Your Spoken Text',
),
onChanged: (textValue) { //인풋값으로 바뀌지 않음.
setState(() => _controllerText = _speechController.text);
},
),
Text(_speechToText.isListening
? _words
: _speechEnable
? 'Press Mic to Start to Speech to Text'
: 'Speech Not Available'),
Container(
width: 100,
height: 100,
child: IconButton(
onPressed: _speechToText.isListening ? _stopListening : _startListening,
icon: Icon(_speechToText.isListening ? Icons.mic_off : Icons.mic),
iconSize: 65,
color: Colors.cyan),
),
Divider(),
/////////////////////// ValueListenableBuilder test
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ValueListenableBuilder(
valueListenable: countNum,
builder: (context, int value, child) {
return Text(
'$value',
style: TextStyle(fontSize: 30),
);
}),
IconButton(
onPressed: () {
countNum.value += 1;
},
icon: Icon(Icons.add)),
],
)
],
),
),
);
}
}
'Flutter' 카테고리의 다른 글
WebView 로 bard gpt chat 만들기? .. (1) | 2023.12.15 |
---|---|
Chat GPT 만들기 , 음성입력 gpt 3.5 turbo 202312 (0) | 2023.12.13 |
Dark & Light Mode with Provider (0) | 2023.12.10 |
Flutter Voice to Text 음성입력.#2 (2) | 2023.12.05 |
Flutter Voice to Text 음성입력 변환 #1 (1) | 2023.12.05 |