This is my text controller
final amountController = TextEditingController();
@override
void dispose() {
amountController.dispose();
super.dispose();
}
and when i want to add an item to a list a got an iconbutton that shows a dialog box with a text field where im supposed to get inputs.
IconButton(
icon: Icon(Icons.add),
onPressed: () {
// //this is the part where i add a new income to my list
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Add income'),
content: TextField(
keyboardType: TextInputType.number,
controller: amountController,
onChanged: (s) {
int s = int.parse(amountController.text);
amount = s;
transactions.add(s);
},
),
));
}),
the probleme is that i dont know how to save the data here
List<int> transactions = [];
int amount = 0;
CodePudding user response:
you should not write this line transactions.add(s);
in onChanged method, because in this way in each number you enter you add a value to the list.
so you have to assign onChanged value to amount value and add a button to your dialog in button's onPressed method add this line transactions.add(s);
CodePudding user response:
I tried another way :
TextEditingController _textFieldController = TextEditingController();
List<int> transactions = [];
int Total = 0;
Future<void> _displayTextInputDialog(BuildContext context) async {
int Amount = 0;
String valueText = '';
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Add income'),
content: TextField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
valueText = value;
});
},
controller: _textFieldController,
),
actions: <Widget>[
FlatButton(
color: Colors.red,
textColor: Colors.white,
child: Text('CANCEL'),
onPressed: () {
setState(() {
Navigator.pop(context);
});
},
),
FlatButton(
color: Colors.green,
textColor: Colors.white,
child: Text('OK'),
onPressed: () {
setState(() {
Amount = int.parse(valueText);
transactions.add(Amount);
Total = Total Amount;
print(Amount);
Navigator.pop(context);
});
},
),
],
);
});
}
and i just called the function within a flatbutton