我有一个文本按钮,当用户单击它时,它应该弹出一个窗口,但没有任何反应。我已经对文本按钮所在的文件添加了很多更改,但我没有对文本按钮本身进行任何更改。
这是文本按钮代码:
TextButton(
child: Text(
'3% Commission: ${_formatCurrency(strCommission)}',
style: TextStyle(fontSize: constants.kTextFieldTextFontSize),
),
onPressed: () {
CommissionCalculatorPopup
.showCommissionCalculator(
context,
double.parse(
contractPriceController.text));
},
),
这是弹出窗口代码:
class CommissionCalculatorPopup {
static void showCommissionCalculator(BuildContext context, double salePrice) {
showDialog(
context: context,
builder: (BuildContext context) {
return CommissionCalculatorDialog(salePrice: salePrice);
},
);
}
}
class CommissionCalculatorDialog extends StatefulWidget {
final double salePrice;
const CommissionCalculatorDialog({required this.salePrice, super.key});
@override
State<CommissionCalculatorDialog> createState() =>
_CommissionCalculatorDialogState();
}
class _CommissionCalculatorDialogState
extends State<CommissionCalculatorDialog> {
TextEditingController salePriceController = TextEditingController();
TextEditingController commissionRateController = TextEditingController();
double commission = 0.0;
@override
void initState() {
super.initState();
salePriceController.text = widget.salePrice.toString();
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Commission Calculator'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: salePriceController,
decoration: const InputDecoration(labelText: 'Sale Price (\$)'),
keyboardType: TextInputType.number,
),
TextField(
controller: commissionRateController,
decoration: const InputDecoration(labelText: 'Commission Rate (%)'),
keyboardType: TextInputType.number,
),
SizedBox(height: 20.sp),
ElevatedButton(
onPressed: calculateCommission,
child: const Text('Calculate Commission'),
),
SizedBox(height: 20.sp),
Text(
'Commission: \$${commission.toStringAsFixed(2)}',
style: TextStyle(fontSize: 18.sp),
),
],
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Close'),
),
],
);
}
void calculateCommission() {
if (salePriceController.text.isNotEmpty &&
commissionRateController.text.isNotEmpty) {
double salePrice = double.parse(salePriceController.text);
double commissionRate = double.parse(commissionRateController.text);
double commissionAmount = (salePrice * commissionRate) / 100;
setState(() {
commission = commissionAmount;
});
} else {
// Handle empty fields
// You can show an error message or take appropriate action
}
}
}
我不确定为什么这不起作用,因为它不久前就起作用了,而且我没有在弹出窗口上更改任何内容。
感谢您的帮助。
我认为它来自输入,确保输入参数是数字。
抱歉,我没有评论权。