import 'dart:convert'; import 'package:intl/intl.dart'; import 'package:flutter/material.dart'; import 'package:healthcare_user/common/settings.dart'; import 'package:day_night_time_picker/day_night_time_picker.dart'; class MyMedicineTimings extends StatefulWidget { const MyMedicineTimings({Key? key}) : super(key: key); @override State createState() => _MyMedicineTimingsState(); } class _MyMedicineTimingsState extends State { bool isLoading=false; TimeOfDay selectedTime = TimeOfDay.now(); 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); String wakeUpfForApi=''; String _beforeBreakfastTimeForApi=''; String _afterBreakfastTimeForApi=''; String _beforeLunchTimeForApi=''; String _afterLunchTimeForApi=''; String _eveningTeaTimeForApi=''; String _beforeDinnerTimeForApi=''; String _afterDinnerTimeForApi=''; String _sleepTimeForApi=''; DateTime wakeUp = DateTime.now(); bool iosStyle = true; 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'} ]; Map medicineTimings={}; Future getMedicineTimingsList() async { isLoading=true; var response1= await AppSettings.getMedicineTimings(); setState(() { if(response1!='null'){ medicineTimings=jsonDecode(response1); wakeUpfForApi=medicineTimings['wakeUp']; _beforeBreakfastTimeForApi=medicineTimings['beforeBreakfast']; _afterBreakfastTimeForApi=medicineTimings['afterBreakfast']; _beforeLunchTimeForApi=medicineTimings['beforeLunch']; _afterLunchTimeForApi=medicineTimings['afterLunch']; _eveningTeaTimeForApi=medicineTimings['eveningTea']; _beforeDinnerTimeForApi=medicineTimings['beforeDinner']; _afterDinnerTimeForApi=medicineTimings['afterDinner']; _sleepTimeForApi=medicineTimings['sleep']; } isLoading=false; }); } @override void initState() { getMedicineTimingsList(); super.initState(); } 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 valueOfTiming(int index) { if (index == 0) { _time = _wakeupTime; } else if (index == 1) { _time = _beforeBreakfastTime; } } String formatTimeOfDay(TimeOfDay tod) { final now = new DateTime.now(); final dt = DateTime(now.year, now.month, now.day, tod.hour, tod.minute); final format = DateFormat.jm(); //"6:00 AM" return 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; }); } else if (ind == 2) { setState(() { _afterBreakfastTime = newTime; }); } else if (ind == 3) { setState(() { _beforeLunchTime = newTime; }); } else if (ind == 4) { setState(() { _afterLunchTime = newTime; }); } else if (ind == 5) { setState(() { _eveningTeaTime = newTime; }); } else if (ind == 6) { setState(() { _beforeDinnerTime = newTime; }); } else if (ind == 7) { setState(() { _afterDinnerTime = newTime; }); } else if (ind == 8) { setState(() { _sleepTime = newTime; }); }*/ } 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(); } } Widget renderTimings(){ return Column( children: [ Expanded( child: ListView.separated( separatorBuilder: (context, index) => const Divider( height: 4.0, color: primaryColor, ), //padding: EdgeInsets.all(8), itemCount: timings.length, itemBuilder: (BuildContext context, int index) { return 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(), ), ), GestureDetector( onTap: () { }, child: 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 { editTimingsDialog(); }, child: const Text('Edit'), ), ], ) ) ], ); } editTimingsDialog(){ return showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return AlertDialog( title: const Text('Edit Timings'), content: SingleChildScrollView( child: Container( width: double.maxFinite, child: Column( mainAxisSize: MainAxisSize.min, children: [ ConstrainedBox( constraints: BoxConstraints( maxHeight: MediaQuery.of(context).size.height * 0.4, ), child: ListView.separated( separatorBuilder: (context, index) => const Divider( height: 4.0, color: primaryColor, ), //padding: EdgeInsets.all(8), itemCount: timings.length, itemBuilder: (BuildContext context, int index) { return 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 * .25, child: Text( timings[index]['name'], style: wrapTextStyle(), ), ), GestureDetector( onTap: () { Navigator.of(context).push( showPicker( showSecondSelector: 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: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), textForSelectedTime(index) ], ), ], )), ); }) ), ]), )), actions: [ TextButton( child: Text('Cancel', style: textButtonStyle()), onPressed: () { Navigator.of(context).pop(); }, ), TextButton( child: Text('Save', style: textButtonStyle()), onPressed: () async { }, ), ], ); }); }, ); } Widget renderUi() { return Column( children: [ Expanded( child: ListView.separated( separatorBuilder: (context, index) => const Divider( height: 4.0, color: primaryColor, ), //padding: EdgeInsets.all(8), itemCount: timings.length, itemBuilder: (BuildContext context, int index) { return 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(), ), ), GestureDetector( onTap: () { Navigator.of(context).push( showPicker( showSecondSelector: 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: 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(); payload["wakeUp"] = wakeUpfForApi;//_wakeupTime.hour.toString()+':'+_wakeupTime.minute.toString(); payload["beforeBreakfast"] = _beforeBreakfastTime.hour.toString()+':'+_beforeBreakfastTime.minute.toString(); payload["afterBreakfast"] = _afterBreakfastTime.hour.toString()+':'+_afterBreakfastTime.minute.toString(); payload["beforeLunch"] = _beforeLunchTime.hour.toString()+':'+_beforeLunchTime.minute.toString(); payload["afterLunch"] = _afterLunchTime.hour.toString()+':'+_afterLunchTime.minute.toString(); payload["eveningTea"] = _eveningTeaTime.hour.toString()+':'+_eveningTeaTime.minute.toString(); payload["beforeDinner"] = _beforeDinnerTime.hour.toString()+':'+_beforeDinnerTime.minute.toString(); payload["afterDinner"] = _afterDinnerTime.hour.toString()+':'+_afterDinnerTime.minute.toString(); payload["sleep"] = _sleepTime.hour.toString()+':'+_sleepTime.minute.toString(); var value = await AppSettings.addMedicineTimings(payload); var valueResponse = jsonDecode(value); Navigator.of(context,rootNavigator: true).pop(); getMedicineTimingsList(); } else{ Navigator.of(context,rootNavigator: true).pop(); AppSettings.longFailedToast('Please select timing' ); } } }, child: const Text('Save'), ), SizedBox(width: 20,), ElevatedButton( style: ElevatedButton.styleFrom( primary: primaryColor, // background onPrimary: Colors.white, // foreground ), onPressed: () async {}, child: const Text('Cancel'), ), ], ) ) ], ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppSettings.appBar('My medicine timings'), body: medicineTimings.length==0?renderUi():renderTimings(), /*Container( child: Padding( padding: EdgeInsets.all(10), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox(height: 10,), Text('Wakeup'), SizedBox(height: 10,), Text('Before breakfast'), SizedBox(height: 15,), Text('After breakfast'), SizedBox(height: 15,), Text('Before lunch'), SizedBox(height: 15,), Text('After lunch'), SizedBox(height: 15,), Text('Evening Tea'), SizedBox(height: 15,), Text('Before dinner'), SizedBox(height: 15,), Text('After dinner'), SizedBox(height: 15,), Text('Sleep'), ], ), Column( children: [ GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), SizedBox(height: 5,), GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), SizedBox(height: 5,), GestureDetector( onTap: (){ Navigator.of(context).push( showPicker( showSecondSelector: true, context: context, value: _time, onChange: onTimeChanged, minuteInterval: TimePickerInterval.FIVE, // Optional onChange to receive value as DateTime onChangeDateTime: (DateTime dateTime) { // print(dateTime); debugPrint("[debug datetime]: $dateTime"); }, ), ); }, child: Image( image: AssetImage('images/time.png'), width: 30, height: 30, ), ), ], ), ], ), ], ), ) )*/ ); } }