|
|
|
|
@ -13,8 +13,14 @@ import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
|
|
|
|
|
import 'package:doctor_app_flutter/widgets/shared/dr_app_circular_progress_Indeicator.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
|
|
import '../../util/extenstions.dart';
|
|
|
|
|
import 'package:doctor_app_flutter/util/translations_delegate_base.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 'dart:math';
|
|
|
|
|
|
|
|
|
|
DrAppSharedPreferances sharedPref = DrAppSharedPreferances();
|
|
|
|
|
|
|
|
|
|
@ -30,20 +36,55 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
|
|
|
|
|
var data;
|
|
|
|
|
final myController = TextEditingController();
|
|
|
|
|
Helpers helpers = new Helpers();
|
|
|
|
|
|
|
|
|
|
bool _hasSpeech = false;
|
|
|
|
|
MedicineProvider _medicineProvider;
|
|
|
|
|
|
|
|
|
|
String _currentLocaleId = "";
|
|
|
|
|
bool _isInit = true;
|
|
|
|
|
|
|
|
|
|
final SpeechToText speech = SpeechToText();
|
|
|
|
|
String lastStatus = '';
|
|
|
|
|
// String lastWords;
|
|
|
|
|
List<LocaleName> _localeNames = [];
|
|
|
|
|
String lastError;
|
|
|
|
|
double level = 0.0;
|
|
|
|
|
double minSoundLevel = 50000;
|
|
|
|
|
double maxSoundLevel = -50000;
|
|
|
|
|
String reconizedWord;
|
|
|
|
|
@override
|
|
|
|
|
void didChangeDependencies() {
|
|
|
|
|
super.didChangeDependencies();
|
|
|
|
|
if (_isInit) {
|
|
|
|
|
_medicineProvider = Provider.of<MedicineProvider>(context);
|
|
|
|
|
requestPermissions();
|
|
|
|
|
initSpeechState();
|
|
|
|
|
}
|
|
|
|
|
_isInit = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void requestPermissions() async {
|
|
|
|
|
Map<Permission, PermissionStatus> statuses = await [
|
|
|
|
|
Permission.microphone,
|
|
|
|
|
].request();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> initSpeechState() async {
|
|
|
|
|
bool hasSpeech = await speech.initialize(
|
|
|
|
|
onError: errorListener, onStatus: statusListener);
|
|
|
|
|
// if (hasSpeech) {
|
|
|
|
|
// _localeNames = await speech.locales();
|
|
|
|
|
|
|
|
|
|
// var systemLocale = await speech.systemLocale();
|
|
|
|
|
_currentLocaleId = TranslationBase.of(context).locale.languageCode == 'en'
|
|
|
|
|
? 'en-GB'
|
|
|
|
|
: 'ar-SA'; // systemLocale.localeId;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
_hasSpeech = hasSpeech;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return AppScaffold(
|
|
|
|
|
@ -60,10 +101,22 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
|
|
|
|
|
hintText: TranslationBase.of(context).searchMedicineNameHere,
|
|
|
|
|
controller: myController,
|
|
|
|
|
onSaved: (value) {},
|
|
|
|
|
onFieldSubmitted: (value){
|
|
|
|
|
searchMedicine(context);
|
|
|
|
|
onFieldSubmitted: (value) {
|
|
|
|
|
searchMedicine(context);
|
|
|
|
|
},
|
|
|
|
|
textInputAction: TextInputAction.search,
|
|
|
|
|
prefix: IconButton(
|
|
|
|
|
icon: Icon(Icons.mic),
|
|
|
|
|
color:
|
|
|
|
|
lastStatus == 'listening' ? Colors.red : Colors.grey,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
myController.text = '';
|
|
|
|
|
setState(() {
|
|
|
|
|
lastStatus = 'listening';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
startVoiceSearch();
|
|
|
|
|
}),
|
|
|
|
|
inputFormatter: ONLY_LETTERS),
|
|
|
|
|
),
|
|
|
|
|
Container(
|
|
|
|
|
@ -92,7 +145,10 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
AppText(
|
|
|
|
|
TranslationBase.of(context).youCanFind +
|
|
|
|
|
(_medicineProvider.pharmacyItemsList == null ? "0" : _medicineProvider.pharmacyItemsList.length.toString()) +
|
|
|
|
|
(_medicineProvider.pharmacyItemsList == null
|
|
|
|
|
? "0"
|
|
|
|
|
: _medicineProvider.pharmacyItemsList.length
|
|
|
|
|
.toString()) +
|
|
|
|
|
TranslationBase.of(context).itemsInSearch,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
margin: 5,
|
|
|
|
|
@ -116,12 +172,19 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
|
|
|
|
|
: ListView.builder(
|
|
|
|
|
scrollDirection: Axis.vertical,
|
|
|
|
|
shrinkWrap: true,
|
|
|
|
|
itemCount: _medicineProvider.pharmacyItemsList == null ? 0 : _medicineProvider.pharmacyItemsList.length,
|
|
|
|
|
itemCount: _medicineProvider.pharmacyItemsList ==
|
|
|
|
|
null
|
|
|
|
|
? 0
|
|
|
|
|
: _medicineProvider.pharmacyItemsList.length,
|
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
|
return InkWell(
|
|
|
|
|
child: MedicineItemWidget(
|
|
|
|
|
label: _medicineProvider.pharmacyItemsList[index]["ItemDescription"],
|
|
|
|
|
url: _medicineProvider.pharmacyItemsList[index]["ProductImageBase64"],
|
|
|
|
|
label:
|
|
|
|
|
_medicineProvider.pharmacyItemsList[index]
|
|
|
|
|
["ItemDescription"],
|
|
|
|
|
url:
|
|
|
|
|
_medicineProvider.pharmacyItemsList[index]
|
|
|
|
|
["ImageThumbUrl"],
|
|
|
|
|
),
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
@ -129,9 +192,12 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) =>
|
|
|
|
|
PharmaciesListScreen(
|
|
|
|
|
itemID: _medicineProvider.pharmacyItemsList[index]["ItemID"],
|
|
|
|
|
url: _medicineProvider.pharmacyItemsList[index]
|
|
|
|
|
["ProductImageBase64"]),
|
|
|
|
|
itemID: _medicineProvider
|
|
|
|
|
.pharmacyItemsList[index]
|
|
|
|
|
["ItemID"],
|
|
|
|
|
url: _medicineProvider
|
|
|
|
|
.pharmacyItemsList[index]
|
|
|
|
|
["ProductImageBase64"]),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
@ -153,4 +219,62 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
|
|
|
|
|
}
|
|
|
|
|
_medicineProvider.getMedicineItem(myController.text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startVoiceSearch() {
|
|
|
|
|
// lastWords = "";
|
|
|
|
|
lastError = "";
|
|
|
|
|
speech.listen(
|
|
|
|
|
onResult: resultListener,
|
|
|
|
|
listenFor: Duration(seconds: 10),
|
|
|
|
|
localeId: _currentLocaleId,
|
|
|
|
|
onSoundLevelChange: soundLevelListener,
|
|
|
|
|
cancelOnError: true,
|
|
|
|
|
partialResults: true,
|
|
|
|
|
onDevice: true,
|
|
|
|
|
listenMode: ListenMode.confirmation);
|
|
|
|
|
setState(() {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resultListener(SpeechRecognitionResult result) {
|
|
|
|
|
setState(() {
|
|
|
|
|
// lastWords = "${result.recognizedWords} - ${result.finalResult}";
|
|
|
|
|
reconizedWord = result.recognizedWords;
|
|
|
|
|
lastStatus = '';
|
|
|
|
|
myController.text = reconizedWord;
|
|
|
|
|
Future.delayed(const Duration(seconds: 2), () {
|
|
|
|
|
searchMedicine(context);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void errorListener(SpeechRecognitionError error) {
|
|
|
|
|
// print("Received error status: $error, listening: ${speech.isListening}");
|
|
|
|
|
setState(() {
|
|
|
|
|
lastError = "${error.errorMsg} - ${error.permanent}";
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void statusListener(String status) {
|
|
|
|
|
// print(
|
|
|
|
|
// "Received listener status: $status, listening: ${speech.isListening}");
|
|
|
|
|
setState(() {
|
|
|
|
|
lastStatus = status;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// _switchLang(selectedVal) {
|
|
|
|
|
// setState(() {
|
|
|
|
|
// _currentLocaleId = selectedVal;
|
|
|
|
|
// });
|
|
|
|
|
// print(selectedVal);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
void soundLevelListener(double level) {
|
|
|
|
|
minSoundLevel = min(minSoundLevel, level);
|
|
|
|
|
maxSoundLevel = max(maxSoundLevel, level);
|
|
|
|
|
// print("sound level $level: $minSoundLevel - $maxSoundLevel ");
|
|
|
|
|
setState(() {
|
|
|
|
|
this.level = level;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|