Convert DateTime into millisecondsSinceEpoch using DateTimeField in flutter - flutter

I need to convert the user inputted time to milli seconds Since Epoch (millisecondsSinceEpoch). I was looking for a solution for this but I could found only millisecondsSinceEpoch to regular time. So here is my code. I tried to do this in this way. Anyone can help, I really appreciate it.
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
_timeDateConvertToMili createState() => _timeDateConvertToMili();
}
class _timeDateConvertToMili extends State<MyApp>{
final format = DateFormat("dd-MM-yyyy HH:mm");
final initialValue = DateTime.now();
bool autoValidate = false;
bool showResetIcon = true;
DateTime value = DateTime.now();
TextEditingController _startTimeController = TextEditingController();
TextEditingController _endTimeController = TextEditingController();
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Container(
child: Column(
children: [
SizedBox(height: 50,),
Container(
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
border: Border(
// bottom: BorderSide(color: Colors.grey[100])
)),
child: DateTimeField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Start Time',
labelStyle: TextStyle(color: Colors.black),
),
style: TextStyle(
color: Colors.black,
),
controller: _startTimeController,
format: format,
onShowPicker: (context, currentValue) async {
final date = await showDatePicker(
context: context,
firstDate: DateTime(2000),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime(2100));
if (date != null) {
final time = await showTimePicker(
context: context,
initialTime:
TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
);
return DateTimeField.combine(date, time);
} else {
return currentValue;
}
},
resetIcon: showResetIcon ? Icon(Icons.delete) : null,
),
),
Container(
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
border: Border(
// bottom: BorderSide(color: Colors.grey[100])
)),
child: DateTimeField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'End Time',
labelStyle: TextStyle(color: Colors.black),
),
style: TextStyle(
color: Colors.black,
),
controller: _endTimeController,
format: format,
onShowPicker: (context, currentValue) async {
final date = await showDatePicker(
context: context,
firstDate: DateTime(2000),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime(2100));
if (date != null) {
final time = await showTimePicker(
context: context,
initialTime:
TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
);
return DateTimeField.combine(date, time);
} else {
return currentValue;
}
},
resetIcon: showResetIcon ? Icon(Icons.delete) : null,
),
),
Container(
child: RaisedButton(
onPressed: () {
convertNow();
},
child: Text('Convert to Epoch'),
),
),
Container(
child: Text('Result is :'
),
),
],
),
),
),
);
}
void convertNow() {
print ('inputted time is : $_startTimeController');
//var unix = DateTime.parse(_startTimeController.toString()).millisecondsSinceEpoch / 1000;
}
}
Thanks in advance

Here I got the result.
Change the time date format final format = DateFormat("yyyy-MM-dd HH:mm");
void convertNow() {
var inputedStartTime = DateTime.parse(startTimeController.text);
var mili = inputedStartTime.millisecondsSinceEpoch/1000;
var startTime = mili.toInt();
var inputedEndtTime = DateTime.parse(endTimeController.text);
var mili2 = inputedEndtTime.millisecondsSinceEpoch/1000;
var endTime = mili2.toInt();
<!--Result see on run / terminal-->
print ('Converted start time is $startTime');
print ('Converted end time is $endTime');
}

Related

Not able to remove focus from input field

