I'd like to ask on if there are anyway on how to compare in a conditional statement the two variable. As you can see, couponSalePriceCtrlr
and couponOrigPriceCtrlr
. I'd like to validate that the user's input in sale price
SHOULD not be greater than the original price
, but it seems like the validator
accepts only the (value)
parameter and a String
data type.
Widget editCouponSalePriceWidget(couponSalePriceCtrlr, couponOrigPriceCtrlr) {
// converted the variable parameters into double data type
double convertedSalePrice = double.parse(couponSalePriceCtrlr);
double convertedOrigPrice = double.parse(couponOrigPriceCtrlr);
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 0.0),
child: TextFormField(
style: const TextStyle(fontFamily: 'Poppins', fontSize: 13),
controller: couponSalePriceCtrlr,
keyboardType: TextInputType.number,
decoration: InputDecoration(
suffixText: "*",
suffixStyle: TextStyle(color: Colors.red),
labelText: 'Sale Price',
labelStyle: const TextStyle(
fontSize: 15, fontFamily: 'Poppins', color: Color(0xFF152C4C)),
isDense: true,
prefixIcon: const Icon(Icons.person),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xFFCECECE)),
borderRadius: BorderRadius.circular(12),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Color(0xFFCECECE)),
),
hintText: 'Orig Price',
fillColor: const Color(0xFFFEFEFE),
filled: true,
),
// however the validator only accepts, a string data type.
validator: (convertedSalePrice,convertedOrigPrice) {
if (convertedSalePrice!.isEmpty ||
!RegExp(r'[0-9] [,.]{0,1}[0-9]*').hasMatch(convertedSalePrice)) {
return "Please enter a valid original price.";
} else {
return null;
}
},
),
);
}
CodePudding user response:
I assume you have 2 TextFormField
, one for original price, another for sale price. Of course it is String
type, that's the rule :) Therefore you need to convert it to integer/double type. If your keyboardType
is number, it is unnecessary to check user's input is string type, else do it.
TextFormField(
controller: couponOrigPriceCtrlr,
keyboardType: TextInputType.number,
)
TextFormField(
controller: convertedSalePrice,
keyboardType: TextInputType.number,
validator: (saleStr) {
double originalDouble = double.parse(couponOrigPriceCtrlr.text);
double saleDouble = double.parse(saleStr.text);
// check what ever you want here
// ...
}
)