buildTimestampCell function
- required BuildContext context,
- required BPEditorState editorState,
- required BPObservation observation,
- required BPObservation currentEdit,
- required ValueChanged<
DateTime> onTimestampChanged,
Builds a DataCell that lets the user pick a date/time, calling onTimestampChanged
to update the timestamp in a parent setState, which triggers a rebuild.
Implementation
DataCell buildTimestampCell({
required BuildContext context,
required BPEditorState editorState,
required BPObservation observation,
required BPObservation currentEdit,
// The callback we just added.
required ValueChanged<DateTime> onTimestampChanged,
}) {
return DataCell(
InkWell(
onTap: () async {
final date = await showDatePicker(
context: context,
initialDate: currentEdit.timestamp,
firstDate: DateTime(2000),
lastDate: DateTime.now(),
);
if (!context.mounted) return;
if (date != null) {
final time = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(currentEdit.timestamp),
);
if (time != null && context.mounted) {
final newTimestamp = DateTime(
date.year,
date.month,
date.day,
time.hour,
time.minute,
);
// Check for duplicates.
final conflict = editorState.observations.any(
(r) => r.timestamp == newTimestamp && r != observation,
);
if (conflict) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'An observation with this date/time already exists',
),
),
);
return;
}
// Instead of updating editorState here, call the parent's callback.
onTimestampChanged(newTimestamp);
}
}
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(
DateFormat('yyyy-MM-dd HH:mm:ss').format(currentEdit.timestamp),
style: const TextStyle(
decoration: TextDecoration.underline,
color: Colors.blue,
),
),
),
),
);
}