You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/widgets/date_and_time/time_picker.dart

72 lines
2.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:test_sa/views/app_style/sizing.dart';
class ADateTimePicker extends StatelessWidget {
final DateTime date;
final DateTime from;
final DateTime to;
final Function(DateTime) onDateTimePicker;
final bool enable;
const ADateTimePicker({Key key, this.date, this.onDateTimePicker, this.from, this.to, this.enable}) : super(key: key);
@override
Widget build(BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
textStyle: Theme.of(context).textTheme.subtitle2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12 * AppStyle.getScaleFactor(context)),
),
),
child: Text(
date == null ? "Pick Time" : date.toString().substring(0, date.toString().lastIndexOf(":")),
textScaleFactor: AppStyle.getScaleFactor(context),
),
onPressed: enable? () async {
// TimeOfDay picked = await showTimePicker(context: context, initialTime: TimeOfDay.now());
onDateTimePicker(await showDateTimePicker(context: context, initialDate: date, firstDate: from, lastDate: to));
}: null,
);
}
}
Future<DateTime> showDateTimePicker({
BuildContext context,
DateTime initialDate,
DateTime firstDate,
DateTime lastDate,
}) async {
initialDate ??= DateTime.now();
firstDate ??= initialDate.subtract(const Duration(days: 365 * 100));
lastDate ??= firstDate.add(const Duration(days: 365 * 200));
final DateTime selectedDate = await showDatePicker(
context: context,
initialDate: initialDate,
firstDate: firstDate,
lastDate: lastDate,
);
if (selectedDate == null) return null;
if (!context.mounted) return selectedDate;
final TimeOfDay selectedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(selectedDate),
);
return selectedTime == null
? selectedDate
: DateTime(
selectedDate.year,
selectedDate.month,
selectedDate.day,
selectedTime.hour,
selectedTime.minute,
);
}