I have four textfields, a title field, a details field, a date field, and a time field. Both the date and time fields are wrapped within a gesture detector, and onTap calls a pickDateAndTime method. The problem is that when I click on the date field and try to manually change the time through the input rather than the dial way, the focus goes to the title field and when I am still on the time picker and type something in the time picker, the title field gets changed with the new input. The weird part is that this error just appeared out of nowhere, and there are no errors reported in the console.
class TodoScreen extends StatefulWidget {
final int? todoIndex;
final int? arrayIndex;
const TodoScreen({Key? key, this.todoIndex, this.arrayIndex})
: super(key: key);
#override
State<TodoScreen> createState() => _TodoScreenState();
}
class _TodoScreenState extends State<TodoScreen> {
final ArrayController arrayController = Get.find();
final AuthController authController = Get.find();
final String uid = Get.find<AuthController>().user!.uid;
late TextEditingController _dateController;
late TextEditingController _timeController;
late TextEditingController titleEditingController;
late TextEditingController detailEditingController;
late String _setTime, _setDate;
late String _hour, _minute, _time;
late String dateTime;
late bool done;
#override
void initState() {
super.initState();
String title = '';
String detail = '';
String date = '';
String? time = '';
if (widget.todoIndex != null) {
title = arrayController
.arrays[widget.arrayIndex!].todos![widget.todoIndex!].title ??
'';
detail = arrayController
.arrays[widget.arrayIndex!].todos![widget.todoIndex!].details ??
'';
date = arrayController
.arrays[widget.arrayIndex!].todos![widget.todoIndex!].date!;
time = arrayController
.arrays[widget.arrayIndex!].todos![widget.todoIndex!].time;
}
_dateController = TextEditingController(text: date);
_timeController = TextEditingController(text: time);
titleEditingController = TextEditingController(text: title);
detailEditingController = TextEditingController(text: detail);
done = (widget.todoIndex == null)
? false
: arrayController
.arrays[widget.arrayIndex!].todos![widget.todoIndex!].done!;
}
DateTime selectedDate = DateTime.now();
TimeOfDay selectedTime = TimeOfDay(
hour: (TimeOfDay.now().minute > 55)
? TimeOfDay.now().hour + 1
: TimeOfDay.now().hour,
minute: (TimeOfDay.now().minute > 55) ? 0 : TimeOfDay.now().minute + 5);
Future<DateTime?> _selectDate() => showDatePicker(
builder: (context, child) {
return datePickerTheme(child);
},
initialEntryMode: DatePickerEntryMode.calendarOnly,
context: context,
initialDate: selectedDate,
initialDatePickerMode: DatePickerMode.day,
firstDate: DateTime.now(),
lastDate: DateTime(DateTime.now().year + 5));
Future<TimeOfDay?> _selectTime() => showTimePicker(
builder: (context, child) {
return timePickerTheme(child);
},
context: context,
initialTime: selectedTime,
initialEntryMode: TimePickerEntryMode.input);
Future _pickDateTime() async {
DateTime? date = await _selectDate();
if (date == null) return;
if (date != null) {
selectedDate = date;
_dateController.text = DateFormat("MM/dd/yyyy").format(selectedDate);
}
TimeOfDay? time = await _selectTime();
if (time == null) {
_timeController.text = formatDate(
DateTime(
DateTime.now().year,
DateTime.now().day,
DateTime.now().month,
DateTime.now().hour,
DateTime.now().minute + 5),
[hh, ':', nn, " ", am]).toString();
}
if (time != null) {
selectedTime = time;
_hour = selectedTime.hour.toString();
_minute = selectedTime.minute.toString();
_time = '$_hour : $_minute';
_timeController.text = _time;
_timeController.text = formatDate(
DateTime(2019, 08, 1, selectedTime.hour, selectedTime.minute),
[hh, ':', nn, " ", am]).toString();
}
}
#override
Widget build(BuildContext context) {
bool visible =
(_dateController.text.isEmpty && _timeController.text.isEmpty)
? false
: true;
final formKey = GlobalKey<FormState>();
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text((widget.todoIndex == null) ? 'New Task' : 'Edit Task',
style: menuTextStyle),
leadingWidth: (MediaQuery.of(context).size.width < 768) ? 90.0 : 100.0,
leading: Center(
child: Padding(
padding: (MediaQuery.of(context).size.width < 768)
? const EdgeInsets.only(left: 0)
: const EdgeInsets.only(left: 21.0),
child: TextButton(
style: const ButtonStyle(
splashFactory: NoSplash.splashFactory,
),
onPressed: () {
Get.back();
},
child: Text(
"Cancel",
style: paragraphPrimary,
),
),
),
),
centerTitle: true,
actions: [
Center(
child: Padding(
padding: (MediaQuery.of(context).size.width < 768)
? const EdgeInsets.only(left: 0)
: const EdgeInsets.only(right: 21.0),
child: TextButton(
style: const ButtonStyle(
splashFactory: NoSplash.splashFactory,
),
onPressed: () async {
},
child: Text((widget.todoIndex == null) ? 'Add' : 'Update',
style: paragraphPrimary),
),
),
)
],
),
body: SafeArea(
child: Container(
width: double.infinity,
padding: (MediaQuery.of(context).size.width < 768)
? const EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0)
: const EdgeInsets.symmetric(horizontal: 35.0, vertical: 15.0),
child: Column(
children: [
Form(
key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
validator: Validator.titleValidator,
controller: titleEditingController,
autofocus: true, // problem here
autocorrect: false,
cursorColor: Colors.grey,
maxLines: 1,
maxLength: 25,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
counterStyle: counterTextStyle,
hintStyle: hintTextStyle,
hintText: "Title",
border: InputBorder.none),
style: todoScreenStyle),
primaryDivider,
TextField(
controller: detailEditingController,
maxLines: null,
autocorrect: false,
cursorColor: Colors.grey,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
counterStyle: counterTextStyle,
hintStyle: hintTextStyle,
hintText: "Notes",
border: InputBorder.none),
style: todoScreenDetailsStyle),
],
),
),
Visibility(
visible: (widget.todoIndex != null) ? true : false,
child: GestureDetector(
onTap: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Completed",
style: todoScreenStyle,
),
Transform.scale(
scale: 1.3,
child: Theme(
data: ThemeData(
unselectedWidgetColor: const Color.fromARGB(
255, 187, 187, 187)),
child: Checkbox(
shape: const CircleBorder(),
checkColor: Colors.white,
activeColor: primaryColor,
value: done,
side: Theme.of(context).checkboxTheme.side,
onChanged: (value) {
setState(() {
done = value!;
});
})),
)
],
),
),
),
GestureDetector(
onTap: () async {
await _pickDateTime();
setState(() {
visible = true;
});
},
child: Column(
children: [
Row(
children: [
Flexible(
child: TextField(
enabled: false,
controller: _dateController,
onChanged: (String val) {
_setDate = val;
},
decoration: InputDecoration(
hintText: "Date",
hintStyle: hintTextStyle,
border: InputBorder.none),
style: todoScreenStyle,
),
),
visible
? IconButton(
onPressed: () {
_dateController.clear();
_timeController.clear();
setState(() {});
},
icon: const Icon(
Icons.close,
color: Colors.white,
))
: Container()
],
),
primaryDivider,
TextField(
onChanged: (String val) {
_setTime = val;
},
enabled: false,
controller: _timeController,
decoration: InputDecoration(
hintText: "Time",
hintStyle: hintTextStyle,
border: InputBorder.none),
style: todoScreenStyle,
)
],
),
),
],
),
),
),
);
}
}
Should I open an issue on Github, as I had not made any changes to the code for it behave this way and also because there were no errors in the console
Here is the full code on Github
Update
Here is a reproducible example:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const TodoScreen(),
);
}
}
class TodoScreen extends StatefulWidget {
const TodoScreen({Key? key}) : super(key: key);
#override
State<TodoScreen> createState() => _TodoScreenState();
}
class _TodoScreenState extends State<TodoScreen> {
late TextEditingController _dateController;
late TextEditingController _timeController;
late TextEditingController titleEditingController;
late TextEditingController detailEditingController;
late String _setTime, _setDate;
late String _hour, _minute, _time;
late String dateTime;
#override
void initState() {
super.initState();
String title = '';
String detail = '';
String date = '';
String? time = '';
_dateController = TextEditingController(text: date);
_timeController = TextEditingController(text: time);
titleEditingController = TextEditingController(text: title);
detailEditingController = TextEditingController(text: detail);
}
#override
void dispose() {
super.dispose();
titleEditingController.dispose();
detailEditingController.dispose();
_timeController.dispose();
_dateController.dispose();
}
Theme timePickerTheme(child) => Theme(
data: ThemeData.dark().copyWith(
timePickerTheme: TimePickerThemeData(
backgroundColor: const Color.fromARGB(255, 70, 70, 70),
dayPeriodTextColor: Colors.green,
hourMinuteTextColor: MaterialStateColor.resolveWith((states) =>
states.contains(MaterialState.selected)
? Colors.white
: Colors.white),
dialHandColor: Colors.green,
helpTextStyle: TextStyle(
fontSize: 12, fontWeight: FontWeight.bold, color: Colors.green),
dialTextColor: MaterialStateColor.resolveWith((states) =>
states.contains(MaterialState.selected)
? Colors.white
: Colors.white),
entryModeIconColor: Colors.green,
),
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
foregroundColor:
MaterialStateColor.resolveWith((states) => Colors.green)),
),
),
child: child!,
);
Theme datePickerTheme(child) => Theme(
data: ThemeData.dark().copyWith(
colorScheme: ColorScheme.dark(
surface: Colors.green,
secondary: Colors.green,
onPrimary: Colors.white,
onSurface: Colors.white,
primary: Colors.green,
)),
child: child!,
);
DateTime selectedDate = DateTime.now();
TimeOfDay selectedTime = TimeOfDay(
hour: (TimeOfDay.now().minute > 55)
? TimeOfDay.now().hour + 1
: TimeOfDay.now().hour,
minute: (TimeOfDay.now().minute > 55) ? 0 : TimeOfDay.now().minute + 5);
Future<DateTime?> _selectDate() => showDatePicker(
builder: (context, child) {
return datePickerTheme(child);
},
initialEntryMode: DatePickerEntryMode.calendarOnly,
context: context,
initialDate: selectedDate,
initialDatePickerMode: DatePickerMode.day,
firstDate: DateTime.now(),
lastDate: DateTime(DateTime.now().year + 5));
Future<TimeOfDay?> _selectTime() => showTimePicker(
builder: (context, child) {
return timePickerTheme(child);
},
context: context,
initialTime: selectedTime,
initialEntryMode: TimePickerEntryMode.input);
Future _pickDateTime() async {
DateTime? date = await _selectDate();
if (date == null) return;
if (date != null) {
selectedDate = date;
_dateController.text = selectedDate.toString();
}
TimeOfDay? time = await _selectTime();
if (time != null) {
selectedTime = time;
_hour = selectedTime.hour.toString();
_minute = selectedTime.minute.toString();
_time = '$_hour : $_minute';
_timeController.text = _time;
_timeController.text =
DateTime(2019, 08, 1, selectedTime.hour, selectedTime.minute)
.toString();
}
}
#override
Widget build(BuildContext context) {
bool visible =
(_dateController.text.isEmpty && _timeController.text.isEmpty)
? false
: true;
final formKey = GlobalKey<FormState>();
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
centerTitle: true,
),
body: SafeArea(
child: Container(
width: double.infinity,
padding: (MediaQuery.of(context).size.width < 768)
? const EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0)
: const EdgeInsets.symmetric(horizontal: 35.0, vertical: 15.0),
child: Column(
children: [
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(14.0)),
padding: const EdgeInsets.symmetric(
horizontal: 24.0, vertical: 15.0),
child: Form(
key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
controller: titleEditingController,
autofocus: true,
autocorrect: false,
cursorColor: Colors.grey,
maxLines: 1,
maxLength: 25,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
hintText: "Title", border: InputBorder.none),
),
Divider(color: Colors.black),
TextField(
controller: detailEditingController,
maxLines: null,
autocorrect: false,
cursorColor: Colors.grey,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
hintText: "Notes", border: InputBorder.none),
),
],
),
)),
GestureDetector(
onTap: () async {
await _pickDateTime();
setState(() {
visible = true;
});
},
child: Container(
margin: const EdgeInsets.only(top: 20.0),
width: double.infinity,
padding: const EdgeInsets.symmetric(
horizontal: 24.0, vertical: 15.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14.0)),
child: Column(
children: [
Row(
children: [
Flexible(
child: TextField(
enabled: false,
controller: _dateController,
onChanged: (String val) {
_setDate = val;
},
decoration: InputDecoration(
hintText: "Date", border: InputBorder.none),
),
),
visible
? IconButton(
onPressed: () {
_dateController.clear();
_timeController.clear();
setState(() {});
},
icon: const Icon(
Icons.close,
color: Colors.white,
))
: Container()
],
),
Divider(
color: Colors.blue,
),
TextField(
onChanged: (String val) {
_setTime = val;
},
enabled: false,
controller: _timeController,
decoration: InputDecoration(
hintText: "Enter", border: InputBorder.none),
)
],
)),
),
],
),
),
),
);
}
}
In your main.dart file, you should return something like this:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
// This allows closing keyboard when tapping outside of a text field
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus &&
currentFocus.focusedChild != null) {
FocusManager.instance.primaryFocus!.unfocus();
}
},
child: // your app's entry point,
);
}
}
Add a focusNode in your textField:
FocusNode focusNode = FocusNode();
TextField(
focusNode: focusNode,
);
And then in the gesture detector, add that following code to unselect the textfield input:
FocusScope.of(context).requestFocus(FocusNode());
simply wrap your Scaffold widget GestureDetector and add FocusScope.of(context).requestFocus(FocusNode()); it will automatically unfocused text field when you click anywhere on your screen
GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(FocusNode());
},
child: Scaffold()
)
You can use below code to remove focus in gesture detector event
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
currentFocus.unfocus();
}

