mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
153 lines
5.0 KiB
Dart
153 lines
5.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:syncrow_web/pages/routines/bloc/effective_period/effect_period_bloc.dart';
|
|
import 'package:syncrow_web/pages/routines/bloc/effective_period/effect_period_event.dart';
|
|
import 'package:syncrow_web/utils/color_manager.dart';
|
|
import 'package:syncrow_web/utils/constants/app_enum.dart';
|
|
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
|
import 'package:time_picker_spinner/time_picker_spinner.dart';
|
|
|
|
class EffectPeriodHelper {
|
|
static Future<List<String>?> showCustomTimePicker(
|
|
BuildContext context) async {
|
|
var selectedStartTime = '00:00';
|
|
var selectedEndTime = '23:59';
|
|
final pageController = PageController(initialPage: 0);
|
|
|
|
final startDateTime = DateTime(2022, 1, 1, 0, 0);
|
|
final endDateTime = DateTime(2022, 1, 1, 23, 59);
|
|
|
|
context.customAlertDialog(
|
|
alertBody: SizedBox(
|
|
height: 250,
|
|
child: PageView(
|
|
controller: pageController,
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
children: [
|
|
_buildTimePickerPage(
|
|
context: context,
|
|
pageController: pageController,
|
|
isStartTime: true,
|
|
time: startDateTime,
|
|
onTimeChange: (time) {
|
|
selectedStartTime =
|
|
"${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}";
|
|
},
|
|
),
|
|
_buildTimePickerPage(
|
|
context: context,
|
|
pageController: pageController,
|
|
isStartTime: false,
|
|
time: endDateTime,
|
|
onTimeChange: (time) {
|
|
selectedEndTime =
|
|
"${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}";
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
title: 'Custom',
|
|
onConfirm: () {
|
|
context.read<EffectPeriodBloc>().add(
|
|
SetCustomTime(selectedStartTime, selectedEndTime),
|
|
);
|
|
context.read<EffectPeriodBloc>().add(
|
|
const SetPeriod(EnumEffectivePeriodOptions.custom),
|
|
);
|
|
|
|
Navigator.of(context).pop();
|
|
},
|
|
);
|
|
return null;
|
|
}
|
|
|
|
static Widget _buildTimePickerPage({
|
|
required BuildContext context,
|
|
required PageController pageController,
|
|
required bool isStartTime,
|
|
required DateTime time,
|
|
required Function(DateTime) onTimeChange,
|
|
}) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
if (!isStartTime)
|
|
TextButton(
|
|
onPressed: () {
|
|
pageController.previousPage(
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.easeIn,
|
|
);
|
|
},
|
|
child: const Text('Start'),
|
|
),
|
|
TextButton(
|
|
onPressed: () {},
|
|
child: Text(isStartTime ? 'Start' : 'End',
|
|
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
|
|
color: ColorsManager.textPrimaryColor,
|
|
fontWeight: FontWeight.w400,
|
|
fontSize: 10)),
|
|
),
|
|
if (isStartTime)
|
|
TextButton(
|
|
onPressed: () {
|
|
pageController.nextPage(
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.easeIn,
|
|
);
|
|
},
|
|
child: Text('End',
|
|
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
|
|
color: ColorsManager.textPrimaryColor,
|
|
fontWeight: FontWeight.w400,
|
|
fontSize: 10)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
TimePickerSpinner(
|
|
is24HourMode: false,
|
|
normalTextStyle: const TextStyle(
|
|
fontSize: 24,
|
|
color: Colors.grey,
|
|
),
|
|
highlightedTextStyle: const TextStyle(
|
|
fontSize: 24,
|
|
color: ColorsManager.primaryColor,
|
|
),
|
|
spacing: 20,
|
|
itemHeight: 50,
|
|
isForce2Digits: true,
|
|
time: time,
|
|
onTimeChange: onTimeChange,
|
|
),
|
|
const SizedBox(height: 16),
|
|
],
|
|
);
|
|
}
|
|
|
|
static String formatEnumValue(EnumEffectivePeriodOptions value) {
|
|
switch (value) {
|
|
case EnumEffectivePeriodOptions.allDay:
|
|
return 'All Day';
|
|
case EnumEffectivePeriodOptions.daytime:
|
|
return 'Daytime';
|
|
case EnumEffectivePeriodOptions.night:
|
|
return 'Night';
|
|
case EnumEffectivePeriodOptions.custom:
|
|
return 'Custom';
|
|
case EnumEffectivePeriodOptions.none:
|
|
return 'None';
|
|
default:
|
|
return '';
|
|
}
|
|
}
|
|
}
|