You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
healthcare-frontend/lib/edit_medicine_timings.dart

464 lines
15 KiB

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:healthcare_user/common/settings.dart';
import 'package:day_night_time_picker/day_night_time_picker.dart';
import 'package:intl/intl.dart';
class EditMedicineTimings extends StatefulWidget {
var medicineTimings;
EditMedicineTimings({this.medicineTimings});
@override
State<EditMedicineTimings> createState() => _EditMedicineTimingsState();
}
class _EditMedicineTimingsState extends State<EditMedicineTimings> {
Time _time = Time(hour: 0, minute: 0, second: 0);
Time _wakeupTime = Time(hour: 0, minute: 0, second: 0);
Time _beforeBreakfastTime = Time(hour: 0, minute: 0, second: 0);
Time _afterBreakfastTime = Time(hour: 0, minute: 0, second: 0);
Time _beforeLunchTime = Time(hour: 0, minute: 0, second: 0);
Time _afterLunchTime = Time(hour: 0, minute: 0, second: 0);
Time _eveningTeaTime = Time(hour: 0, minute: 0, second: 0);
Time _beforeDinnerTime = Time(hour: 0, minute: 0, second: 0);
Time _afterDinnerTime = Time(hour: 0, minute: 0, second: 0);
Time _sleepTime = Time(hour: 0, minute: 0, second: 0);
DateTime wakeUp = DateTime.now();
String wakeUpfForApi='';
String _beforeBreakfastTimeForApi='';
String _afterBreakfastTimeForApi='';
String _beforeLunchTimeForApi='';
String _afterLunchTimeForApi='';
String _eveningTeaTimeForApi='';
String _beforeDinnerTimeForApi='';
String _afterDinnerTimeForApi='';
String _sleepTimeForApi='';
List timings = [
{'name': 'Wakeup'},
{'name': 'Before breakfast'},
{'name': 'After- breakfast'},
{'name': 'Before lunch'},
{'name': 'After lunch'},
{'name': 'Evening tea'},
{'name': 'Before dinner'},
{'name': 'After dinner'},
{'name': 'Sleep'}
];
@override
void initState() {
wakeUpfForApi=widget.medicineTimings['wakeUp'];
_beforeBreakfastTimeForApi=widget.medicineTimings['beforeBreakfast'];
_afterBreakfastTimeForApi=widget.medicineTimings['afterBreakfast'];
_beforeLunchTimeForApi=widget.medicineTimings['beforeLunch'];
_afterLunchTimeForApi=widget.medicineTimings['afterLunch'];
_eveningTeaTimeForApi=widget.medicineTimings['eveningTea'];
_beforeDinnerTimeForApi=widget.medicineTimings['beforeDinner'];
_afterDinnerTimeForApi=widget.medicineTimings['afterDinner'];
_sleepTimeForApi=widget.medicineTimings['sleep'];
super.initState();
}
Widget textForSelectedTime(int index) {
if (index == 0) {
return
Text(
wakeUpfForApi,
style: TextStyle(color: Colors.black),
);
} else if (index == 1) {
return Text(
_beforeBreakfastTimeForApi,
style: TextStyle(color: Colors.black),
);
} else if (index == 2) {
return Text(
_afterBreakfastTimeForApi,
style: TextStyle(color: Colors.black),
);
} else if (index == 3) {
return Text(
_beforeLunchTimeForApi,
style: TextStyle(color: Colors.black),
);
} else if (index == 4) {
return Text(
_afterLunchTimeForApi,
style: TextStyle(color: Colors.black),
);
} else if (index == 5) {
return Text(
_eveningTeaTimeForApi,
style: TextStyle(color: Colors.black),
);
} else if (index == 6) {
return Text(
_beforeDinnerTimeForApi,
style: TextStyle(color: Colors.black),
);
} else if (index == 7) {
return Text(
_afterDinnerTimeForApi,
style: TextStyle(color: Colors.black),
);
} else if (index == 8) {
return Text(
_sleepTimeForApi,
style: TextStyle(color: Colors.black),
);
} else {
return Container();
}
}
void onTimeChanged(Time newTime, int index) {
setState(() {
_time=newTime;
});
if (index == 0) {
setState(() {
_wakeupTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _wakeupTime.hour, _wakeupTime.minute);
final format = DateFormat.jm();
setState(() {
wakeUpfForApi=format.format(dt);
});
}
else if (index == 1) {
setState(() {
_beforeBreakfastTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _beforeBreakfastTime.hour, _beforeBreakfastTime.minute);
final format = DateFormat.jm();
setState(() {
_beforeBreakfastTimeForApi=format.format(dt);
});
}
else if (index == 2) {
setState(() {
_afterBreakfastTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _afterBreakfastTime.hour, _afterBreakfastTime.minute);
final format = DateFormat.jm();
setState(() {
_afterBreakfastTimeForApi=format.format(dt);
});
}
else if (index == 3) {
setState(() {
_beforeLunchTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _beforeLunchTime.hour, _beforeLunchTime.minute);
final format = DateFormat.jm();
setState(() {
_beforeLunchTimeForApi=format.format(dt);
});
}
else if (index == 4) {
setState(() {
_afterLunchTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _afterLunchTime.hour, _afterLunchTime.minute);
final format = DateFormat.jm();
setState(() {
_afterLunchTimeForApi=format.format(dt);
});
}
else if (index == 5) {
setState(() {
_eveningTeaTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _eveningTeaTime.hour, _eveningTeaTime.minute);
final format = DateFormat.jm();
setState(() {
_eveningTeaTimeForApi=format.format(dt);
});
}
else if (index == 6) {
setState(() {
_beforeDinnerTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _beforeDinnerTime.hour, _beforeDinnerTime.minute);
final format = DateFormat.jm();
setState(() {
_beforeDinnerTimeForApi=format.format(dt);
});
}
else if (index == 7) {
setState(() {
_afterDinnerTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _afterDinnerTime.hour, _afterDinnerTime.minute);
final format = DateFormat.jm();
setState(() {
_afterDinnerTimeForApi=format.format(dt);
});
}
else if (index == 8) {
setState(() {
_sleepTime = newTime;
});
final now = new DateTime.now();
final dt = DateTime(now.year, now.month, now.day, _sleepTime.hour, _sleepTime.minute);
final format = DateFormat.jm();
setState(() {
_sleepTimeForApi=format.format(dt);
});
}
}
void _selectedDateWithTime(DateTime newTime,int ind){
if (ind == 0) {
setState(() {
wakeUp = newTime;
String formattedDate = DateFormat('yyyy-MM-dd kk:mm').format(wakeUp);
// DateTime parseDate = new DateFormat("dd-MM-yyyy HH:mm:ss").parse(wakeUp);
print(formattedDate);
});
}
/* else if (ind == 1) {
setState(() {
_beforeBreakfastTime = newTime as Time ;
});
}
else if (ind == 2) {
setState(() {
_afterBreakfastTime = newTime as Time;
});
}
else if (ind == 3) {
setState(() {
_beforeLunchTime = newTime as Time;
});
}
else if (ind == 4) {
setState(() {
_afterLunchTime = newTime as Time;
});
}
else if (ind == 5) {
setState(() {
_eveningTeaTime = newTime as Time;
});
}
else if (ind == 6) {
setState(() {
_beforeDinnerTime = newTime as Time;
});
}
else if (ind == 7) {
setState(() {
_afterDinnerTime = newTime as Time;
});
}
else if (ind == 8) {
setState(() {
_sleepTime = newTime as Time;
});
}*/
}
Widget renderTimings(){
return Column(
children: [
Expanded(
child: ListView.separated(
separatorBuilder: (context, index) => const Divider(
height: 4.0,
color: Colors.transparent,
),
//padding: EdgeInsets.all(8),
itemCount: timings.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(
showPicker(
showSecondSelector: true,
accentColor: primaryColor,
okStyle: TextStyle(color: primaryColor,fontWeight: FontWeight.bold),
cancelStyle: TextStyle(color: Colors.red,fontWeight: FontWeight.bold),
barrierDismissible: false,
blurredBackground: true,
borderRadius: 20,
disableAutoFocusToNextInput: true,
displayHeader: true,
context: context,
value: _time,
//onChange: onTimeChanged,
onChange: (val) {
onTimeChanged(val, index);
},
is24HrFormat: false,
sunrise: TimeOfDay(
hour: 6, minute: 0), // optional
sunset: TimeOfDay(hour: 18, minute: 0),
minuteInterval: TimePickerInterval.FIVE,
// Optional onChange to receive value as DateTime
onChangeDateTime: (DateTime dateTime) {
_selectedDateWithTime(dateTime,index);
},
),
);
},
child: Card(
child: Padding(
padding: EdgeInsets.all(10),
child: Container(
padding: EdgeInsets.fromLTRB(8, 8, 0, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
width:
MediaQuery.of(context).size.width * .40,
child: Text(
timings[index]['name'],
style: wrapTextStyle(),
),
),
Image(
image: AssetImage('images/time.png'),
width: 30,
height: 30,
),
textForSelectedTime(index)
],
),
],
)),
),
),
);
})),
Padding(padding: EdgeInsets.fromLTRB(10, 10, 10, 40),
child: Row(
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor, // background
onPrimary: Colors.white, // foreground
),
onPressed: () async {
AppSettings.preLoaderDialog(context);
bool isOnline = await AppSettings.internetConnectivity();
if(isOnline){
if(_time!=TimeOfDay(hour: 0,minute: 0)){
var payload = new Map<String, dynamic>();
payload["wakeUp"] = wakeUpfForApi;//_wakeupTime.hour.toString()+':'+_wakeupTime.minute.toString();
payload["beforeBreakfast"] = _beforeBreakfastTimeForApi;
payload["afterBreakfast"] = _afterBreakfastTimeForApi;
payload["beforeLunch"] = _beforeLunchTimeForApi;
payload["afterLunch"] = _afterLunchTimeForApi;
payload["eveningTea"] = _eveningTeaTimeForApi;
payload["beforeDinner"] = _beforeDinnerTimeForApi;
payload["afterDinner"] = _afterDinnerTimeForApi;
payload["sleep"] = _sleepTimeForApi;
var value = await AppSettings.updateMedicineTimings(payload);
var valueResponse = jsonDecode(value);
Navigator.of(context,rootNavigator: true).pop();
Navigator.pop(context);
//getMedicineTimingsList();
}
else{
Navigator.of(context,rootNavigator: true).pop();
AppSettings.longFailedToast('Please select timing' );
}
}
else{
Navigator.of(context,rootNavigator: true).pop();
AppSettings.longFailedToast('Please check internet' );
}
},
child: const Text('Update'),
),
],
)
)
],
);
}
Future<bool> onWillPop() async {
final shouldPop = await showDialog<bool>(context: context, builder: (context) {
return AlertDialog(
title: const Text('Do you want to leave this page',
style: TextStyle(
color: primaryColor,
fontSize: 20,
)),
actionsAlignment: MainAxisAlignment.spaceBetween,
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: const Text('Yes',
style: TextStyle(
color: primaryColor,
fontSize: 20,
)),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: const Text('No',
style: TextStyle(
color: primaryColor,
fontSize: 20,
)),
),
],
);
},
);
return shouldPop!;
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => onWillPop(),
child:Scaffold(
appBar: AppSettings.appBar('Edit Medicine Timings'),
body: Container(
child: //Text(widget.medicineTimings.toString())
renderTimings(),
)
//medicineTimings.length==0?renderUi():renderTimings(),
) ,);
}
}