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/util/helpers.dart

338 lines
8.6 KiB
Dart

import 'package:doctor_app_flutter/models/list_doctor_working_hours_table_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../config/size_config.dart';
import '../util/dr_app_toast_msg.dart';
5 years ago
import 'package:connectivity/connectivity.dart';
DrAppToastMsg toastMsg = DrAppToastMsg();
/*
*@author: Elham Rababah
*@Date:12/4/2020
*@param:
*@return:
*@desc: This class will contian some Function will help developer
*/
class Helpers {
int cupertinoPickerIndex = 0;
/*
*@author: Elham Rababah
*@Date:12/4/2020
*@param: context, items, decKey, onSelectFun
*@return: Container Widget
*@desc: showCupertinoPicker its a general function to show cupertino picker
*/
showCupertinoPicker(context, items, decKey, onSelectFun) {
showModalBottomSheet(
isDismissible: false,
context: context,
builder: (BuildContext builder) {
return Container(
// height: 500,
height: SizeConfig.realScreenHeight * 0.4,
color: Color(0xfff7f7f7),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
color: Color(0xfff7f7f7),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
CupertinoButton(
child: Text('Cancel'.toUpperCase(),
style: textStyle(context)),
onPressed: () {
Navigator.pop(context);
},
// padding: const EdgeInsets.symmetric(
// horizontal: 16.0,
// vertical: 5.0,
// ),
),
CupertinoButton(
child: Text(
'Done'.toUpperCase(),
style: textStyle(context),
),
onPressed: () {
Navigator.pop(context);
onSelectFun(cupertinoPickerIndex);
},
)
],
),
),
Container(
height: SizeConfig.realScreenHeight * 0.3,
color: Color(0xfff7f7f7),
child:
buildPickerItems(context, items, decKey, onSelectFun))
],
),
);
});
}
TextStyle textStyle(context) =>
TextStyle(color: Theme.of(context).primaryColor);
/*
*@author: Elham Rababah
*@Date:12/4/2020
*@param: context, List items, decKey, onSelectFun
*@return: Container widget
*@desc: buildPickerIterm this function will build the items of the cupertino
*/
buildPickerItems(context, List items, decKey, onSelectFun) {
return CupertinoPicker(
magnification: 1.5,
scrollController:
FixedExtentScrollController(initialItem: cupertinoPickerIndex),
// backgroundColor: Colors.black87,
children: items.map((item) {
return Text(
'${item["$decKey"]}',
style: TextStyle(fontSize: SizeConfig.textMultiplier * 3),
);
}).toList(),
itemExtent: 25,
//height of each item
looping: true,
onSelectedItemChanged: (int index) {
// selectitem =index;
cupertinoPickerIndex = index;
},
);
}
/*
*@author: Elham Rababah
*@Date:12/4/2020
*@param: msg
*@return:
*@desc: showErrorToast
*/
showErrorToast([msg = null]) {
String localMsg = generateContactAdminMsg();
if (msg != null) {
localMsg = msg.toString();
}
toastMsg.showErrorToast(localMsg);
}
5 years ago
5 years ago
/*
*@author: Mohammad Aljammal
*@Date:27/4/2020
*@param:
*@return: Boolean
*@desc: Check The Internet Connection
*/
static Future<bool> checkConnection() async {
5 years ago
ConnectivityResult connectivityResult =
await (Connectivity().checkConnectivity());
5 years ago
if ((connectivityResult == ConnectivityResult.mobile) ||
(connectivityResult == ConnectivityResult.wifi)) {
return true;
} else {
return false;
}
}
/*
*@author: Mohammad Aljammal
*@Date:26/5/2020
*@param: date in String formatted
*@return: DateTime
*@desc: convert String to DateTime
*/
static DateTime convertStringToDate(String date) {
const start = "/Date(";
const end = "+0300)";
final startIndex = date.indexOf(start);
final endIndex = date.indexOf(end, startIndex + start.length);
return DateTime.fromMillisecondsSinceEpoch(
int.parse(
date.substring(startIndex + start.length, endIndex),
),
);
}
/*
*@author: Amjad Amireh
*@Date:5/5/2020
*@param: checkDate
*@return: DateTime
*@desc: convert String to DateTime
*/
static String checkDate(String dateString) {
DateTime checkedTime = DateTime.parse(dateString);
DateTime currentTime = DateTime.now();
if ((currentTime.year == checkedTime.year) &&
(currentTime.month == checkedTime.month) &&
(currentTime.day == checkedTime.day)) {
return "Today";
} else if ((currentTime.year == checkedTime.year) &&
(currentTime.month == checkedTime.month)) {
if ((currentTime.day - checkedTime.day) == 1) {
return "YESTERDAY";
} else if ((currentTime.day - checkedTime.day) == -1) {
return "Tomorrow";
}
if ((currentTime.day - checkedTime.day) <= -2) {
return "Next Week";
} else {
return "Old Date";
}
}
return "Old Date";
}
/*
*@author: Mohammad Aljammal
*@Date:26/5/2020
*@param: month in int formatted
*@return: DateTime
*@desc: convert month in int to month name
*/
static getMonth(int month) {
switch (month) {
case 1:
return "Jan";
case 2:
return "Feb";
case 3:
return "Mar";
case 4:
return "Apr";
case 5:
return "May";
case 6:
return "Jun";
case 7:
return "Jul";
case 8:
return "Aug";
case 9:
return "Sep";
case 10:
return "Oct";
case 11:
return "Nov";
case 12:
return "Dec";
}
}
/*
*@author: Mohammad Aljammal
*@Date:26/5/2020
*@param: week day in int formatted
*@return: DateTime
*@desc: convert week day in int to week day name
*/
static getWeekDay(int weekDay) {
switch (weekDay) {
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday ";
case 7:
return "Sunday";
}
}
/*
*@author: Mohammad Aljammal
*@Date:26/5/2020
*@param: DateTime
*@return: data formatted like Apr 26,2020
*@desc: convert DateTime to data formatted
*/
static String getDate(DateTime dateTime) {
print(dateTime);
if (dateTime != null)
return getMonth(dateTime.month) +
" " +
dateTime.day.toString() +
"," +
dateTime.year.toString();
else
return "";
}
/*
*@author: Mohammad Aljammal
*@Date:26/5/2020
*@param: DateTime
*@return: data formatted like 26/4/2020
*@desc: convert DateTime to data formatted
*/
static String getDateFormatted(DateTime dateTime) {
print(dateTime);
if (dateTime != null)
return dateTime.day.toString() +
"/" +
dateTime.month.toString() +
"/" +
dateTime.year.toString();
else
return "";
}
/*
*@author: Mohammad Aljammal
*@Date:26/5/2020
*@param: String workingHours
*@return: List<WorkingHours>
*@desc: convert workingHours string to List<WorkingHours>
*/
static List<WorkingHours> getWorkingHours(String workingHours ){
List<WorkingHours> myWorkingHours =[];
List<String> listOfHours = workingHours.split('a');
listOfHours.forEach((element) {
WorkingHours workingHours = WorkingHours();
var from = element.substring(element.indexOf('m ') + 2 , element.indexOf('To')-2);
workingHours.from = from.trim();
var to = element.substring(element.indexOf('To') + 2);
workingHours.to = to.trim();
myWorkingHours.add(workingHours);
});
return myWorkingHours;
}
/*
*@author: Elham Rababah
*@Date:12/5/2020
*@param:
*@return: String
*@desc: generate Contact Admin Msg
*/
generateContactAdminMsg([err = null]) {
String localMsg = 'Something wrong happened, please contact the admin';
if (err != null) {
localMsg = localMsg + '\n \n' + err.toString();
}
return localMsg;
}
}