cpd_2022_zi/lib/widgets/timepicker.dart

59 lines
1.8 KiB
Dart
Raw Normal View History

2023-02-17 11:35:05 +01:00
import 'package:flutter/material.dart';
class TimePicker extends StatefulWidget {
final TimeOfDay _initialTime;
const TimePicker(this._initialTime, {super.key});
@override
State<StatefulWidget> createState() => _TimePickerState();
}
class _TimePickerState extends State<TimePicker> {
TimeOfDay time = const TimeOfDay(hour: 12, minute: 0);
@override
Widget build(BuildContext context) {
time = widget._initialTime;
final hours = time.hour.toString().padLeft(2, '0');
final minutes = time.minute.toString().padLeft(2, '0');
return Center(
child: Column(children: [
const Text(
'Das ist die Zeit',
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 16),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
'${time.hour}:${time.minute}',
style: const TextStyle(fontSize: 32),
),
const SizedBox(width: 16),
ElevatedButton(
onPressed: () async {
//TODO auslagern
TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: time,
builder: (context, child) {
return MediaQuery(
data: MediaQuery.of(context)
.copyWith(alwaysUse24HourFormat: true),
child: child!,
);
},
);
if (newTime == null) return;
setState(() {
time = newTime;
print('Zeit geändert $newTime, Zeit aktualisiert?: $time');
});
},
child: const Text('Zeit einstellen'))
])
]),
);
}
}