cpd_2022_zi/lib/widgets/timepicker.dart

80 lines
2.6 KiB
Dart
Raw Normal View History

2023-02-17 11:35:05 +01:00
import 'package:flutter/material.dart';
2023-02-20 23:23:37 +01:00
// ignore: must_be_immutable
2023-02-17 11:35:05 +01:00
class TimePicker extends StatefulWidget {
2023-02-20 23:23:37 +01:00
TimeOfDay _initialTime;
2023-02-20 23:59:40 +01:00
TimePicker(this._initialTime, {Key? key});
2023-02-20 23:23:37 +01:00
TimeOfDay get getCurrentTime => _initialTime;
2023-02-17 11:35:05 +01:00
@override
2023-02-20 23:23:37 +01:00
State<StatefulWidget> createState() => TimePickerState();
2023-02-17 11:35:05 +01:00
}
2023-02-20 23:23:37 +01:00
class TimePickerState extends State<TimePicker> {
TimePickerState();
2023-02-17 11:35:05 +01:00
@override
Widget build(BuildContext context) {
return Center(
2023-02-20 23:59:40 +01:00
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
2023-02-20 23:23:37 +01:00
Text(
2023-02-20 23:59:40 +01:00
'${widget._initialTime.hour.toString().padLeft(2, '0')}:${widget._initialTime.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 22),
2023-02-17 11:35:05 +01:00
),
2023-02-20 23:59:40 +01:00
const SizedBox(width: 16),
ElevatedButton(
onPressed: () async {
//TODO auslagern
TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: widget._initialTime,
builder: (context, child) {
return MediaQuery(
data: MediaQuery.of(context)
.copyWith(alwaysUse24HourFormat: true),
child: child!,
);
},
);
if (newTime == null) return;
setState(() {
widget._initialTime = newTime;
});
},
child: const Text('Zeit einstellen'))
2023-02-20 23:23:37 +01:00
widget.descriptionText,
2023-02-20 23:33:51 +01:00
style: const TextStyle(fontSize: 12),
2023-02-17 11:35:05 +01:00
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
2023-02-20 23:23:37 +01:00
'${widget._initialTime.hour.toString().padLeft(2, '0')}:${widget._initialTime.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 22),
2023-02-17 11:35:05 +01:00
),
const SizedBox(width: 16),
ElevatedButton(
onPressed: () async {
//TODO auslagern
TimeOfDay? newTime = await showTimePicker(
context: context,
2023-02-20 23:23:37 +01:00
initialTime: widget._initialTime,
2023-02-17 11:35:05 +01:00
builder: (context, child) {
return MediaQuery(
data: MediaQuery.of(context)
.copyWith(alwaysUse24HourFormat: true),
child: child!,
);
},
);
if (newTime == null) return;
setState(() {
2023-02-20 23:23:37 +01:00
widget._initialTime = newTime;
2023-02-17 11:35:05 +01:00
});
},
child: const Text('Zeit einstellen'))
])
]),
);
}
}