How to disable Flutter DatePicker?

I have an issue with DatePicker in my application. Here's a simple TextFormField that I've created in my app which will open up DatePicker whenever the user taps on it.
This widget is a part of a form where I also have specified the GlobalKey and TextController for it. The rightmost calendar icon uses the suffixIcon property of InputDecoration and it changes to a clear icon whenever the user selects a date.
Here's the code for the above widget.
TextFormField(
onTap: () => _selectStartDate(context),
controller: _startDateTextController,
keyboardType: TextInputType.datetime,
readOnly: true,
decoration: InputDecoration(
suffixIcon: showClear ? IconButton(
icon: Icon(Icons.clear),
onPressed: _clearStartDate,
) : Icon(Icons.date_range),
labelText: 'Start Date',
labelStyle: TextStyle(
fontSize: AppDimensions.font26,
color: AppColors.paraColor
),
),
validator: (value){
if(value!.isEmpty) {
return 'Please enter a date';
}
},
),
My goal is to let the user pick on a date, and clear it should they choose to do so.
Here's the code for the _selectStartDate and _clearStartDate functions as well as the necessary controller and key. I'm using the intl package to format the date.
final _formKey = GlobalKey<FormState>();
TextEditingController _startDateTextController = TextEditingController();
DateTime selectedStartDate = DateTime.now();
bool showClear = false;
_selectStartDate(BuildContext context) async {
final DateTime? newStartDate = await showDatePicker(
context: context,
initialDate: selectedStartDate,
firstDate: DateTime(1900),
lastDate: DateTime(2100),
helpText: 'STARTING DATE'
);
if(newStartDate != null && newStartDate != selectedStartDate) {
setState(() {
selectedStartDate = newStartDate;
_startDateTextController.text = DateFormat.yMMMd().format(selectedStartDate);
showClear = true;
});
}
}
_clearStartDate() {
_startDateTextController.clear();
setState(() {
showClear = !showClear;
});
}
When i run the app, the DatePicker pops up and I'm able to select a date. The date is then shown on the TextFormField like the image below.
As you can see the clear icon is displayed. However, when i clicked on it, the DatePicker still popped up. And when i clicked on cancel on the DatePicker window, the TextFormField is cleared as expected.
Here's the complete code.
class BookingForm extends StatefulWidget {
const BookingForm({Key? key}) : super(key: key);
#override
_BookingFormState createState() => _BookingFormState();
}
class _BookingFormState extends State<BookingForm> {
final _formKey = GlobalKey<FormState>();
TextEditingController _startDateTextController = TextEditingController();
DateTime selectedStartDate = DateTime.now();
bool showClear = false;
_selectStartDate(BuildContext context) async {
final DateTime? newStartDate = await showDatePicker(
context: context,
initialDate: selectedStartDate,
firstDate: DateTime(1900),
lastDate: DateTime(2100),
helpText: 'STARTING DATE'
);
if(newStartDate != null && newStartDate != selectedStartDate) {
setState(() {
selectedStartDate = newStartDate;
_startDateTextController.text = DateFormat.yMMMd().format(selectedStartDate);
showClear = true;
});
}
}
_clearStartDate() {
_startDateTextController.clear();
setState(() {
showClear = !showClear;
});
}
#override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Container(
margin: EdgeInsets.only(left: AppDimensions.width20, right: AppDimensions.width20),
padding: EdgeInsets.all(AppDimensions.height20),
decoration: BoxDecoration(
color: Colors.white70,
borderRadius: BorderRadius.circular(AppDimensions.radius20),
boxShadow: [
BoxShadow(
color: Color(0xFFe8e8e8),
blurRadius: 5.0,
spreadRadius: 1.0,
offset: Offset(2,2)
),
]
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
TextFormField(
onTap: () => _selectStartDate(context),
controller: _startDateTextController,
keyboardType: TextInputType.datetime,
readOnly: true,
decoration: InputDecoration(
suffixIcon: showClear ? IconButton(
icon: Icon(Icons.clear),
onPressed: _clearStartDate,
) : Icon(Icons.date_range),
labelText: 'Start Date',
labelStyle: TextStyle(
fontSize: AppDimensions.font26,
color: AppColors.paraColor
),
),
validator: (value){
if(value!.isEmpty) {
return 'Please enter a date';
}
},
),
ElevatedButton(
style: ElevatedButton.styleFrom(
padding: EdgeInsets.only(
top: AppDimensions.height10,
bottom: AppDimensions.height10,
left: AppDimensions.width45,
right: AppDimensions.width45
),
primary: AppColors.mainColor2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppDimensions.radius20)
)
),
child: SmallText(
text: 'Search',
size: AppDimensions.font26,
color: Colors.white,
),
onPressed: (){
if(_formKey.currentState!.validate()) {
_formKey.currentState!.save();
}
}
),
],
),
),
);
}
}
I'm not sure why this happens. Any help is greatly appreciated. Thank you

