xxxxxxxxxx
dependencies:
persian_datetime_picker: 2.4.0
xxxxxxxxxx
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
class MyDateTimePicker extends StatefulWidget {
@override
_MyDateTimePickerState createState() => _MyDateTimePickerState();
}
class _MyDateTimePickerState extends State<MyDateTimePicker> {
String selectedDateAndTime = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Date and Time Picker'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Selected Date and Time:',
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
Text(
selectedDateAndTime,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
RaisedButton(
child: Text('Pick Date and Time'),
onPressed: () {
DatePicker.showDateTimePicker(
context,
showTitleActions: true,
onChanged: (dateTime) {
print('Selected Date and Time: $dateTime');
},
onConfirm: (dateTime) {
setState(() {
selectedDateAndTime = dateTime.toString();
});
},
currentTime: DateTime.now(),
);
},
),
],
),
),
);
}
}
xxxxxxxxxx
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class DateTimePickerWidget extends StatefulWidget {
@override
_DateTimePickerWidgetState createState() => _DateTimePickerWidgetState();
}
class _DateTimePickerWidgetState extends State<DateTimePickerWidget> {
DateTime selectedDate;
TimeOfDay selectedTime;
Future<void> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101),
);
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
});
}
Future<void> _selectTime(BuildContext context) async {
final TimeOfDay picked = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
);
if (picked != null && picked != selectedTime)
setState(() {
selectedTime = picked;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
ListTile(
title: const Text('Select Date'),
subtitle: selectedDate != null
? Text('${selectedDate.year}-${selectedDate.month}-${selectedDate.day}')
: Text('No date selected'),
onTap: () => _selectDate(context),
),
ListTile(
title: const Text('Select Time'),
subtitle: selectedTime != null
? Text('${selectedTime.hour}:${selectedTime.minute}')
: Text('No time selected'),
onTap: () => _selectTime(context),
),
],
);
}
}
// Usage
DateTimePickerWidget(),