You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
doctor_app_flutter/lib/screens/medicine/medicine_search_screen.dart

331 lines
13 KiB
Dart

import 'dart:math';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.dart';
import 'package:doctor_app_flutter/providers/medicine_provider.dart';
import 'package:doctor_app_flutter/screens/medicine/pharmacies_list_screen.dart';
import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/util/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/medicine/medicine_item_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_text_form_field.dart';
5 years ago
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';
5 years ago
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
5 years ago
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 '../../util/extenstions.dart';
6 years ago
DrAppSharedPreferances sharedPref = DrAppSharedPreferances();
class MedicineSearchScreen extends StatefulWidget with DrAppToastMsg {
MedicineSearchScreen({this.changeLoadingStata});
final Function changeLoadingStata;
@override
_MedicineSearchState createState() => _MedicineSearchState();
}
class _MedicineSearchState extends State<MedicineSearchScreen> {
var data;
final myController = TextEditingController();
Helpers helpers = new Helpers();
5 years ago
bool _hasSpeech = false;
6 years ago
MedicineProvider _medicineProvider;
5 years ago
String _currentLocaleId = "";
5 years ago
bool _isInit = true;
5 years ago
final SpeechToText speech = SpeechToText();
String lastStatus = '';
5 years ago
// String lastWords;
List<LocaleName> _localeNames = [];
String lastError;
double level = 0.0;
double minSoundLevel = 50000;
double maxSoundLevel = -50000;
String reconizedWord;
5 years ago
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_isInit) {
_medicineProvider = Provider.of<MedicineProvider>(context);
// requestPermissions();
// initSpeechState();
5 years ago
}
_isInit = false;
}
5 years ago
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) {
6 years ago
return AppScaffold(
appBarTitle: TranslationBase.of(context).searchMedicine,
body: FractionallySizedBox(
widthFactor: 0.97,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Column(
children: <Widget>[
Container(
child: Icon(
DoctorApp.medicine_search,
size: 100,
color: Colors.black,
),
margin: EdgeInsets.only(top: 50),
),
Padding(
padding: const EdgeInsets.only(top: 12.0),
child: AppText(
TranslationBase.of(context).type.toUpperCase(),
fontWeight: FontWeight.bold,
fontSize: SizeConfig.heightMultiplier * 2.5,
),
),
Padding(
padding: const EdgeInsets.only(top: 5.0),
child: AppText(
TranslationBase.of(context).searchMedicineImageCaption,
fontSize: SizeConfig.heightMultiplier * 2,
),
)
],
),
SizedBox(
height: 15,
),
FractionallySizedBox(
widthFactor: 0.9,
child: Column(
children: <Widget>[
Container(
child: AppTextFormField(
hintText: TranslationBase.of(context)
.searchMedicineNameHere,
controller: myController,
onSaved: (value) {},
onFieldSubmitted: (value) {
searchMedicine(context);
},
textInputAction: TextInputAction.search,
// TODO return it back when it needed
// prefix: IconButton(
// icon: Icon(Icons.mic),
// color:
// lastStatus == 'listening' ? Colors.red : Colors.grey,
// onPressed: () {
// myController.text = '';
// setState(() {
// lastStatus = 'listening';
// });
//
// startVoiceSearch();
// }),
inputFormatter: ONLY_LETTERS),
),
SizedBox(
height: 15,
),
Container(
child: Wrap(
alignment: WrapAlignment.center,
children: <Widget>[
AppButton(
title: TranslationBase.of(context).search,
onPressed: () {
searchMedicine(context);
},
),
],
),
),
Container(
margin: EdgeInsets.only(
left: SizeConfig.heightMultiplier * 2),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
AppText(
TranslationBase.of(context).youCanFind +
_medicineProvider.pharmacyItemsList.length
.toString() +
" " +
TranslationBase.of(context).itemsInSearch,
fontWeight: FontWeight.bold,
5 years ago
),
],
),
),
Container(
height: MediaQuery.of(context).size.height * 0.35,
child: Container(
child: !_medicineProvider.isFinished
? DrAppCircularProgressIndeicator()
: _medicineProvider.hasError
? Center(
child: Text(
_medicineProvider.errorMsg,
style: TextStyle(
color:
Theme.of(context).errorColor),
),
)
: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
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]
["ImageSRCUrl"],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
PharmaciesListScreen(
itemID: _medicineProvider
.pharmacyItemsList[
index]["ItemID"],
url: _medicineProvider
.pharmacyItemsList[
index]["ImageSRCUrl"]),
),
);
},
);
},
),
),
),
],
),
),
],
),
),
6 years ago
));
}
6 years ago
searchMedicine(context) {
FocusScope.of(context).unfocus();
if (myController.text.isNullOrEmpty()) {
_medicineProvider.clearPharmacyItemsList();
helpers.showErrorToast(TranslationBase.of(context).typeMedicineName) ;
//"Type Medicine Name")
return;
}
if (myController.text.length < 3) {
_medicineProvider.clearPharmacyItemsList();
helpers.showErrorToast(TranslationBase.of(context).moreThan3Letter);
return;
}
_medicineProvider.getMedicineItem(myController.text);
}
5 years ago
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;
});
}
}