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.
HMG_Patient_App/lib/pages/medical/active_medications/reminder_page.dart

264 lines
9.7 KiB
Dart

import 'package:diplomaticquarterapp/core/viewModels/medical/ActiveMedicationsViewModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/medical/balance/new_text_Field.dart';
import 'package:diplomaticquarterapp/uitl/CalendarUtils.dart';
import 'package:diplomaticquarterapp/uitl/LocalNotification.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:provider/provider.dart';
// ignore: must_be_immutable
class ReminderPage extends StatefulWidget {
final int frequency;
final int days;
final String itemDescription;
List<DateTime> _scheduleList = List();
DateTime startDay;
DateTime endDay;
ReminderPage({Key key, this.frequency, this.days, this.itemDescription}) {
startDay = DateTime.now();
endDay = DateTime.now().add(Duration(days: days));
int hour = (24 ~/ frequency).round();
int durations = 24 ~/ hour;
for (int count = 0; count < durations; count++) {
_scheduleList.add(DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day, (hour * count)));
}
}
@override
_ReminderPageState createState() => _ReminderPageState();
}
class _ReminderPageState extends State<ReminderPage> {
@override
Widget build(BuildContext context) {
ProjectViewModel projectViewModel = Provider.of(context);
return BaseView<ActiveMedicationsViewModel>(
builder: (_, model, w) => AppScaffold(
baseViewModel: model,
isShowAppBar: true,
appBarTitle: 'Reminder',
body: SingleChildScrollView(
child: Container(
margin: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: double.maxFinite,
child: Texts(
'Please select treatment start day and time to be notified when it\'s time to take the medicine'),
),
Divider(),
SizedBox(
height: 12,
),
Texts('Start day'),
InkWell(
onTap: () {
DatePicker.showDatePicker(context,
showTitleActions: true,
minTime: DateTime(
DateTime.now().year, DateTime.now().month - 1, 1),
maxTime: DateTime.now(), onConfirm: (date) {
print('confirm $date');
setState(() {
widget.startDay = date;
});
},
currentTime: widget.startDay,
locale: projectViewModel.localeType);
},
child: Container(
padding: EdgeInsets.all(12),
width: double.infinity,
height: 65,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: Colors.white),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Texts(getStartDay()),
Icon(
Icons.calendar_today,
color: Colors.black,
)
],
),
),
),
SizedBox(
height: 12,
),
Texts('End day'),
InkWell(
onTap: () {
DatePicker.showDatePicker(context,
showTitleActions: true,
minTime: DateTime(
DateTime.now().year, DateTime.now().month - 1, 1),
maxTime: DateTime.now(), onConfirm: (date) {
print('confirm $date');
setState(() {
widget.endDay = date;
});
},
currentTime: widget.endDay,
locale: projectViewModel.localeType);
},
child: Container(
padding: EdgeInsets.all(12),
width: double.infinity,
height: 65,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: Colors.white),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Texts(getEndDay()),
Icon(
Icons.calendar_today,
color: Colors.black,
)
],
),
),
),
...List.generate(
widget._scheduleList.length,
(index) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 7,
),
Texts('Schedule time'),
SizedBox(
height: 7,
),
InkWell(
onTap: () {
DatePicker.showTimePicker(context,
showTitleActions: true, onConfirm: (date) {
print('confirm $date');
setState(() {
widget._scheduleList[index] = date;
});
},
currentTime: widget._scheduleList[index],
locale: projectViewModel.localeType);
},
child: Container(
padding: EdgeInsets.all(12),
width: double.infinity,
height: 65,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: Colors.white),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Texts(getDateTime(
widget._scheduleList[index])),
Icon(
Icons.access_time,
color: Colors.black,
)
],
),
),
),
],
)),
Container(
width: double.maxFinite,
height: MediaQuery.of(context).size.height * 0.12,
),
],
),
),
),
bottomSheet: Container(
width: double.infinity,
height: MediaQuery.of(context).size.height * 0.1,
padding: EdgeInsets.all(10),
child: SecondaryButton(
label: 'OK',
color: Colors.grey[800],
onTap: () {
schedule();
},
),
),
),
);
}
schedule() async {
List<DateTime> scheduleDateTime =
calculateDaysInterval(widget.startDay, widget.endDay);
// scheduleDateTime.forEach((date) {
// LocalNotification.scheduleNotification(
// scheduledNotificationDateTime: date,
// title: 'Remainder',
// description: widget.itemDescription);
// return;
// });
CalendarUtils calendarUtils = await CalendarUtils.getInstance();
calendarUtils.createOrUpdateEvents(
scheduleList: widget._scheduleList,
description: widget.itemDescription,
title: widget.itemDescription,
scheduleDateTime: scheduleDateTime);
var asd = calendarUtils.writableCalendars;
Navigator.pop(context);
}
List<DateTime> calculateDaysInterval(DateTime startDate, DateTime endDate) {
List<DateTime> days = [];
for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
widget._scheduleList.forEach((element) {
days.add(startDate.add(
Duration(days: i, hours: element.hour, minutes: element.minute)));
});
}
return days;
}
String getStartDay() {
return "${DateUtil.getMonth(widget.startDay.month)} ${widget.startDay.day}, ${widget.startDay.year}";
}
String getEndDay() {
return "${DateUtil.getMonth(widget.endDay.month)} ${widget.endDay.day}, ${widget.endDay.year}";
}
String getDateTime(DateTime dateTime) {
return '${dateTime.hour}:${dateTime.minute}';
}
String getDays() {
return '';
}
}