mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-11 07:38:05 +00:00
@ -1,5 +1,4 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:syncrow_web/pages/routines/widgets/condition_toggle.dart';
|
||||
@ -46,10 +45,10 @@ class _CustomRoutinesTextboxState extends State<CustomRoutinesTextbox> {
|
||||
String? errorMessage;
|
||||
|
||||
int getDecimalPlaces(double step) {
|
||||
final stepStr = step.toString();
|
||||
String stepStr = step.toString();
|
||||
if (stepStr.contains('.')) {
|
||||
final parts = stepStr.split('.');
|
||||
var decimalPart = parts[1];
|
||||
List<String> parts = stepStr.split('.');
|
||||
String decimalPart = parts[1];
|
||||
decimalPart = decimalPart.replaceAll(RegExp(r'0+$'), '');
|
||||
return decimalPart.isEmpty ? 0 : decimalPart.length;
|
||||
} else {
|
||||
@ -112,11 +111,13 @@ class _CustomRoutinesTextboxState extends State<CustomRoutinesTextbox> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _validateInput(String value) {
|
||||
final doubleValue = double.tryParse(value);
|
||||
if (doubleValue == null) {
|
||||
setState(() {
|
||||
errorMessage = 'Invalid number';
|
||||
errorMessage = "Invalid number";
|
||||
hasError = true;
|
||||
});
|
||||
return;
|
||||
@ -127,23 +128,23 @@ class _CustomRoutinesTextboxState extends State<CustomRoutinesTextbox> {
|
||||
|
||||
if (doubleValue < min) {
|
||||
setState(() {
|
||||
errorMessage = 'Value must be at least $min';
|
||||
errorMessage = "Value must be at least $min";
|
||||
hasError = true;
|
||||
});
|
||||
} else if (doubleValue > max) {
|
||||
setState(() {
|
||||
errorMessage = 'Value must be at most $max';
|
||||
errorMessage = "Value must be at most $max";
|
||||
hasError = true;
|
||||
});
|
||||
} else {
|
||||
final decimalPlaces = getDecimalPlaces(widget.stepIncreaseAmount);
|
||||
final factor = pow(10, decimalPlaces).toInt();
|
||||
final scaledStep = (widget.stepIncreaseAmount * factor).round();
|
||||
final scaledValue = (doubleValue * factor).round();
|
||||
int decimalPlaces = getDecimalPlaces(widget.stepIncreaseAmount);
|
||||
int factor = pow(10, decimalPlaces).toInt();
|
||||
int scaledStep = (widget.stepIncreaseAmount * factor).round();
|
||||
int scaledValue = (doubleValue * factor).round();
|
||||
|
||||
if (scaledValue % scaledStep != 0) {
|
||||
setState(() {
|
||||
errorMessage = 'must be a multiple of ${widget.stepIncreaseAmount}';
|
||||
errorMessage = "must be a multiple of ${widget.stepIncreaseAmount}";
|
||||
hasError = true;
|
||||
});
|
||||
} else {
|
||||
@ -155,10 +156,11 @@ class _CustomRoutinesTextboxState extends State<CustomRoutinesTextbox> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void _correctAndUpdateValue(String value) {
|
||||
final doubleValue = double.tryParse(value) ?? 0.0;
|
||||
final decimalPlaces = getDecimalPlaces(widget.stepIncreaseAmount);
|
||||
var rounded = (doubleValue / widget.stepIncreaseAmount).round() *
|
||||
int decimalPlaces = getDecimalPlaces(widget.stepIncreaseAmount);
|
||||
double rounded = (doubleValue / widget.stepIncreaseAmount).round() *
|
||||
widget.stepIncreaseAmount;
|
||||
rounded = rounded.clamp(widget.sliderRange.$1, widget.sliderRange.$2);
|
||||
rounded = double.parse(rounded.toStringAsFixed(decimalPlaces));
|
||||
@ -177,9 +179,9 @@ class _CustomRoutinesTextboxState extends State<CustomRoutinesTextbox> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final decimalPlaces = getDecimalPlaces(widget.stepIncreaseAmount);
|
||||
int decimalPlaces = getDecimalPlaces(widget.stepIncreaseAmount);
|
||||
|
||||
final formatters = <TextInputFormatter>[];
|
||||
List<TextInputFormatter> formatters = [];
|
||||
if (decimalPlaces == 0) {
|
||||
formatters.add(FilteringTextInputFormatter.digitsOnly);
|
||||
} else {
|
||||
@ -231,7 +233,7 @@ class _CustomRoutinesTextboxState extends State<CustomRoutinesTextbox> {
|
||||
color: ColorsManager.lightGrayBorderColor, width: 1),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: ColorsManager.blackColor.withValues(alpha: 0.05),
|
||||
color: ColorsManager.blackColor.withOpacity(0.05),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
|
Reference in New Issue
Block a user