showDurationPicker static method

Future<Duration?> showDurationPicker({
  1. Duration? initialDuration,
  2. Duration? maxDuration,
  3. bool showHours = true,
  4. bool showMinutes = true,
  5. bool showSeconds = true,
  6. Color? primaryColor,
  7. Color? backgroundColor,
  8. Color? textColor,
})

Implementation

static Future<Duration?> showDurationPicker({
  Duration? initialDuration,
  Duration? maxDuration,
  bool showHours = true,
  bool showMinutes = true,
  bool showSeconds = true,
  Color? primaryColor,
  Color? backgroundColor,
  Color? textColor,
}) async {
  initialDuration ??= const Duration(minutes: 30);
  maxDuration ??= const Duration(hours: 24);
  primaryColor ??= AppColors.primary;
  backgroundColor ??= Colors.white;
  textColor ??= AppColors.text;

  int hours = initialDuration.inHours;
  int minutes = (initialDuration.inMinutes % 60);
  int seconds = (initialDuration.inSeconds % 60);

  Duration? result = await Get.dialog<Duration>(
    Dialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      backgroundColor: backgroundColor,
      child: Padding(
        padding: const EdgeInsets.all(24.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              'Select Duration',
              style: AppTextThemes.heading5(color: textColor),
            ),
            const SizedBox(height: 24),
            StatefulBuilder(
              builder: (context, setState) {
                return Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    if (showHours) ...[
                      _buildDurationPicker(
                        context: context,
                        value: hours,
                        label: 'Hours',
                        maxValue: maxDuration?.inHours ?? 0,
                        onChanged: (value) {
                          setState(() => hours = value);
                        },
                        primaryColor: primaryColor ?? AppColors.primary,
                        textColor: textColor ?? AppColors.text,
                      ),
                      const SizedBox(width: 8),
                    ],
                    if (showMinutes) ...[
                      _buildDurationPicker(
                        context: context,
                        value: minutes,
                        label: 'Min',
                        maxValue: 59,
                        onChanged: (value) {
                          setState(() => minutes = value);
                        },
                        primaryColor: primaryColor ?? AppColors.primary,
                        textColor: textColor ?? AppColors.text,
                      ),
                      const SizedBox(width: 8),
                    ],
                    if (showSeconds) ...[
                      _buildDurationPicker(
                        context: context,
                        value: seconds,
                        label: 'Sec',
                        maxValue: 59,
                        onChanged: (value) {
                          setState(() => seconds = value);
                        },
                        primaryColor: primaryColor ?? AppColors.primary,
                        textColor: textColor ?? AppColors.text,
                      ),
                    ],
                  ],
                );
              },
            ),
            const SizedBox(height: 24),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                TextButton(
                  onPressed: () => Get.back(),
                  style: TextButton.styleFrom(
                    foregroundColor: Colors.grey[700],
                  ),
                  child: Text(
                    'Cancel',
                    style: AppTextThemes.button(color: Colors.grey[700]),
                  ),
                ),
                ElevatedButton(
                  onPressed: () {
                    final duration = Duration(
                      hours: hours,
                      minutes: minutes,
                      seconds: seconds,
                    );
                    Get.back(result: duration);
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: primaryColor,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  child: Text(
                    'Confirm',
                    style: AppTextThemes.button(color: Colors.white),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    ),
  );

  return result;
}