How can i show a 24 hour format after a showTimePicker selection in flutter?

I have created a showTimePicker dialogue and I can clearly select a specific hour in 24 hour format but i cannot display it in a text field in 24 Hour format. It falls back to a 12 Hrs format.
Here is the code i used.
GestureDetector(
onTap: () async {
final TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: trainingStartTime,
builder: (BuildContext context, Widget? child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
child: child!,
);
},
);
if (newTime != null) {
setState(() {
trainingStartTime = newTime;
});
}
},
child: Container(
width: size.width * 0.12,
height: size.height * 0.05,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade400),
borderRadius: BorderRadius.circular(5)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
'${trainingStartTime.format(context)}',
style: const TextStyle(
//color: Colors.grey,
fontSize: 18,
),
),
Icon(Icons.access_time),
],
),
),
),
Format your selected time like this,
if (newTime != null) {
final localizations = MaterialLocalizations.of(context);
String formattedTime = localizations.formatTimeOfDay(newTime, alwaysUse24HourFormat: true);
setState(() {
initialTime = newTime;
trainingStartTime = formattedTime;
});
}
initialTime variable is used to display the selected time inside the timePicker widget and trainingStartTime displays the time inside the Text widget.
Here is the full code,
import 'package:flutter/material.dart';
class TimeExample extends StatefulWidget {
static var tag = "/TimeExample";
const TimeExample({Key? key}) : super(key: key);
#override
_TimeExampleState createState() => _TimeExampleState();
}
class _TimeExampleState extends State<TimeExample> {
String? trainingStartTime;
TimeOfDay initialTime = TimeOfDay.now();
late Size size;
#override
Widget build(BuildContext context) {
size = MediaQuery.of(context).size;
return Scaffold(
body: Center(
child: GestureDetector(
onTap: () async {
final TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: initialTime,
builder: (BuildContext context, Widget? child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
child: child!,
);
},
);
if (newTime != null) {
final localizations = MaterialLocalizations.of(context);
String formattedTime = localizations.formatTimeOfDay(newTime, alwaysUse24HourFormat: true);
setState(() {
initialTime = newTime;
trainingStartTime = formattedTime;
});
}
},
child: Container(
width: size.width * 0.9,
height: size.height * 0.05,
decoration: BoxDecoration(border: Border.all(color: Colors.grey.shade400), borderRadius: BorderRadius.circular(5)),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [Expanded(child: Text(trainingStartTime ?? 'N/A', style: const TextStyle(fontSize: 18))), Icon(Icons.access_time)],
),
),
),
),
);
}
}

