I want the user to pick a date using datePicker widget, when the user finish picking a date, a timePicker widget should appear and do the same thing. After that an item will be added to a list which uses the picked date & time. But what really happens is that everything shows up at the same time.
onPressed: () {
var date = DateTime.now();
showMaterialDatePicker(
context: context,
selectedDate: date,
onChanged: (value) => setState(() => date = value),
);
//Should wait until DatePicker finish
var time = TimeOfDay.now();
showMaterialTimePicker(
context: context,
selectedTime: time,
onChanged: (value) => setState(() => time = value),
);
//Should wait until timePicker finish, to add the item to the list
this.widget.litems.add(Reminder(
time: time,
date: date,
));
setState((){});
}
I suggest using the showDatePicker & showTimePicker. As per the documentation showDatePicker and showTimePicker returns Future. You can simply await these for the desired outcome.
onPressed: () async {
var date = DateTime.now();
date = await showDatePicker(
context: context,
initialDate: date,
);
//Should wait until DatePicker finish
var time = TimeOfDay.now();
time = await showTimePicker(
context: context,
initialTime: time,
);
//Should wait until timePicker finish, to add the item to the list
this.widget.litems.add(Reminder(
time: time,
date: date,
));
setState((){});
}
If you don't want to use these, then you should add showMaterialDatePicker & showMaterialTimePicker to separate functions which returns a Future and handle the logic inside those functions.
Related
Having a little hard time doing what I want to achieve.
I am using googlesheets as database, with autosort function to sort the products.
In flutter, after editing 3 products, the flutter app stops "refreshing" so It stops updating inside the app, but in the googlesheets it's always updating.
This is what I currently have to edit the date.
Future<void> _updateDate(
BuildContext context) async {
final DateTime? nD =
await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1901, 1),
lastDate: DateTime(2100));
if (nD != oD) {
await widget.provider
.pushNewDate(_nDF, index);
setState(() {});
//this is what I added after changing some code
Future.delayed(const Duration(seconds: 1),
(() => (setState(() {}))));
///
}
}
If you wrap the _updateDate in a set state it will propably work . If it doesnt try _updateDate.then(SetState(){});
after a long time I figured it out it was not on my end bue on google sheets that took a litle time to update. I went for another route!
I need to update the date display on bottom sheet when I select date on date picker of another bottom sheet, please help me.
date picker
need update date text
You must use StatefulWidget in bottom sheet. With this widget you can setstate in your bottom sheet.
Example usage: https://api.flutter.dev/flutter/widgets/StatefulBuilder-class.html
//used library
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
//declare variables
String hourOfPeriod = "AM";
String hour = "00";
TimeOfDay _time = TimeOfDay(hour: 00, minute: 00);
NumberFormat f = NumberFormat("00", "en_US");
//for displaying TimePicker
void _selectTime() async {
final TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: _time,
helpText: "RESCHEDULE TIME",
);
if (newTime != null) {
setState(() {
_time = newTime;
hourOfPeriod = _time.period == DayPeriod.pm ? "PM" : "AM";
});
}
}
//code inside build(BuildContext context)
InkWell(
onTap: () {
_selectTime();
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("${f.format(_time.hourOfPeriod)}:"),
Text("${f.format(_time.minute)}"),
Text("$hourOfPeriod")
],
),
)
//Output Format: HH:MM AM/PM
I am working on a project which have a complete button:
Expanded(child: ElevatedButton(
onPressed: () {
completeTrip(
list[index]['id']);
},
child: Text("Complete"),
style: ElevatedButton
.styleFrom(
primary: Colors.green,),
),
and i have a date and time in my database:
{
'from_date':'16-01-2022'
'time' :'1:15 PM'
}
what i want is to show that button only when the given is passed, before that this button must not be shown?
is there anything or any way to do it?
Thanks in advance <3.
You can use Stream.periodic
DateTime current = DateTime.now();
Stream timer = Stream.periodic( Duration(seconds: 1), (i){
current = current.add(Duration(seconds: 1));
return current;
});
timer.listen((data){
//if it reached the given time do something on your button
});
and at the end call timer.cancel;
Couldn't figure out if you wanted help in parsing your date and time or using some sort of timer for displaying the button.So I modified #Bunny1376 's answer for adding some things:
Use a boolean to check whether to show button or not:
bool showBtn = false;
In your initState or some other place, parse the date and time you received as a json as :
Map<String,String> _dateTimeJson = {
'from_date':'16-01-2022',
'time': '1:15 PM'
};
String _dateTimeString = _dateTimeJson['from_date'] + ' ' +_dateTimeJson['time'];
DateFormat _format = DateFormat('dd-mm-yyyy HH:mm a');
DateTime _dateTime = _format.parse(_dateTimeString);
Here, we have appended 'time' field with 'from_date' to form a singe dateTime String which later on is parsed as DateTime.I have used intl package for this. For more details: https://pub.dev/packages/intl
Now, add a timer that executes every second to check if current date time is more than your dateTime as:
DateTime current = DateTime.now();
Stream timer = Stream.periodic( Duration(seconds: 1), (i){
current = current.add(Duration(seconds: 1));
return current;
});
timer.listen((data){
if(current.isAfter(_dateTime)){
// show button
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
setState(() {
showBtn = true;
});
});
}
});
The logic of the button would be:
if(showBtn)...[
Expanded(child: ElevatedButton(
onPressed: () {
completeTrip(
list[index]['id']);
},
child: Text("Complete"),
style: ElevatedButton
.styleFrom(
primary: Colors.green,),
),
]
I'm trying to let the user select only a certain time into business-hours (using 24H format), but I don't know how to achieve this. Is there a way to do it? I tried this plugin https://pub.dev/packages/time_picker_widget but it's not available in 24H format and sometimes it doesn't let you select a "correct" time
you need to use alwaysUse24HourFormat in MediaQuery like this
void inputTimeSelect() async {
final TimeOfDay picked = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
child: child,
);
},
);
}
I need to get a time from a timepicker and show it on a text widget.
Here you have the widget that should show the time:
pickedTime = TimeOfDay.now();
ListTile(
title: Text(
" ${pickedTime.hour}:${pickedTime.minute}"),
trailing: Icon(Icons.timer,size: 45,),
onTap: _pickTime,
),
And here you have the function _pickTime:
_pickTime() async{
TimeOfDay time = await showTimePicker(
context: context,
initialTime: pickedTime);
setState(() {
pickedTime = time;
});
}
I have detected an issue when the picked time hour or minute is smaller than 10, the output is as shown in the picture for 04:05:
I would like to show the picked time always in format HH:mm.
The following is an extension on the int class that you can use to enforce two characters for each part of the time:
extension TwoChar on int {
String toTwoChars() {
return this.toString().padLeft(2, '0');
}
}
Then modify your code to use the extension:
title: Text("${pickedTime.hour.toTwoChars()}:${pickedTime.minute.toTwoChars()}"),