mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
331 lines
9.7 KiB
Dart
331 lines
9.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:syncrow_web/pages/routiens/models/ac/ac_function.dart';
|
|
import 'package:syncrow_web/pages/routiens/models/device_functions.dart';
|
|
import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart';
|
|
import 'package:syncrow_web/pages/routiens/widgets/dialog_header.dart';
|
|
import 'package:syncrow_web/utils/color_manager.dart';
|
|
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
|
|
|
class ACHelper {
|
|
static Future<Map<String, dynamic>?> showACFunctionsDialog(
|
|
BuildContext context,
|
|
List<DeviceFunction<dynamic>> functions,
|
|
) async {
|
|
List<ACFunction> acFunctions = functions.whereType<ACFunction>().toList();
|
|
String? selectedFunction;
|
|
dynamic selectedValue = 20;
|
|
String? selectedCondition = "==";
|
|
List<bool> _selectedConditions = [false, true, false];
|
|
|
|
return showDialog<Map<String, dynamic>?>(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return StatefulBuilder(
|
|
builder: (context, setState) {
|
|
return AlertDialog(
|
|
contentPadding: EdgeInsets.zero,
|
|
content: _buildDialogContent(
|
|
context,
|
|
setState,
|
|
acFunctions,
|
|
selectedFunction,
|
|
selectedValue,
|
|
selectedCondition,
|
|
_selectedConditions,
|
|
(fn) => selectedFunction = fn,
|
|
(val) => selectedValue = val,
|
|
(cond) => selectedCondition = cond,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
/// Build dialog content for AC functions dialog
|
|
static Widget _buildDialogContent(
|
|
BuildContext context,
|
|
StateSetter setState,
|
|
List<ACFunction> acFunctions,
|
|
String? selectedFunction,
|
|
dynamic selectedValue,
|
|
String? selectedCondition,
|
|
List<bool> selectedConditions,
|
|
Function(String?) onFunctionSelected,
|
|
Function(dynamic) onValueSelected,
|
|
Function(String?) onConditionSelected,
|
|
) {
|
|
return Container(
|
|
width: selectedFunction != null ? 600 : 360,
|
|
height: 450,
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(20),
|
|
),
|
|
padding: const EdgeInsets.only(top: 20),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const DialogHeader('AC Functions'),
|
|
Flexible(
|
|
child: Row(
|
|
children: [
|
|
_buildFunctionsList(
|
|
context,
|
|
setState,
|
|
acFunctions,
|
|
selectedFunction,
|
|
onFunctionSelected,
|
|
),
|
|
if (selectedFunction != null)
|
|
_buildValueSelector(
|
|
context,
|
|
setState,
|
|
selectedFunction,
|
|
selectedValue,
|
|
selectedCondition,
|
|
selectedConditions,
|
|
onValueSelected,
|
|
onConditionSelected,
|
|
acFunctions,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
DialogFooter(
|
|
onCancel: () => Navigator.pop(context),
|
|
onConfirm: selectedFunction != null && selectedValue != null
|
|
? () => Navigator.pop(context, {
|
|
'function': selectedFunction,
|
|
'value': selectedValue,
|
|
'condition': selectedCondition ?? "==",
|
|
})
|
|
: null,
|
|
isConfirmEnabled: selectedFunction != null && selectedValue != null,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Build functions list for AC functions dialog
|
|
static Widget _buildFunctionsList(
|
|
BuildContext context,
|
|
StateSetter setState,
|
|
List<ACFunction> acFunctions,
|
|
String? selectedFunction,
|
|
Function(String?) onFunctionSelected,
|
|
) {
|
|
return Expanded(
|
|
child: ListView.separated(
|
|
shrinkWrap: false,
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
itemCount: acFunctions.length,
|
|
separatorBuilder: (context, index) => const Divider(
|
|
color: ColorsManager.dividerColor,
|
|
),
|
|
itemBuilder: (context, index) {
|
|
final function = acFunctions[index];
|
|
return ListTile(
|
|
leading: SvgPicture.asset(
|
|
function.icon,
|
|
width: 24,
|
|
height: 24,
|
|
),
|
|
title: Text(
|
|
function.operationName,
|
|
style: context.textTheme.bodyMedium,
|
|
),
|
|
trailing: const Icon(
|
|
Icons.arrow_forward_ios,
|
|
size: 16,
|
|
color: ColorsManager.textGray,
|
|
),
|
|
onTap: () => setState(() => onFunctionSelected(function.code)),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Build value selector for AC functions dialog
|
|
static Widget _buildValueSelector(
|
|
BuildContext context,
|
|
StateSetter setState,
|
|
String selectedFunction,
|
|
dynamic selectedValue,
|
|
String? selectedCondition,
|
|
List<bool> selectedConditions,
|
|
Function(dynamic) onValueSelected,
|
|
Function(String?) onConditionSelected,
|
|
List<ACFunction> acFunctions,
|
|
) {
|
|
if (selectedFunction == 'temp_set' || selectedFunction == 'temp_current') {
|
|
return Expanded(
|
|
child: _buildTemperatureSelector(
|
|
context,
|
|
setState,
|
|
selectedValue,
|
|
selectedCondition,
|
|
selectedConditions,
|
|
onValueSelected,
|
|
onConditionSelected,
|
|
),
|
|
);
|
|
}
|
|
|
|
final selectedFn =
|
|
acFunctions.firstWhere((f) => f.code == selectedFunction);
|
|
final values = selectedFn.getOperationalValues();
|
|
return Expanded(
|
|
child: _buildOperationalValuesList(
|
|
context,
|
|
setState,
|
|
values,
|
|
selectedValue,
|
|
onValueSelected,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Build temperature selector for AC functions dialog
|
|
static Widget _buildTemperatureSelector(
|
|
BuildContext context,
|
|
StateSetter setState,
|
|
dynamic selectedValue,
|
|
String? selectedCondition,
|
|
List<bool> selectedConditions,
|
|
Function(dynamic) onValueSelected,
|
|
Function(String?) onConditionSelected,
|
|
) {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
_buildConditionToggle(
|
|
context,
|
|
setState,
|
|
selectedConditions,
|
|
onConditionSelected,
|
|
),
|
|
const SizedBox(height: 20),
|
|
_buildTemperatureDisplay(context, selectedValue),
|
|
const SizedBox(height: 20),
|
|
_buildTemperatureSlider(
|
|
context,
|
|
setState,
|
|
selectedValue,
|
|
onValueSelected,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// Build condition toggle for AC functions dialog
|
|
static Widget _buildConditionToggle(
|
|
BuildContext context,
|
|
StateSetter setState,
|
|
List<bool> selectedConditions,
|
|
Function(String?) onConditionSelected,
|
|
) {
|
|
return ToggleButtons(
|
|
onPressed: (int index) {
|
|
setState(() {
|
|
for (int i = 0; i < selectedConditions.length; i++) {
|
|
selectedConditions[i] = i == index;
|
|
}
|
|
onConditionSelected(index == 0
|
|
? "<"
|
|
: index == 1
|
|
? "=="
|
|
: ">");
|
|
});
|
|
},
|
|
borderRadius: const BorderRadius.all(Radius.circular(8)),
|
|
selectedBorderColor: ColorsManager.primaryColorWithOpacity,
|
|
selectedColor: Colors.white,
|
|
fillColor: ColorsManager.primaryColorWithOpacity,
|
|
color: ColorsManager.primaryColorWithOpacity,
|
|
constraints: const BoxConstraints(
|
|
minHeight: 40.0,
|
|
minWidth: 40.0,
|
|
),
|
|
isSelected: selectedConditions,
|
|
children: const [Text("<"), Text("="), Text(">")],
|
|
);
|
|
}
|
|
|
|
/// Build temperature display for AC functions dialog
|
|
static Widget _buildTemperatureDisplay(
|
|
BuildContext context, dynamic selectedValue) {
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
|
|
decoration: BoxDecoration(
|
|
color: ColorsManager.primaryColorWithOpacity.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: Text(
|
|
'${selectedValue ?? 20}°C',
|
|
style: context.textTheme.headlineMedium!.copyWith(
|
|
color: ColorsManager.primaryColorWithOpacity,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
static Widget _buildTemperatureSlider(
|
|
BuildContext context,
|
|
StateSetter setState,
|
|
dynamic selectedValue,
|
|
Function(dynamic) onValueSelected,
|
|
) {
|
|
final currentValue = selectedValue is int ? selectedValue.toDouble() : 20.0;
|
|
return Slider(
|
|
value: currentValue,
|
|
min: 16,
|
|
max: 30,
|
|
divisions: 14,
|
|
label: '${currentValue.toInt()}°C',
|
|
onChanged: (value) {
|
|
setState(() => onValueSelected(value.toInt()));
|
|
},
|
|
);
|
|
}
|
|
|
|
static Widget _buildOperationalValuesList(
|
|
BuildContext context,
|
|
StateSetter setState,
|
|
List<dynamic> values,
|
|
dynamic selectedValue,
|
|
Function(dynamic) onValueSelected,
|
|
) {
|
|
return ListView.builder(
|
|
shrinkWrap: false,
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
itemCount: values.length,
|
|
itemBuilder: (context, index) {
|
|
final value = values[index];
|
|
return ListTile(
|
|
leading: SvgPicture.asset(
|
|
value.icon,
|
|
width: 24,
|
|
height: 24,
|
|
),
|
|
title: Text(
|
|
value.description,
|
|
style: context.textTheme.bodyMedium,
|
|
),
|
|
trailing: Radio<dynamic>(
|
|
value: value.value,
|
|
groupValue: selectedValue,
|
|
onChanged: (newValue) {
|
|
setState(() => onValueSelected(newValue));
|
|
},
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|