Late Initialization Error in Flutter because of _startDate

As you can see in the included screenshot, I am getting a LateInitializationError upon running my app. The cause is in the code below, but I can't figure out how to fix it. It certainly has to do with the "late DateTime _startDate;" that I am using, but unsure what the right approach is. Do you have any idea? Thanks in advance for looking into it!
class AddEventPage extends StatefulWidget {
final DateTime? selectedDate;
final AppEvent? event;
const AddEventPage({Key? key, this.selectedDate, this.event})
: super(key: key);
#override
_AddEventPageState createState() => _AddEventPageState();
}
late DateTime _startDate;
late TimeOfDay _startTime;
late DateTime _endDate;
late TimeOfDay _endTime;
class _AddEventPageState extends State<AddEventPage> {
final _formKey = GlobalKey<FormBuilderState>();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
leading: IconButton(
icon: Icon(
Icons.clear,
color: AppColors.primaryColor,
),
onPressed: () {
Navigator.pop(context);
},
),
actions: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () async {
//save
_formKey.currentState!.save();
final data =
Map<String, dynamic>.from(_formKey.currentState!.value);
data["Time Start"] =
(data["Time Start"] as DateTime).millisecondsSinceEpoch;
if (widget.event != null) {
//update
await eventDBS.updateData(widget.event!.id!, data);
} else {
//create
await eventDBS.create({
...data,
"user_id": context.read(userRepoProvider).user!.id,
});
}
Navigator.pop(context);
},
child: Text("Save"),
),
)
],
),
body: ListView(
padding: const EdgeInsets.all(16.0),
children: <Widget>[
//add event form
FormBuilder(
key: _formKey,
child: Column(
children: [
FormBuilderTextField(
name: "title",
initialValue: widget.event?.title,
decoration: InputDecoration(
hintText: "Add Title",
border: InputBorder.none,
contentPadding: const EdgeInsets.only(left: 48.0)),
),
Divider(),
FormBuilderTextField(
name: "description",
initialValue: widget.event?.description,
minLines: 1,
maxLines: 5,
decoration: InputDecoration(
hintText: "Add Details",
border: InputBorder.none,
prefixIcon: Icon(Icons.short_text)),
),
Divider(),
FormBuilderSwitch(
name: "public",
initialValue: widget.event?.public ?? false,
title: Text("Public"),
controlAffinity: ListTileControlAffinity.leading,
decoration: InputDecoration(
border: InputBorder.none,
),
),
Divider(),
Neumorphic(
style: NeumorphicStyle(color: Colors.white),
child: Column(
children: [
GestureDetector(
child: Text(
DateFormat('EEE, MMM dd, yyyy')
.format(_startDate),
textAlign: TextAlign.left),
onTap: () async {
final DateTime? date = await showDatePicker(
context: context,
initialDate: _startDate,
firstDate: DateTime(2000),
lastDate: DateTime(2100),
);
if (date != null && date != _startDate) {
setState(() {
final Duration difference =
_endDate.difference(_startDate);
_startDate = DateTime(
date.year,
date.month,
date.day,
_startTime.hour,
_startTime.minute,
0);
_endDate = _startDate.add(difference);
_endTime = TimeOfDay(
hour: _endDate.hour,
minute: _endDate.minute);
});
}
}),
Container(
child: FormBuilderDateTimePicker(
name: "Time End",
initialValue: widget.selectedDate ??
widget.event?.date ??
DateTime.now(),
initialDate: DateTime.now(),
fieldHintText: "Add Date",
initialDatePickerMode: DatePickerMode.day,
inputType: InputType.both,
format: DateFormat('EEE, dd MMM, yyyy HH:mm'),
decoration: InputDecoration(
border: InputBorder.none,
prefix: Text(' '),
),
),
),
],
),
),
],
),
),
],
),
);
}
}
late to the keyword means that your property will be initialized when you use it for the first time.
You like to initialize like this:
late DateTime _startDate = DateTime.now();
And as well as change the others value respectively
In GestureDetector you are using a Text widget and passing the _startDate as value but you have not assigned any value to it beforehand, this causes this error, try giving it an initial value before using it.
You can use the following code as well :
DateTime? _startDate;
I have exactly problem.
Some objects should not be initialized directly, hence the creation of late.
For example I don't want to initialize a File object at creation, but afterwards I use late but flutter returns an error.strong text.
So run: flutter run --release

