voice search in progress
parent
12d5ef69a9
commit
98f84c2161
Binary file not shown.
|
Before Width: | Height: | Size: 50 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 61 KiB |
@ -1,249 +0,0 @@
|
||||
import 'package:diplomaticquarterapp/config/size_config.dart';
|
||||
import 'package:diplomaticquarterapp/services/robo_search/event_provider.dart';
|
||||
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
|
||||
import 'package:diplomaticquarterapp/widgets/buttons/button.dart';
|
||||
import 'package:diplomaticquarterapp/widgets/others/floating_button_search.dart';
|
||||
import 'package:diplomaticquarterapp/widgets/robo-search/search.dart';
|
||||
import 'package:diplomaticquarterapp/widgets/text/app_texts_widget.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class RoboSearch extends StatefulWidget {
|
||||
@override
|
||||
_RoboSearch createState() => new _RoboSearch();
|
||||
}
|
||||
|
||||
class _RoboSearch extends State<RoboSearch> {
|
||||
var event = RobotProvider();
|
||||
var searchText = '';
|
||||
bool isRetry = false;
|
||||
var tween = Tween(begin: 0.0, end: 1.0).chain(CurveTween(curve: Curves.ease));
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
event.controller.stream.listen((p) {
|
||||
if (p['searchText'] != null) {
|
||||
if (p['searchText'] == 'null') {
|
||||
//Future.delayed(const Duration(seconds: 1), () {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
isRetry = true;
|
||||
searchText = '';
|
||||
});
|
||||
}
|
||||
//});
|
||||
} else {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
searchText = p['searchText'];
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
content: Container(
|
||||
color: Colors.white,
|
||||
height: SizeConfig.realScreenHeight * 0.5,
|
||||
width: SizeConfig.realScreenWidth * 0.8,
|
||||
child: Container(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
isRetry == true
|
||||
? Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Container(
|
||||
margin: EdgeInsets.all(20),
|
||||
padding: EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius:
|
||||
BorderRadius.circular(100),
|
||||
border: Border.all(
|
||||
width: 5, color: Colors.red)),
|
||||
child: Icon(
|
||||
Icons.mic,
|
||||
color: Colors.blue,
|
||||
size: 48,
|
||||
)),
|
||||
Center(
|
||||
child: AppText(
|
||||
"Didn't catch that, Try speaking again",
|
||||
fontSize: 14,
|
||||
)),
|
||||
RaisedButton(
|
||||
color: Colors.transparent,
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
isRetry = false;
|
||||
});
|
||||
FloatingSearchButton()
|
||||
.createState()
|
||||
.startVoiceSearch();
|
||||
},
|
||||
child: AppText(
|
||||
'Try again',
|
||||
color: Colors.blue,
|
||||
))
|
||||
])
|
||||
: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Container(
|
||||
margin: EdgeInsets.all(20),
|
||||
padding: EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(100)),
|
||||
child: Icon(
|
||||
Icons.mic,
|
||||
color: Colors.white,
|
||||
size: 48,
|
||||
)),
|
||||
SizedBox(height: 20),
|
||||
Center(
|
||||
child: Text(searchText == ''
|
||||
? 'Try saying something'
|
||||
: searchText))
|
||||
],
|
||||
)
|
||||
]),
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
// class RoboSearch {
|
||||
// final BuildContext context;
|
||||
// var event = RobotProvider();
|
||||
// var searchText = '';
|
||||
// bool isRetry = false;
|
||||
// var tween = Tween(begin: 0.0, end: 1.0).chain(CurveTween(curve: Curves.ease));
|
||||
// RoboSearch({
|
||||
// @required this.context,
|
||||
// });
|
||||
|
||||
// showAlertDialog(BuildContext context) {
|
||||
// // set up the buttons
|
||||
|
||||
// // set up the AlertDialog
|
||||
// AlertDialog alert = AlertDialog(
|
||||
// content: StatefulBuilder(
|
||||
// builder: (BuildContext context, StateSetter setState) {
|
||||
|
||||
// event.controller.stream.listen((p) {
|
||||
// if (p['searchText'] != null) {
|
||||
// if (p['searchText'] == 'null') {
|
||||
// Future.delayed(const Duration(seconds: 1), ()
|
||||
// {
|
||||
// setState(() {
|
||||
// isRetry = true;
|
||||
// searchText = '';
|
||||
// });
|
||||
// });
|
||||
// } else {
|
||||
// Future.delayed(const Duration(seconds: 1), ()
|
||||
// {
|
||||
// setState(() {
|
||||
// searchText = p['searchText'];
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// return Container(
|
||||
// color: Colors.white,
|
||||
// height: SizeConfig.realScreenHeight * 0.5,
|
||||
// width: SizeConfig.realScreenWidth * 0.8,
|
||||
// child: Container(
|
||||
// child: Column(
|
||||
// mainAxisAlignment: MainAxisAlignment.center,
|
||||
// children: [
|
||||
// isRetry == true
|
||||
// ? Column(
|
||||
// mainAxisAlignment: MainAxisAlignment.center,
|
||||
// children: [
|
||||
// Container(
|
||||
// margin: EdgeInsets.all(20),
|
||||
// padding: EdgeInsets.all(10),
|
||||
// decoration: BoxDecoration(
|
||||
// borderRadius:
|
||||
// BorderRadius.circular(100),
|
||||
// border: Border.all(
|
||||
// width: 5, color: Colors.red)),
|
||||
// child: Icon(
|
||||
// Icons.mic,
|
||||
// color: Colors.blue,
|
||||
// size: 48,
|
||||
// )),
|
||||
// Center(
|
||||
// child: AppText(
|
||||
// "Didn't catch that, Try speaking again",
|
||||
// fontSize: 14,
|
||||
// )),
|
||||
// RaisedButton(
|
||||
// color: Colors.transparent,
|
||||
// onPressed: () {
|
||||
// setState(() {
|
||||
// isRetry = false;
|
||||
|
||||
// });
|
||||
// startVoiceSearch();
|
||||
// },
|
||||
// child: AppText(
|
||||
// 'Try again',
|
||||
// color: Colors.blue,
|
||||
// ))
|
||||
// ])
|
||||
// : Column(
|
||||
// mainAxisAlignment: MainAxisAlignment.center,
|
||||
// children: [
|
||||
// Container(
|
||||
// margin: EdgeInsets.all(20),
|
||||
// padding: EdgeInsets.all(20),
|
||||
// decoration: BoxDecoration(
|
||||
// color: Colors.blue,
|
||||
// borderRadius: BorderRadius.circular(100)),
|
||||
// child: Icon(
|
||||
// Icons.mic,
|
||||
// color: Colors.white,
|
||||
// size: 48,
|
||||
// )),
|
||||
// SizedBox(height: 20),
|
||||
// Center(
|
||||
// child: Text(searchText == ''
|
||||
// ? 'Try saying something'
|
||||
// : searchText))
|
||||
// ],
|
||||
// )
|
||||
// ]),
|
||||
// ));
|
||||
// }),
|
||||
// );
|
||||
|
||||
// // show the dialog
|
||||
// showDialog(
|
||||
// context: context,
|
||||
// builder: (BuildContext context) {
|
||||
// return alert;
|
||||
// },
|
||||
// );
|
||||
// }
|
||||
|
||||
// static closeAlertDialog(BuildContext context) {
|
||||
// Navigator.of(context).pop();
|
||||
// }
|
||||
|
||||
// startVoiceSearch() {
|
||||
|
||||
// // Navigator.of(context).pop();
|
||||
// //this.showAlertDialog(context);
|
||||
// // new RoboSearch(context: context).showAlertDialog(context);
|
||||
// FloatingSearchButton().createState().startVoiceSearch();
|
||||
// }
|
||||
// }
|
||||
@ -1,233 +0,0 @@
|
||||
import 'package:diplomaticquarterapp/config/config.dart';
|
||||
import 'package:diplomaticquarterapp/config/size_config.dart';
|
||||
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
|
||||
import 'package:diplomaticquarterapp/pages/BookAppointment/BookingOptions.dart';
|
||||
import 'package:diplomaticquarterapp/pages/BookAppointment/Search.dart';
|
||||
import 'package:diplomaticquarterapp/pages/landing/landing_page.dart';
|
||||
import 'package:diplomaticquarterapp/pages/medical/medical_profile_page.dart';
|
||||
import 'package:diplomaticquarterapp/pages/medical/my_admissions_page.dart';
|
||||
import 'package:diplomaticquarterapp/services/robo_search/search_provider.dart';
|
||||
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
|
||||
import 'package:diplomaticquarterapp/widgets/bottom_navigation/bottom_nav_bar.dart';
|
||||
import 'package:diplomaticquarterapp/widgets/text/app_texts_widget.dart';
|
||||
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_tts/flutter_tts.dart';
|
||||
import 'package:hexcolor/hexcolor.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:speech_to_text/speech_recognition_error.dart';
|
||||
import 'package:speech_to_text/speech_recognition_result.dart';
|
||||
import 'package:speech_to_text/speech_to_text.dart';
|
||||
import 'package:diplomaticquarterapp/widgets/robo-search/robosearch.dart';
|
||||
import 'dart:math';
|
||||
import 'package:diplomaticquarterapp/services/robo_search/event_provider.dart';
|
||||
|
||||
class SearchBot with ChangeNotifier {
|
||||
SearchBot() {
|
||||
requestPermissions();
|
||||
initSpeechState();
|
||||
}
|
||||
|
||||
SearchProvider searchProvider = new SearchProvider();
|
||||
// RobotProvider eventProvider = RobotProvider();
|
||||
bool isLoading = false;
|
||||
bool isError = false;
|
||||
final SpeechToText speech = SpeechToText();
|
||||
String error = '';
|
||||
String _currentLocaleId = "";
|
||||
String lastError;
|
||||
double level = 0.0;
|
||||
|
||||
double minSoundLevel = 50000;
|
||||
double maxSoundLevel = -50000;
|
||||
String reconizedWord = '';
|
||||
FlutterTts flutterTts = FlutterTts();
|
||||
var selectedLang;
|
||||
bool isSearching = false;
|
||||
|
||||
String lastStatus;
|
||||
|
||||
bool _isInit = true;
|
||||
|
||||
// @override
|
||||
// void didChangeDependencies() {
|
||||
// super.didChangeDependencies();
|
||||
// if (_isInit) {
|
||||
|
||||
// } else {
|
||||
// startVoiceSearch();
|
||||
// }
|
||||
|
||||
// _isInit = false;
|
||||
// }
|
||||
|
||||
//@override
|
||||
// Widget build(BuildContext context) {
|
||||
// return Column(
|
||||
// children: <Widget>[
|
||||
// Image.asset('assets/images/siri-ianzhao.gif'),
|
||||
// AppText(
|
||||
// reconizedWord,
|
||||
// fontSize: 28,
|
||||
// ),
|
||||
// // SizedBox(
|
||||
// // child: FlatButton(
|
||||
// // child: Text('Start'),
|
||||
// // onPressed: () {
|
||||
// // setState(() {
|
||||
// // reconizedWord = 'Lisening...';
|
||||
// // });
|
||||
|
||||
// // },
|
||||
// // ),
|
||||
// // height: 40,
|
||||
// // width: 80)
|
||||
// ],
|
||||
// );
|
||||
// }
|
||||
|
||||
startVoiceSearch() async {
|
||||
speech.listen(
|
||||
onResult: resultListener,
|
||||
listenFor: Duration(seconds: 10),
|
||||
localeId: _currentLocaleId,
|
||||
onSoundLevelChange: soundLevelListener,
|
||||
cancelOnError: true,
|
||||
partialResults: true,
|
||||
onDevice: true,
|
||||
listenMode: ListenMode.confirmation);
|
||||
}
|
||||
|
||||
void resultListener(SpeechRecognitionResult result) {
|
||||
// lastWords = "${result.recognizedWords} - ${result.finalResult}";
|
||||
|
||||
if (result.finalResult == true) {
|
||||
// setState(() {
|
||||
reconizedWord = result.recognizedWords;
|
||||
//});
|
||||
Future.delayed(const Duration(seconds: 1), () {
|
||||
_speak(reconizedWord);
|
||||
});
|
||||
}
|
||||
//});
|
||||
}
|
||||
|
||||
Future _speak(reconizedWord) async {
|
||||
await flutterTts.speak(reconizedWord);
|
||||
|
||||
getPages(reconizedWord);
|
||||
}
|
||||
|
||||
void soundLevelListener(double level) {
|
||||
minSoundLevel = min(minSoundLevel, level);
|
||||
maxSoundLevel = max(maxSoundLevel, level);
|
||||
// print("sound level $level: $minSoundLevel - $maxSoundLevel ");
|
||||
//setState(() {
|
||||
this.level = level;
|
||||
// });
|
||||
}
|
||||
|
||||
void requestPermissions() async {
|
||||
Map<Permission, PermissionStatus> statuses = await [
|
||||
Permission.microphone,
|
||||
].request();
|
||||
}
|
||||
|
||||
Future<void> initSpeechState() async {
|
||||
await speech.initialize(onError: errorListener, onStatus: statusListener);
|
||||
|
||||
_currentLocaleId =
|
||||
TranslationBase.of(AppGlobal.context).locale.languageCode == 'en'
|
||||
? 'en-GB'
|
||||
: 'ar-SA'; // systemLocale.localeId;
|
||||
flutterTts.setLanguage(_currentLocaleId);
|
||||
|
||||
await flutterTts.speak("Hello! How May I help you?");
|
||||
flutterTts.setCompletionHandler(() {
|
||||
startVoiceSearch();
|
||||
});
|
||||
|
||||
// if (!mounted) return;
|
||||
}
|
||||
|
||||
void errorListener(SpeechRecognitionError error) {
|
||||
//setState(() {
|
||||
// reconizedWord = "${error.errorMsg} - ${error.permanent}";
|
||||
//});
|
||||
}
|
||||
|
||||
void statusListener(String status) {
|
||||
//setState(() {
|
||||
reconizedWord = status == 'listening' ? 'Lisening...' : 'Sorry....';
|
||||
//});
|
||||
}
|
||||
|
||||
getPages(text) {
|
||||
var request = {
|
||||
'VoiceMessage': text,
|
||||
'Lang': TranslationBase.of(AppGlobal.context).locale.languageCode == 'en'
|
||||
? 'En'
|
||||
: 'Ar'
|
||||
};
|
||||
|
||||
searchProvider.getBotPages(request).then((value) => {});
|
||||
//getCommands(value['Response'], value['Understand'])
|
||||
}
|
||||
|
||||
getCommands(result, understand) async {
|
||||
//RoboSearch.closeAlertDialog(context);
|
||||
print(result);
|
||||
switch (result["CommandNumber"]) {
|
||||
case '100':
|
||||
Navigator.push(
|
||||
AppGlobal.context,
|
||||
FadePage(
|
||||
page: BookingOptions(
|
||||
isAppbar: true,
|
||||
)));
|
||||
break;
|
||||
case '101':
|
||||
Navigator.push(
|
||||
AppGlobal.context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => Search(
|
||||
type: 0,
|
||||
)));
|
||||
break;
|
||||
case '102':
|
||||
Navigator.push(
|
||||
AppGlobal.context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => Search(
|
||||
type: 1,
|
||||
)));
|
||||
break;
|
||||
// case '103':
|
||||
// eventProvider.setValue({"clinic_id": understand});
|
||||
// break;
|
||||
|
||||
// case '104':
|
||||
// eventProvider.setValue({"project_id": understand});
|
||||
// break;
|
||||
// case '105':
|
||||
// eventProvider.setValue({"doctor_id": understand});
|
||||
// break;
|
||||
default:
|
||||
{
|
||||
//eventProvider.setValue({"doctor_id": '40036'});
|
||||
//eventProvider.setValue();
|
||||
// if (result['CommandNumber'] == '0') {
|
||||
// searchProvider.setData(understand);
|
||||
// }
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (_currentLocaleId == 'en-GB') {
|
||||
await flutterTts.speak(result['ResponseEn']);
|
||||
} else {
|
||||
await flutterTts.speak(result['ResponseAr']);
|
||||
}
|
||||
//searchProvider.setLisener(result);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue