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.
diplomatic-quarter/lib/pages/AlHabibMedicalService/h2o/h20_setting.dart

656 lines
25 KiB
Dart

import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_detail_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/H2O_view_model.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:manage_calendar_events/manage_calendar_events.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'Dialog/setting_page_radio_button_list_dialog.dart';
class H2oSetting extends StatefulWidget {
final UserDetailModel userDetailModel;
final H2OViewModel viewModel;
H2oSetting({Key key, this.userDetailModel, this.viewModel}) : super(key: key);
@override
_H2oSettingState createState() {
return _H2oSettingState();
}
}
class _H2oSettingState extends State<H2oSetting> {
TextEditingController _nameController = TextEditingController();
TextEditingController _heightController = TextEditingController();
TextEditingController _weightController = TextEditingController();
List<PopupMenuItem> _weightPopupList = List();
List<PopupMenuItem> _heightPopupList = List();
bool _isUnitML = true;
bool _isGenderMale = false;
bool _isHeightCM = false;
bool _isWeightKG = false;
double _heightValue = 1;
double _weightValue = 1;
List<String> _activityLevelListEng = [];
List<String> _remindedTimeListEng = [];
int _selectedActiveLevel = 1;
int _selectedRemindedTime = 0;
DateTime _dobDate = DateTime.now();
DateTime _tempDate = DateTime.now();
UserDetailModel _userDetailModel;
@override
void initState() {
super.initState();
_userDetailModel = widget.userDetailModel;
_heightValue = _userDetailModel?.height ?? 100;
_weightValue = _userDetailModel?.weight ?? 50;
_heightController.text = _heightValue.toStringAsFixed(0);
_weightController.text = _weightValue.toStringAsFixed(0);
_nameController.text = _userDetailModel.firstName;
_isWeightKG = _userDetailModel?.isWeightInKG ?? true;
_isHeightCM = _userDetailModel?.isHeightInCM ?? true;
_isGenderMale = (_userDetailModel?.gender ?? "M") == "M" ? true : false;
_dobDate = DateUtil.convertStringToDate(_userDetailModel.dOB);
_selectedActiveLevel = _userDetailModel.activityID ?? 1;
readPrefs();
}
SharedPreferences prefs;
void readPrefs() async {
prefs = await SharedPreferences.getInstance();
_isUnitML = (prefs.getString(H2O_UNIT) ?? "ml") == "ml" ? true : false;
_selectedRemindedTime = prefs.getInt(H2O_REMINDER) ?? 0;
setState(() {});
}
@override
void didChangeDependencies() {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
_activityLevelListEng = [TranslationBase.of(context).notActive, TranslationBase.of(context).lightActive, TranslationBase.of(context).modActive, TranslationBase.of(context).active];
_remindedTimeListEng = ["1 ${TranslationBase.of(context).time}", "2 ${TranslationBase.of(context).times}", "3 ${TranslationBase.of(context).times}", "4 ${TranslationBase.of(context).times}"];
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
_weightPopupList = <PopupMenuItem>[PopupMenuItem(child: Text(TranslationBase.of(context).kg), value: true), PopupMenuItem(child: Text(TranslationBase.of(context).lb), value: false)];
_heightPopupList = <PopupMenuItem>[PopupMenuItem(child: Text(TranslationBase.of(context).cm), value: true), PopupMenuItem(child: Text(TranslationBase.of(context).ft), value: false)];
return AppScaffold(
isShowAppBar: true,
appBarTitle: TranslationBase.of(context).h2o,
showNewAppBar: true,
showNewAppBarTitle: true,
backgroundColor: Color(0xffF7F7F7),
showHomeAppBarIcon: false,
body: Column(
children: [
Expanded(
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
padding: EdgeInsets.all(21),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
TranslationBase.of(context).enterDetailBelow,
style: TextStyle(
fontSize: 19,
height: 23 / 19,
fontWeight: FontWeight.w700,
color: Color(0xff2E303A),
letterSpacing: -1.14,
),
),
SizedBox(height: 12),
_containerWidget(inputWidget(TranslationBase.of(context).enterNameHere, TranslationBase.of(context).name, _nameController)),
SizedBox(height: 16),
Text(
TranslationBase.of(context).preferredunit,
style: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w600,
color: Color(0xff2B353E),
letterSpacing: -0.44,
),
),
SizedBox(height: 12),
_commonRadioButtonsRow(TranslationBase.of(context).mililitre, TranslationBase.of(context).litre, _isUnitML, (value) {
if (_isUnitML != value) {
setState(() {
_isUnitML = value;
});
}
}),
SizedBox(height: 12),
Text(
TranslationBase.of(context).pleaseSelectGender,
style: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w600,
color: Color(0xff2B353E),
letterSpacing: -0.44,
),
),
SizedBox(height: 12),
_commonRadioButtonsRow(TranslationBase.of(context).male, TranslationBase.of(context).female, _isGenderMale, (value) {
if (_isGenderMale != value) {
setState(() {
_isGenderMale = value;
});
}
}),
SizedBox(height: 18),
_commonInputAndUnitRow(
TranslationBase.of(context).height,
_heightController,
1,
270,
_heightValue,
(text) {
_heightController.text = text;
},
(value) {
_heightValue = value;
},
_isHeightCM ? TranslationBase.of(context).cm : TranslationBase.of(context).ft,
(value) {
if (_isHeightCM != value) {
setState(() {
_isHeightCM = value;
});
}
},
_heightPopupList),
SizedBox(height: 12),
_commonInputAndUnitRow(
TranslationBase.of(context).weight,
_weightController,
1,
250,
_weightValue,
(text) {
_heightController.text = text;
},
(value) {
_weightValue = value;
},
_isWeightKG ? TranslationBase.of(context).kg : TranslationBase.of(context).lb,
(value) {
if (_isWeightKG != value) {
setState(() {
_isWeightKG = value;
});
}
},
_weightPopupList),
SizedBox(height: 12),
_containerWidget(
_commonDropDownView(TranslationBase.of(context).dateOfBirth, DateUtil.getFormattedDate(_dobDate, "dd MMMM, yyyy"), () {
showModalBottomSheet(
context: context,
builder: (context) {
return Container(
height: 250,
padding: EdgeInsets.all(8),
child: Column(children: [
Container(
height: 40,
alignment: Alignment.centerRight,
child: Row(mainAxisSize: MainAxisSize.min, children: [
InkWell(
onTap: () => Navigator.pop(context),
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
child: Text(TranslationBase.of(context).cancel),
),
),
SizedBox(width: 8),
InkWell(
onTap: () {
Navigator.pop(context);
setState(() {
_dobDate = _tempDate;
});
},
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
child: Text(TranslationBase.of(context).ok),
),
)
]),
),
Expanded(
child: Container(
width: MediaQuery.of(context).size.width,
child: CupertinoDatePicker(
initialDateTime: _dobDate,
mode: CupertinoDatePickerMode.date,
onDateTimeChanged: (_date) {
_tempDate = _date;
},
),
),
)
]),
);
});
}, iconData: Icons.calendar_today),
),
SizedBox(height: 12),
_containerWidget(
_commonDropDownView(TranslationBase.of(context).activityLevel, _activityLevelListEng[_selectedActiveLevel - 1], () {
showDialog(
context: context,
child: CommonRadioButtonDialog(
list: _activityLevelListEng,
title: TranslationBase.of(context).activityLevel,
onSelect: (index) {
Navigator.pop(context);
setState(() {
_selectedActiveLevel = index + 1;
});
},
selectedIndex: _selectedActiveLevel - 1,
),
);
}),
),
SizedBox(height: 18),
Text(
TranslationBase.of(context).reminderLabel,
style: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w600,
color: Color(0xff2B353E),
letterSpacing: -0.44,
),
),
SizedBox(height: 12),
_containerWidget(
_commonDropDownView(TranslationBase.of(context).reminderTimesLabel, _remindedTimeListEng[_selectedRemindedTime], () {
showDialog(
context: context,
child: CommonRadioButtonDialog(
list: _remindedTimeListEng,
onSelect: (index) {
Navigator.pop(context);
setState(() {
_selectedRemindedTime = index;
});
},
selectedIndex: _selectedRemindedTime,
),
);
}),
),
],
),
),
),
Container(
color: Colors.white,
padding: EdgeInsets.only(top: 16, bottom: 16, right: 21, left: 21),
child: DefaultButton(
TranslationBase.of(context).save,
_updateUserDetails,
color: Color(0xff349745),
),
),
],
),
);
}
Widget _containerWidget(Widget child) {
return Container(
padding: EdgeInsets.only(left: 16, right: 16, bottom: 15, top: 15),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.white,
border: Border.all(
color: Color(0xffefefef),
width: 1,
),
),
child: child,
);
}
Widget _commonDropDownView(String title, String value, VoidCallback callback, {IconData iconData}) {
return InkWell(
onTap: callback,
child: Row(
children: [
Expanded(
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(
title,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Color(0xff2E303A),
letterSpacing: -0.44,
),
),
Text(
value,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Color(0xff575757),
letterSpacing: -0.56,
),
),
]),
),
Icon(
iconData ?? Icons.keyboard_arrow_down_sharp,
color: Color(0xff2E303A),
)
],
),
);
}
Widget inputWidget(String _labelText, String _hintText, TextEditingController _controller, {String prefix, bool isEnable = true, bool hasSelection = false}) {
return InkWell(
onTap: hasSelection ? () {} : null,
child: Row(
children: [
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_labelText,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Color(0xff2E303A),
letterSpacing: -0.44,
),
),
TextField(
enabled: isEnable,
scrollPadding: EdgeInsets.zero,
keyboardType: TextInputType.number,
controller: _controller,
onChanged: (value) => {
// validateForm()
},
style: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w400,
color: Color(0xff575757),
letterSpacing: -0.56,
),
decoration: InputDecoration(
isDense: true,
hintText: _hintText,
hintStyle: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w400,
color: Color(0xff575757),
letterSpacing: -0.56,
),
prefixIconConstraints: BoxConstraints(minWidth: 50),
prefixIcon: prefix == null
? null
: Text(
"+" + prefix,
style: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w500,
color: Color(0xff2E303A),
letterSpacing: -0.56,
),
),
contentPadding: EdgeInsets.zero,
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
),
),
],
),
),
if (hasSelection) Icon(Icons.keyboard_arrow_down_outlined),
],
),
);
}
Widget _commonRadioButtonsRow(String rightText, String leftText, bool checkParam, Function(bool) callBack) {
return Row(
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(width: 22, height: 22, child: Radio(value: true, groupValue: checkParam, onChanged: callBack)),
SizedBox(width: 8),
Text(
rightText,
style: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w600,
color: Color(0xff575757),
letterSpacing: -0.44,
),
),
],
),
SizedBox(width: 12),
Row(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(width: 22, height: 22, child: Radio(value: false, groupValue: checkParam, onChanged: callBack)),
SizedBox(width: 8),
Text(
leftText,
style: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w600,
color: Color(0xff575757),
letterSpacing: -0.44,
),
),
],
),
],
);
return Row(children: [
Expanded(
child: SizedBox(
height: 40,
child: RaisedButton(
color: checkParam ? Theme.of(context).appBarTheme.color : Colors.white,
child: Text(
rightText,
style: TextStyle(color: checkParam ? Colors.white : Theme.of(context).appBarTheme.color, fontWeight: FontWeight.w600),
),
onPressed: () => callBack(true),
),
),
),
SizedBox(width: 12),
Expanded(
child: SizedBox(
height: 40,
child: RaisedButton(
color: !checkParam ? Theme.of(context).appBarTheme.color : Colors.white,
child: Text(
leftText,
style: TextStyle(color: !checkParam ? Colors.white : Theme.of(context).appBarTheme.color, fontWeight: FontWeight.w600),
),
onPressed: () => callBack(false),
),
),
),
]);
}
Widget _commonInputAndUnitRow(_title, _controller, double _minValue, double _maxValue, double _valueOrg, Function(String) onTextValueChange, Function(double) onValueChange, String unitTitle,
Function(bool) onUnitTap, _list) {
return _containerWidget(
Row(
children: [
Expanded(
flex: 3,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_title,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Color(0xff2E303A),
letterSpacing: -0.44,
),
),
TextField(
controller: _controller,
keyboardType: TextInputType.number,
onChanged: (value) {
double _value = double.parse(value);
if (_value > _maxValue) {
onTextValueChange(_maxValue.toStringAsFixed(0));
onValueChange(_maxValue);
return;
} else if (_value < _minValue) {
onTextValueChange(_minValue.toStringAsFixed(0));
onValueChange(_minValue);
return;
} else if (_value >= _minValue && _value <= _maxValue) {
onValueChange(_value);
return;
}
},
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
],
style: TextStyle(
color: Color(0xff575757),
letterSpacing: -0.56,
),
decoration: InputDecoration(
isDense: true,
hintText: "0",
hintStyle: TextStyle(
fontSize: 14,
height: 21 / 14,
fontWeight: FontWeight.w400,
color: Color(0xff575757),
letterSpacing: -0.56,
),
contentPadding: EdgeInsets.zero,
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
),
),
],
),
),
Container(height: 34, width: 1, color: Color(0xffE0E0E0), margin: EdgeInsets.only(left: 12, right: 12)),
Expanded(
flex: 1,
child: PopupMenuButton(
child: _commonDropDownView(TranslationBase.of(context).unit, unitTitle, null),
onSelected: (value) {
onUnitTap(value);
},
itemBuilder: (context) => _list),
)
],
),
);
}
void _updateUserDetails() async {
_userDetailModel.height = _heightValue;
_userDetailModel.weight = _weightValue;
_userDetailModel.firstName = _nameController.text;
_userDetailModel.isWeightInKG = _isWeightKG;
_userDetailModel.isHeightInCM = _isHeightCM;
_userDetailModel.gender = _isGenderMale ? "M" : "F";
var tempDate = DateUtil.convertDateToString(_dobDate);
if (!tempDate.endsWith("/")) {
tempDate = tempDate + "/";
}
_userDetailModel.dOB = tempDate;
_userDetailModel.activityID = _selectedActiveLevel;
GifLoaderDialogUtils.showMyDialog(context);
await widget.viewModel.updateUserDetail(_userDetailModel, (tag) async {
if (tag) {
AppToast.showSuccessToast(message: TranslationBase.of(context).success);
await prefs.setString(H2O_UNIT, _isUnitML ? "ml" : "l");
int _tempRemindTime = prefs.getInt(H2O_REMINDER) ?? _selectedRemindedTime;
if (_tempRemindTime != _selectedRemindedTime) {
await prefs.setInt(H2O_REMINDER, _selectedRemindedTime);
_addReminderToCalender(_selectedRemindedTime);
}
}
GifLoaderDialogUtils.hideDialog(context);
});
Navigator.pop(context);
}
final CalendarPlugin _myPlugin = CalendarPlugin();
void _addReminderToCalender(int _selectedRemindedTime) async {
bool _calendarPermission = await _myPlugin.hasPermissions();
if (_calendarPermission) {
} else {
await _myPlugin.requestPermissions();
}
_myPlugin.hasPermissions().then((value) {
if (!value) {
_myPlugin.requestPermissions();
} else {
_myPlugin.getCalendars().then((value) => {});
}
});
}
}