meal.dart 2.63 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
      children: children,
39
      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));
Hixie's avatar
Hixie committed
58
    Navigator.pop(context);
59 60 61 62 63 64
  }

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

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

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

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

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