반응형

 

 

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'),
    ],
  ),
);
반응형

+ Recent posts