How to use timepicker widget to show start and end time using Flutter

I am creating a page to show start time and end time but when I couldn't find a way to update the time in individual fields. I am only using a single function to build a timepicker and showing the time.
Here is my timepicker function
Future<Null> selectedTime(BuildContext context, bool ifPickedTime) async{
_pickedTime = await showTimePicker(
context: context,
initialTime: _currentTime);
if (_pickedTime != null){
setState(() {
_currentTime = _pickedTime;
print("The picked time is: $_pickedTime");
});
}
}
This is my Widget Builder
Widget _buildTimePick(String title, bool ifPickedTime){
return Row(
children: [
Container(
width: 80,
child: Text(
title,
style: AppStyles.textTitle,
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
decoration: BoxDecoration(
border: Border.all(color: MyColors.borderColor),
borderRadius: BorderRadius.circular(20),
),
child: GestureDetector(
child: Text(
"${_currentTime.format(context)}",
style: AppStyles.textBody,
),
onTap: () {
selectedTime(context, ifPickedTime);
},
),
),
],
);
}
According to the question, I feel that what you want to achieve is that you want to update the start and the end time individually. But according to the code only one time is updated which is shared by both start the end time widget, so it shows the same time.
What you can do is create 2 variables one for start time and one for end time.
TimeOfDay _startTime;
TimeOfDay _endTime;
Then make your _buildTimePick function a little more flexible by passing the time to display.
Widget _buildTimePick(String title, bool ifPickedTime, TimeOfDay currentTime, Function(TimeOfDay) onTimePicked){
return Row(
children: [
Container(
width: 80,
child: Text(
title,
style: AppStyles.textTitle,
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
decoration: BoxDecoration(
border: Border.all(color: MyColors.borderColor),
borderRadius: BorderRadius.circular(20),
),
child: GestureDetector(
child: Text(
currentTime.format(context),
style: AppStyles.textBody,
),
onTap: () {
selectedTime(context, ifPickedTime,currentTime, onTimePicked);
},
),
),
],
);
}
Also the selectedTime function should be more flexible such that it can be used
for selecting both start and end time by creating a callback.
Future selectedTime(BuildContext context, bool ifPickedTime, TimeOfDay initialTime, Function(TimeOfDay) onTimePicked) async{
var _pickedTime = await showTimePicker(
context: context,
initialTime: initialTime);
if (_pickedTime != null){
onTimePicked(_pickedTime);
}
}
So, the final code might look something like :
import 'package:flutter/material.dart';
void main() => runApp(MainApp());
class MainApp extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MainApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Home(),
));
}
}
class Home extends StatefulWidget {
#override
State<Home> createState() => HomeState();
}
class HomeState extends State<Home> {
TimeOfDay startTime = TimeOfDay.now();
TimeOfDay endTime = TimeOfDay.now();
#override
Widget build(BuildContext context) {
return ListView(
children: [
_buildTimePick("Start", true, startTime, (x) {
setState(() {
startTime = x;
print("The picked time is: $x");
});
}),
const SizedBox(height: 10),
_buildTimePick("End", true, endTime, (x) {
setState(() {
endTime = x;
print("The picked time is: $x");
});
}),
],
);
}
Future selectedTime(BuildContext context, bool ifPickedTime,
TimeOfDay initialTime, Function(TimeOfDay) onTimePicked) async {
var _pickedTime =
await showTimePicker(context: context, initialTime: initialTime);
if (_pickedTime != null) {
onTimePicked(_pickedTime);
}
}
Widget _buildTimePick(String title, bool ifPickedTime, TimeOfDay currentTime,
Function(TimeOfDay) onTimePicked) {
return Row(
children: [
SizedBox(
width: 80,
child: Text(
title,
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(20),
),
child: GestureDetector(
child: Text(
currentTime.format(context),
),
onTap: () {
selectedTime(context, ifPickedTime, currentTime, onTimePicked);
},
),
),
],
);
}
}
You can update the TextField using TextEditingController. Put in in the setState so it can update the TextField whenever you pick a date.
Exp:
final txtController = TextEditingController();
final DateTime? picked = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2025));
if(picked != null && picked != now) {
setState(() {
txtController.text = DateFormat('dd-MM-yyyy').format(picked); //I'm using intl package, you can use toString()
});
}