這是我的文本控制器
final amountController = TextEditingController();
@override
void dispose() {
amountController.dispose();
super.dispose();
}
當我想將一個專案添加到串列中時,一個圖示按鈕顯示了一個帶有文本欄位的對話框,我應該在其中獲取輸入。
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);
},
),
));
}),
問題是我不知道如何在這里保存資料
List<int> transactions = [];
int amount = 0;
uj5u.com熱心網友回復:
您不應該transactions.add(s);在 onChanged 方法中寫入這一行,因為這樣在您輸入的每個數字中都會向串列中添加一個值。所以你必須將 onChanged 值分配給數量值并在按鈕的 onPressed 方法中向對話框添加一個按鈕添加這一行transactions.add(s);
uj5u.com熱心網友回復:
我嘗試了另一種方法:
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);
});
},
),
],
);
});
}
我只是在一個平面按鈕中呼叫了這個函式
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/459945.html
下一篇:Flutter動態底部導航欄
