반응형

아래는 바드가 짜준 코드이다. 

 

내가 막히는 부분이... Recipe안에 ingredients의 리스트가 존재하는 것을 다루는 법이다. 좀 헷갈린다. 

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Recipe Calculator',
      home: Provider<RecipeCalculatorModel>(
        create: (context) => RecipeCalculatorModel(),
        child: RecipeCalculatorView(),
      ),
    );
  }
}

class RecipeCalculatorModel with ChangeNotifier {
  String _recipeName = '';
  List<Ingredient> _ingredients = [];

  void setRecipeName(String recipeName) {
    _recipeName = recipeName;
    notifyListeners();
  }

  void addIngredient(Ingredient ingredient) {
    _ingredients.add(ingredient);
    notifyListeners();
  }

  String getRecipeName() {
    return _recipeName;
  }

  List<Ingredient> getIngredients() {
    return _ingredients;
  }
}

class Ingredient {
  String name;
  int quantity;

  Ingredient(this.name, this.quantity);
}

class RecipeCalculatorView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final model = Provider.of<RecipeCalculatorModel>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Recipe Calculator'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              decoration: InputDecoration(labelText: 'Recipe Name'),
              onChanged: (text) {
                model.setRecipeName(text);
              },
            ),
            TextField(
              decoration: InputDecoration(labelText: 'Ingredient Name'),
              onChanged: (text) {
                model.addIngredient(Ingredient(text, 1));
              },
            ),
            TextField(
              decoration: InputDecoration(labelText: 'Ingredient Quantity'),
              keyboardType: TextInputType.number,
              onChanged: (text) {
                model.ingredients[model.ingredients.length - 1].quantity = int.parse(text);
              },
            ),
            RaisedButton(
              child: Text('Calculate'),
              onPressed: () {
                print(model.recipeName);
                print(model.ingredients);
              },
            ),
          ],
        ),
      ),
    );
  }
}
이 코드는 Provider를 사용하여 레시피 계산기를 구현합니다. RecipeCalculatorModel은 레시피 이름, 재료, 양을 저장하고 계산하는 기능을 제공합니다. RecipeCalculatorView는 RecipeCalculatorModel을 제공받아 화면에 표시합니다.

더미 데이터는 다음과 같습니다.

RecipeCalculatorModel model = RecipeCalculatorModel();
model.setRecipeName('Spaghetti Carbonara');
model.addIngredient('Spaghetti', 1);
model.addIngredient('Eggs', 2);
model.addIngredient('Parmesan cheese', 1/2);
model.addIngredient('Pancetta', 1/2);

 

 

 

hive 를 이용한 데이터 저장 기능 코드 

 

class Recipe {
  String name;
  List<Ingredient> ingredients;

  Recipe({this.name, this.ingredients});
}

class Ingredient {
  String name;
  int quantity;

  Ingredient({this.name, this.quantity});
}

void main() {
  final recipe = Recipe(
    name: 'Spaghetti Carbonara',
    ingredients: [
      Ingredient(name: 'Spaghetti', quantity: 1),
      Ingredient(name: 'Eggs', quantity: 2),
      Ingredient(name: 'Parmesan cheese', quantity: 1/2),
      Ingredient(name: 'Pancetta', quantity: 1/2),
    ],
  );

  final hiveBox = Hive.box<Recipe>('recipes');
  hiveBox.put('spaghetti_carbonara', recipe);

  final retrievedRecipe = hiveBox.get('spaghetti_carbonara');

  print(retrievedRecipe.name);
  print(retrievedRecipe.ingredients);
}
반응형

+ Recent posts