nc-photos/app/lib/widget/photo_date_time_edit_dialog.dart

192 lines
6.5 KiB
Dart
Raw Normal View History

2021-06-21 12:39:17 +02:00
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2021-06-21 12:39:17 +02:00
import 'package:nc_photos/num_extension.dart';
class PhotoDateTimeEditDialog extends StatefulWidget {
2021-09-15 08:58:06 +02:00
const PhotoDateTimeEditDialog({
2021-07-23 22:05:57 +02:00
Key? key,
required this.initialDateTime,
2021-06-21 12:39:17 +02:00
}) : super(key: key);
@override
createState() => _PhotoDateTimeEditDialogState();
final DateTime initialDateTime;
}
class _PhotoDateTimeEditDialogState extends State<PhotoDateTimeEditDialog> {
@override
build(BuildContext context) {
return AlertDialog(
title: Text(L10n.global().updateDateTimeDialogTitle),
2021-06-21 12:39:17 +02:00
content: Form(
key: _formKey,
2021-09-15 12:50:51 +02:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
L10n.global().dateSubtitle,
style: Theme.of(context).textTheme.subtitle2,
),
Row(
children: [
Flexible(
child: TextFormField(
decoration: InputDecoration(
hintText: L10n.global().dateYearInputHint,
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
keyboardType: TextInputType.number,
validator: (value) {
try {
int.parse(value!);
return null;
} catch (_) {
return L10n.global().dateTimeInputInvalid;
2021-09-15 12:50:51 +02:00
}
},
onSaved: (value) {
_formValue.year = int.parse(value!);
},
initialValue: "${widget.initialDateTime.year}",
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
flex: 1,
),
const SizedBox(width: 4),
Flexible(
child: TextFormField(
decoration: InputDecoration(
hintText: L10n.global().dateMonthInputHint,
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
keyboardType: TextInputType.number,
validator: (value) {
if (int.tryParse(value!)?.inRange(1, 12) == true) {
return null;
}
return L10n.global().dateTimeInputInvalid;
},
onSaved: (value) {
_formValue.month = int.parse(value!);
},
initialValue:
widget.initialDateTime.month.toString().padLeft(2, "0"),
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
flex: 1,
),
const SizedBox(width: 4),
Flexible(
child: TextFormField(
decoration: InputDecoration(
hintText: L10n.global().dateDayInputHint,
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
keyboardType: TextInputType.number,
validator: (value) {
if (int.tryParse(value!)?.inRange(1, 31) == true) {
return null;
}
return L10n.global().dateTimeInputInvalid;
},
onSaved: (value) {
_formValue.day = int.parse(value!);
},
initialValue:
widget.initialDateTime.day.toString().padLeft(2, "0"),
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
flex: 1,
),
],
),
const SizedBox(height: 16),
Text(
L10n.global().timeSubtitle,
style: Theme.of(context).textTheme.subtitle2,
),
Row(
children: [
Flexible(
child: TextFormField(
decoration: InputDecoration(
hintText: L10n.global().timeHourInputHint,
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
keyboardType: TextInputType.number,
validator: (value) {
if (int.tryParse(value!)?.inRange(0, 23) == true) {
return null;
}
return L10n.global().dateTimeInputInvalid;
},
onSaved: (value) {
_formValue.hour = int.parse(value!);
},
initialValue:
widget.initialDateTime.hour.toString().padLeft(2, "0"),
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
flex: 1,
),
const SizedBox(width: 4),
Flexible(
child: TextFormField(
decoration: InputDecoration(
hintText: L10n.global().timeMinuteInputHint,
),
keyboardType: TextInputType.number,
validator: (value) {
if (int.tryParse(value!)?.inRange(0, 59) == true) {
return null;
}
return L10n.global().dateTimeInputInvalid;
},
onSaved: (value) {
_formValue.minute = int.parse(value!);
},
initialValue: widget.initialDateTime.minute
.toString()
.padLeft(2, "0"),
2021-06-21 12:39:17 +02:00
),
2021-09-15 12:50:51 +02:00
flex: 1,
),
const SizedBox(width: 4),
const Flexible(
child: SizedBox(),
flex: 1,
),
],
),
],
2021-06-21 12:39:17 +02:00
),
),
actions: [
TextButton(
onPressed: () => _onSavePressed(context),
child: Text(MaterialLocalizations.of(context).saveButtonLabel),
),
],
);
}
void _onSavePressed(BuildContext context) {
2021-07-23 22:05:57 +02:00
if (_formKey.currentState?.validate() == true) {
_formKey.currentState!.save();
2021-06-21 12:39:17 +02:00
final d = DateTime(_formValue.year, _formValue.month, _formValue.day,
_formValue.hour, _formValue.minute);
_log.info("[_onSavePressed] Set date time: $d");
Navigator.of(context).pop(d);
}
}
final _formKey = GlobalKey<FormState>();
final _formValue = _FormValue();
static final _log = Logger(
"widget.photo_date_time_edit_dialog._PhotoDateTimeEditDialogState");
}
class _FormValue {
2021-07-23 22:05:57 +02:00
late int year;
late int month;
late int day;
late int hour;
late int minute;
2021-06-21 12:39:17 +02:00
}