반응형
onchange 를 쓰면 해당 값이 바로 변수에 저장이 되어 다른 곳에 영향을 줄 수 있게 된다
조금 더 공부해봐야 겠다..!!
ListTile(
leading: IconButton(
icon: Icon(Icons.edit),
onPressed: () {
showDialog(
context: context,
builder: (context) {
String name;
double weight;
double price;
String brand;
return AlertDialog(
title: Text('Add Ingredient'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
onChanged: (value) => name = value,
),
TextFormField(
decoration: InputDecoration(labelText: 'Weight'),
keyboardType: TextInputType.number,
onChanged: (value) => weight = double.tryParse(value),
),
TextFormField(
decoration: InputDecoration(labelText: 'Price'),
keyboardType: TextInputType.number,
onChanged: (value) => price = double.tryParse(value),
),
TextFormField(
decoration: InputDecoration(labelText: 'Brand'),
onChanged: (value) => brand = value,
),
],
),
actions: [
TextButton(
onPressed: () {
// Add the new ingredient to the list and close the dialog
setState(() {
ingredients.add(Ingredient(
name: name,
weight: weight,
price: price,
brand: brand,
));
});
Navigator.pop(context);
},
child: Text('Save'),
),
],
);
},
);
},
),
title: Text('고추장'),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('${ingredient.price / ingredient.weight} g'),
TextFormField(
decoration: InputDecoration(labelText: 'Amount'),
keyboardType: TextInputType.number,
onChanged: (value) {
// Calculate the total price and update the display
double inputAmount = double.tryParse(value) ?? 0.0;
double totalPrice = inputAmount * (ingredient.price / ingredient.weight);
setState(() {
_totalPrice = totalPrice;
});
},
),
Text('$_totalPrice'),
],
),
);
반응형
'진행중인 프로젝트 > 프로젝트 2 : 원가 계산기' 카테고리의 다른 글
외식업 원가계산기 진행 요약 (0) | 2023.05.21 |
---|---|
원가계산기 : 승민님 코드 읽기 (0) | 2023.03.01 |
원가계산기 진행상황 2 : onchange 이용하기 (0) | 2023.02.20 |
원가계산기 진행상황 1 : 리스트 안에 리스트? (0) | 2023.02.19 |
원가계산기 CardWidget (0) | 2023.01.11 |