i have this ui what i want to achieve is that i want to show data according to the date and also if the number of week is selected 1 then i want to show that same data for seven days from that particular date and after that date i want to remove that item
void chooseDay(CalendarDayModel clickedDay) {
setState(() {
_lastChooseDay = _daysList.indexOf(clickedDay);
for (var day in _daysList) {
day.isChecked = false;
}
CalendarDayModel chooseDay = _daysList[_daysList.indexOf(clickedDay)];
chooseDay.isChecked = true;
dailyPills.clear();
for (var pill in allListOfPills) {
DateTime pillDate =
DateTime.fromMicrosecondsSinceEpoch(pill.time! * 1000);
int? week = pill.howManyWeeks;
int totalDays = week! * 7;
final extraDuration = Duration(days: totalDays);
final startDate = pillDate;
final newDateTime = startDate.add(extraDuration);
if (chooseDay.dayNumber == pillDate.day &&
chooseDay.month == pillDate.month &&
chooseDay.year == pillDate.year) {
log("first condition vhitra");
dailyPills.add(pill);
} else if (pillDate.day + 7 != newDateTime.day + 1) {
dailyPills.add(pill);
} else if (newDateTime.day.isLowerThan(chooseDay.dayNumber!)) {
setState(() {
dailyPills.clear();
});
}
}
dailyPills.sort((pill1, pill2) => pill1.time!.compareTo(pill2.time!));
});
}
this is what i tried need some insight here thanks
Related
i created function in flutter using dart that takes Today date and see if it equal to today or not. and u can use for a week, month..etc
here it is
bool daysPassed(int timeStamp, int days) {
if (DateTime.now()
.difference(DateTime.fromMillisecondsSinceEpoch(timeStamp))
.inDays >=
days) {
return true;
}
return false;
}
so i used this function to see if a one day passed or not , if one day passed make all the variables from shared preference to zero, otherwise get the current variables
here is the logic that i made,
HOME.pref = await SharedPreferences.getInstance();
int? timeStamp = HOME.pref!.getInt('day_timestamp_values_changed');
int? timeStamp1Week = HOME.pref!.getInt('week_timestamp');
setState(() {
if (timeStamp != null) {
if (daysPassed(timeStamp, 1)) {
// 1 means if 1 day is passed
HOME.pref!.setDouble('currentCircleProgressValue', 0);
HOME.pref!.setDouble('currentCircleProgressValue2', 0);
HOME.pref!.setDouble('currentCircleProgressValue3', 0);
HOME.pref!.setInt('iii', 0);
if (HOME.Currentday != 0) {
HOME.Lastday = HOME.pref!.getInt('Currentday')!;
HOME.pref!.setInt('Currentday', 0);
}
} else {
HOME.currentCircleProgressValue =
HOME.pref!.getDouble('currentCircleProgressValue')!;
HOME.currentCircleProgressValue2 =
HOME.pref!.getDouble('currentCircleProgressValue2')!;
HOME.currentCircleProgressValue3 =
HOME.pref!.getDouble('currentCircleProgressValue3')!;
HOME.iii = HOME.pref!.getInt('iii')!;
HOME.Currentday = HOME.pref!.getInt('Currentday')!;
HOME.Lastday = HOME.pref!.getInt('Lastday')!;
}
}
the problem is when i test this out the variables after still the same. nothing happens the variables not initialize to zero.
Can u please help me out ?
Thank u
I am using this code but sometimes it runs the code in wrong order. Is it possible to wait for each for loop to finish before next one starts?
void changeWeek() {
for (int i = 0; i < newactivities.length; i++) {
int year = DateTime.now().year;
DateTime startDate = DateTime(year, 1, 1);
while (startDate.weekday != DateTime.monday) {
startDate = startDate.add(const Duration(days: 1));
}
for (String weekday in newactivities[i].daylistdone) {
int offset = (int.parse(weekday)) + (int.parse(currentweek) - 1) * 7;
DateTime donedate = startDate.add(Duration(days: offset));
oldDatesDone.add(Timestamp.fromDate(donedate));
}
for (String weekday in newactivities[i].daylist) {
int offset =
(int.parse(weekday) - 1) + (int.parse(currentweek) - 1) * 7;
DateTime notdonedate = startDate.add(Duration(days: offset));
oldDatesNotDone.add(Timestamp.fromDate(notdonedate));
}
for (var i in oldDatesDone) {
oldDatesNotDone.remove(i);
}
finaloldDatesDone = newactivities[i].weekdaysdone + oldDatesDone;
finaloldDatesNotDone = newactivities[i].weekdaysnotdone + oldDatesNotDone;
act.add({
'title': newactivities[i].title,
'days': newactivities[i].daylist,
'notificationidnumber': newactivities[i].notificationidnumber,
'daysdone': myData22,
'weekdaysdone': finaloldDatesDone,
'weekdaysnotdone': finaloldDatesNotDone,
'timelist': newactivities[i].timelist,
'time': newactivities[i].time
});
FirebaseFirestore.instance
.collection(widget.user.user.uid)
.doc(documentName)
.update({"activities": act});
}
}
Or can I do it in another way to solve the code?
Your Firebase update returns a Future, so it is an asynchronous operation. To preserve order of calls, you need to await the result of this call:
void changeWeek() async {
// ...
await FirebaseFirestore.instance
.collection(widget.user.user.uid)
.doc(documentName)
.update({"activities": act});
}
Note that you need do declare the changeWeek() function as async for this to work.
I tried to write my own action Future using . clear() and re-generate it in the same place, but it didn't work.
For example, it first generates data for the month of the last image (August), selects May and it clears August data and generates for May.
To explain: I get data for several months or years, for example: August 2022, May 2022, December 2021, etc.
To make the code more optimized I want to generate first for the last incoming item, and then when I click on a certain month in the dropdown, deleting the old data to generate new data for the month and year he chose.
#override
Widget build(BuildContext context) {
final _provider = Provider.of<MapRepository>(context);
currentIndicator = _provider.fieldIndicator;
filterLayer = _provider.filterLayer;
selectedMonth = currentIndicator != null ? currentIndicator!.getMonth : "";
selectedYear = currentIndicator != null ? currentIndicator!.getYear : "";
String dateMonth =
currentIndicator != null ? "$selectedMonth $selectedYear" : "Загрузка";
_controller = FixedExtentScrollController(initialItem: 0);
return BlocConsumer(
bloc: indicatorBloc,
listener: (context, state) {
if (state is FetchedIndicatorsState) {
int lastPresent = 0;
Future.delayed(Duration(seconds: 1)).then((value) {
for (int i = 0; i < indicators.length; i++) {
if (indicators.reversed.toList()[i].isPresent) {
lastPresent = i;
}
dates.add(indicators[i].getDateTime);
}
for (var date in dates) {
String shortMonth = DateFormat("MMM", "ru").format(date);
String year = DateFormat("yy", "ru").format(date);
String month =
DateFormat("MMMM", "ru").format(date).toCapitalized();
indicatorDates['$shortMonth$year'] = '$month ${date.year}';
}
selectIndicator(indicators.reversed.toList()[lastPresent]);
});
}
},
builder: (context, state) {
if (state is FetchedIndicatorsState) {
indicators = state.indicators;
for (int i = 0; i < indicators.length; i++) {
if (indicators[i].getMonth == selectedMonth &&
indicators[i].getYear == selectedYear) {
childrenIndicators.add(buildIndicator(
indicators[i], indicators[(i - 1 < 0) ? 0 : (i - 1)], i));
}
}
return buildBody(context, dateMonth, childrenIndicators);
}
return Container();
},
);
}
OnTap actions:
onTap: () {
_refreshData();
setState(() {
selectedYear = entry.value.substring(entry.value.length - 5);
selectedMonth = entry.value.substring(0, entry.value.length - 5);
});
Navigator.pop(context);
},
My action:
Future _refreshData() async {
await Future.delayed(Duration(seconds: 3));
childrenIndicators.clear();
for (int i = 0; i < indicators.length; i++) {
if (indicators[i].getMonth == selectedMonth &&
indicators[i].getYear == selectedYear) {
childrenIndicators.add(buildIndicator(
indicators[i], indicators[(i - 1 < 0) ? 0 : (i - 1)], i));
}
}
setState(() {});
}
I am trying to compare two DateTimes in flutter and execute a particular function using if statements. However, I have been getting NoSuchMethodError.
Here is my code:
DateTime _myNowTime; DateTime _myClickTime
_getClickTime()async{ FirebaseUser currentUser = await
FirebaseAuth.instance.currentUser();
Firestore.instance.collection('user').document(currentUser.uid).snapshots().listen((event) {
if(event.data.isNotEmpty){ setState(() { _now_time = event['now_time'];
_click_time = event['click_time']; _myNowTime = _now_time.toDate(); _myClickTime = _click_time.toDate();
});
} }); }
I want to create a function:
checkTime(){
if(_myNowTime.compareTo(_myClickTime) >= 30 minutes){
//do something ...}
}
void main() {
var now = DateTime.now();
var yesterday = now.subtract(const Duration(days: 1));
if(now == yesterday) {
print("Dates are the same");
} else print("Dates are not the same");
}
I have a service which returns a list of dates, which the user can opt for a future payment.
In the UI, there are three drop down boxes, one each for year, month and date.
Now when the user selects a particular year, then the months shown in the next drop down should only contain months corresponding to that particular selected year and similarly when the month is selected only the corresponding dates to that particular selected month should be shown.
The service response is something like below :
[
{
"availableDate":"03/13/2020"
},
{
"availableDate":"04/14/2020"
},
{
"availableDate":"01/15/2020"
},
{
"availableDate":"01/16/2020"
},
{
"availableDate":"02/17/2020"
},
{
"availableDate":"02/18/2020"
},
{
"availableDate":"02/22/2021"
}
]
I was able to split out the dates,months and years and when I tried to change values using onChange, didn't get the desired result. Could some one please help me with the logic or maybe give me a link to get started?
I have found out a solution.
First initialize the variables :
List _serviceData = [];
List _yearList = [];
List _monthList = [];
List _dateList = [];
List<SchedulerYear> _yearData = new List<SchedulerYear>();
List<SchedulerMonth> _monthData = new List<SchedulerMonth>();
List<SchedulerDate> _dateData = new List<SchedulerDate>();
var yearData = [];
var monthData = [];
var _loadData, _year, _month, _date;
Models used :
class SchedulerYear {
String year;
String month;
String date;
SchedulerYear({this.year,this.month,this.date});
#override
String toString() {
return '$year $month $date';
}
}
class SchedulerMonth {
String month;
String date;
SchedulerMonth({this.month,this.date});
#override
String toString() {
return '$month $date';
}
}
class SchedulerDate {
String date;
SchedulerDate({this.date});
#override
String toString() {
return '$date';
}
}
And finally the functions :
List<DropdownMenuItem<String>> getMenuItems(List options) {
List<DropdownMenuItem<String>> items = List();
for (String n in options) {
items.add(DropdownMenuItem(value: n, child: Text(n)));
}
return items;
}
_getYears() async {
_serviceData = await serviceHandler.fadfj; // the service response as a List
_yearData = [];
_yearList = [];
_yearItems = [];
for (int i = 0; i < _serviceData.length; i++) {
_loadData = _serviceData[i].toString();
_month = _loadData.split('/')[0];
_date = _loadData.split('/')[1];
_year = _loadData.split('/')[2];
_yearData.add(SchedulerYear(year: _year, month: _month, date: _date));
_yearList.add(_year);
}
_yearList = _yearList.toSet().toList();
_yearItems = getMenuItems(_yearList);
_selectedYear = _yearItems[0].value;
yearData = _yearData;
}
_getMonths(selectedYear) {
_dateItems = [];
_dateItems = getMenuItems(_initialDate);
_selectedDate = _dateItems[0].value;
_yearData = yearData;
_monthData = [];
_monthList = [];
_monthItems = [];
for (int i = 0; i < _yearData.length; i++) {
if (_yearData[i].year == selectedYear) {
_monthData.add(SchedulerMonth(month: _yearData[i].month, date: _yearData[i].date));
_monthList.add(_yearData[i].month);
}
}
monthData = _monthData;
_monthList = _monthList.toSet().toList();
_monthItems = getMenuItems(_monthList);
_selectedMonth = _monthItems[0].value;
}
_getDates(selectedMonth) {
_monthData = monthData;
_dateData = [];
_dateList = [];
_dateItems = [];
for (int i = 0; i < _monthData.length; i++) {
if (_monthData[i].month == selectedMonth) {
_dateData.add(SchedulerDate(date: _monthData[i].date));
_dateList.add(_monthData[i].date);
}
}
_dateList = _dateList.toSet().toList();
_dateItems = getMenuItems(_dateList);
_selectedDate = _dateItems[0].value;
}
_getYears() is called inside init() and the _getMonths(selectedYear) is called onChange of years dropdown button and _getDates(selectedMonth) on the months dropdown button