Hi I'm trying to convert different units of weight. when I enter an amount and clear it, the 2nd text field would still show the amount other than showing "0". How can I get through this issue. There are 2 pictures below.
[
I want to get the 2000 to remove after deleting too. my code is below.
class Weight extends StatefulWidget {
@override
_WeightState createState() => _WeightState();
}
class _WeightState extends State<Weight> {
String _value1;
String _value2;
TextEditingController _controller;
TextEditingController _controller2;
static final Map<String,double> changes= {
"gram-kilogram": 0.001,
"kilogram-gram": 1000,
"kilogram-kilogram": 1,
"gram-gram": 1,
};
initState(){
super.initState();
_controller=TextEditingController();
_controller2=TextEditingController();
}
dispose(){
super.dispose();
_controller.dispose();
_controller2.dispose();
}
There is more code here I deleted cause too much code.
new TextField(
controller: _controller,
onChanged: (text)=>{
_controller2.text=(double.parse(text)*changes[_value1+"-"+_value2]).toString(),
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: ("Enter amount in $_value1")
),
keyboardType: TextInputType.number,
style: new TextStyle(
fontFamily: "Poppins",
),
),
new Padding(padding: EdgeInsets.only(top: 50.0)),
new DropdownButton(
items: [
DropdownMenuItem(child: Text("Kilogram"),
value: "kilogram",
onTap: (){
},),
DropdownMenuItem(child: Text("gram"),
value: "gram",
onTap: (){
},),
],
onChanged: (String value){
setState((){
_value2 = value;
print("$value");
});
},
hint: Text('Select Item'),
value: _value2,
),
new TextField(
controller: _controller2,
onChanged: (text)=>{
_controller.text=(double.parse(text)*changes[_value2+"-"+_value1]).toString(),
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: ("Enter amount in $_value2")
),),],),),);}}
