cpd_2022_zi/lib/widgets/timepicker.dart

57 lines
1.7 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;
final String descriptionText;
TimePicker(this._initialTime, {super.key, required this.descriptionText});
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(
child: Column(children: [
2023-02-20 23:23:37 +01:00
Text(
widget.descriptionText,
style: 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'))
])
]),
);
}
}