【发布时间】:2021-05-17 08:23:19
【问题描述】:
我正在尝试添加两个数字,但无法做到。 我认为它应该足够简单,但我想并非如此。我认为一切都很好,但它根本不起作用。
import 'package:flutter/material.dart';
void main() { runApp(ClassMaterial()); }
class ClassMaterial extends StatefulWidget { @override _Tax
createState() => _Tax(); }
class _Tax extends State<ClassMaterial> {
bool _value = false;
bool a = false;
bool b = false;
final TextEditingController x = TextEditingController();
final TextEditingController y = TextEditingController();
var basic = 0, other = 0, sum = 0;
void doSum() {
setState(() {
basic = int.parse(x.text);
other = int.parse(y.text);
sum += basic + other;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('SALARY TAX CALCULATOR'),
centerTitle: true,
),
body: Card(
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Container(
margin: EdgeInsets.fromLTRB(10, 10, 0, 0),
child: Text('GENDER',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold)),
),
Row(
children: <Widget>[
Container(margin: EdgeInsets.fromLTRB(20, 0, 15, 0)),
Text('Male'),
(Checkbox(
value: _value,
onChanged: (value) {
setState(() {
_value = value;
a = false;
b = false;
});
}))
],
),
Row(
children: <Widget>[
Container(margin: EdgeInsets.fromLTRB(20, 0, 0, 0)),
Text('Female'),
(Checkbox(
value: a,
onChanged: (value) {
setState(() {
a = value;
_value = false;
b = false;
});
}))
],
),
Row(
children: <Widget>[
Container(margin: EdgeInsets.fromLTRB(20, 0, 3, 0)),
Text('Couple'),
(Checkbox(
value: b,
onChanged: (value) {
setState(() {
b = value;
_value = false;
a = false;
});
}))
],
),
Container(
margin: EdgeInsets.fromLTRB(10, 10, 0, 0),
child: Text('SALARY DETAILS',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold)),
),
TextField(
controller: x,
decoration: InputDecoration(
labelText: "Basic Salary(Monthly)",
fillColor: Colors.white,
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
),
keyboardType: TextInputType.number,
style: new TextStyle(
fontFamily: "Poppins",
),
),
TextField(
controller: y,
decoration: InputDecoration(
labelText: "Other Salary(Monthly)",
fillColor: Colors.white,
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
),
keyboardType: TextInputType.number,
style: new TextStyle(
fontFamily: "Poppins",
),
),
Text("$sum")
])),
),
);
} }
【问题讨论】:
-
你没有在任何地方调用 doSum() 函数对吗?
-
请不要在您的问题中多次复制粘贴相同的文本,也不要忽略其他的内容。