Implemented Cooking Duration Button
This commit is contained in:
79
lib/widgets/cooking_duration_button.dart
Normal file
79
lib/widgets/cooking_duration_button.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'duration_picker.dart';
|
||||
import '../services/tools.dart' show durationToFormattedString;
|
||||
|
||||
class CookingDurationButton extends StatelessWidget {
|
||||
const CookingDurationButton({
|
||||
super.key,
|
||||
required this.selectedDuration,
|
||||
required this.onDurationChanged,
|
||||
this.label = '',
|
||||
});
|
||||
|
||||
final String label;
|
||||
final void Function(Duration duration) onDurationChanged;
|
||||
final Duration selectedDuration;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
String text = label;
|
||||
if (label.isEmpty || selectedDuration.inMinutes != 0) {
|
||||
text = durationToFormattedString(selectedDuration);
|
||||
}
|
||||
return FloatingActionButton.extended(
|
||||
onPressed: () => showDialog(
|
||||
context: context,
|
||||
builder: (context) => _getDurationDialog(
|
||||
context,
|
||||
onDurationChanged,
|
||||
selectedDuration,
|
||||
),
|
||||
),
|
||||
isExtended: selectedDuration.inMinutes != 0,
|
||||
label: Text(
|
||||
text,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
icon:
|
||||
selectedDuration.inMinutes != 0 ? Icon(Icons.edit) : Icon(Icons.add),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Dialog _getDurationDialog(
|
||||
BuildContext context,
|
||||
void Function(Duration duration) update,
|
||||
Duration initialDuration,
|
||||
) {
|
||||
final width = MediaQuery.of(context).size.width * 0.8;
|
||||
final height = MediaQuery.of(context).size.height * 0.6;
|
||||
Duration selectedTime = initialDuration;
|
||||
return Dialog(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
DurationPicker(
|
||||
initialDuration: initialDuration,
|
||||
onChangedCallback: (duration) => selectedTime = duration,
|
||||
height: width * 1.25 > height ? height : width * 1.25,
|
||||
width: width,
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.bottomRight,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(0, 20, 20, 20),
|
||||
child: FloatingActionButton.extended(
|
||||
label: Text('Save'),
|
||||
icon: Icon(Icons.save),
|
||||
onPressed: () {
|
||||
update(selectedTime);
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user