meal.dart 2.59 KB
Newer Older
1 2 3 4
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
part of fitness;
6 7 8 9 10

class Meal extends FitnessItem {
  Meal({ DateTime when, this.description }) : super(when: when);

  final String description;
11 12 13 14

  FitnessItemRow toRow({ FitnessItemHandler onDismissed }) {
    return new MealRow(meal: this, onDismissed: onDismissed);
  }
15 16 17 18 19 20
}

class MealRow extends FitnessItemRow {
  MealRow({ Meal meal, FitnessItemHandler onDismissed })
    : super(item: meal, onDismissed: onDismissed);

21
  Widget buildContent(BuildContext context) {
22
    Meal meal = item;
Hixie's avatar
Hixie committed
23
    List<Widget> children = <Widget>[
24 25 26 27 28 29 30 31 32
      new Flexible(
        child: new Text(
          meal.description,
          style: const TextStyle(textAlign: TextAlign.right)
        )
      ),
      new Flexible(
        child: new Text(
          meal.displayDate,
33
          style: Theme.of(context).text.caption.copyWith(textAlign: TextAlign.right)
34 35 36
        )
      )
    ];
37
    return new Row(
38 39
      children,
      alignItems: FlexAlignItems.baseline,
40
      textBaseline: DefaultTextStyle.of(context).textBaseline
41 42 43 44 45
    );
  }
}

class MealFragment extends StatefulComponent {
Ian Hickson's avatar
Ian Hickson committed
46
  MealFragment({ this.onCreated });
47 48 49

  FitnessItemHandler onCreated;

50 51
  MealFragmentState createState() => new MealFragmentState();
}
52

53
class MealFragmentState extends State<MealFragment> {
54 55
  String _description = "";

56
  void _handleSave() {
57
    config.onCreated(new Meal(when: new DateTime.now(), description: _description));
Ian Hickson's avatar
Ian Hickson committed
58
    Navigator.of(context).pop();
59 60 61 62 63 64
  }

  Widget buildToolBar() {
    return new ToolBar(
      left: new IconButton(
        icon: "navigation/close",
Ian Hickson's avatar
Ian Hickson committed
65
        onPressed: Navigator.of(context).pop),
66
      center: new Text('New Meal'),
Hixie's avatar
Hixie committed
67
      right: <Widget>[
68 69
        // TODO(abarth): Should this be a FlatButton?
        new InkWell(
70
          onTap: _handleSave,
71 72
          child: new Text('SAVE')
        )
73
      ]
74 75 76 77 78 79 80 81 82
    );
  }

  void _handleDescriptionChanged(String description) {
    setState(() {
      _description = description;
    });
  }

83 84
  static final GlobalKey descriptionKey = new GlobalKey();

85 86
  Widget buildBody() {
    Meal meal = new Meal(when: new DateTime.now());
Hixie's avatar
Hixie committed
87 88 89 90 91 92 93 94 95
    return new Block(<Widget>[
        new Text(meal.displayDate),
        new Input(
          key: descriptionKey,
          placeholder: 'Describe meal',
          onChanged: _handleDescriptionChanged
        ),
      ],
      padding: const EdgeDims.all(20.0)
96 97 98
    );
  }

99
  Widget build(BuildContext context) {
100
    return new Scaffold(
Adam Barth's avatar
Adam Barth committed
101
      toolBar: buildToolBar(),
102 103 104 105
      body: buildBody()
    );
  }
}