measurement.dart 4.12 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
class Measurement extends FitnessItem {
  Measurement({ DateTime when, this.weight }) : super(when: when);
Hixie's avatar
Hixie committed
9
  Measurement.fromJson(Map json) : weight = json['weight'], super.fromJson(json);
10

11 12
  final double weight;

13
  // TODO(jackson): Internationalize
14
  String get displayWeight => "${weight.toStringAsFixed(1)} lbs";
15

16 17 18 19
  @override
  Map toJson() {
    Map json = super.toJson();
    json['weight'] = weight;
20
    json['type'] = runtimeType.toString();
21 22 23
    return json;
  }

24 25 26
  FitnessItemRow toRow({ FitnessItemHandler onDismissed }) {
    return new MeasurementRow(measurement: this, onDismissed: onDismissed);
  }
27 28 29 30 31 32
}

class MeasurementRow extends FitnessItemRow {
  MeasurementRow({ Measurement measurement, FitnessItemHandler onDismissed })
    : super(item: measurement, onDismissed: onDismissed);

33
  Widget buildContent(BuildContext context) {
34
    Measurement measurement = item;
Hixie's avatar
Hixie committed
35
    List<Widget> children = <Widget>[
36 37 38
      new Flexible(
        child: new Text(
          measurement.displayWeight,
39
          style: Theme.of(context).text.subhead
40 41 42 43 44
        )
      ),
      new Flexible(
        child: new Text(
          measurement.displayDate,
45
          style: Theme.of(context).text.caption.copyWith(textAlign: TextAlign.right)
46 47 48
        )
      )
    ];
49
    return new Row(
50 51
      children,
      alignItems: FlexAlignItems.baseline,
52
      textBaseline: DefaultTextStyle.of(context).textBaseline
53 54
    );
  }
55 56
}

57
class MeasurementFragment extends StatefulComponent {
Ian Hickson's avatar
Ian Hickson committed
58
  MeasurementFragment({ this.onCreated });
59

60
  final FitnessItemHandler onCreated;
61

62 63
  MeasurementFragmentState createState() => new MeasurementFragmentState();
}
64

65
class MeasurementFragmentState extends State<MeasurementFragment> {
66
  String _weight = "";
67
  DateTime _when = new DateTime.now();
68

69
  void _handleSave() {
70 71 72
    double parsedWeight;
    try {
      parsedWeight = double.parse(_weight);
73 74
    } on FormatException catch(e) {
      print("Exception $e");
Hixie's avatar
Hixie committed
75
      Scaffold.of(context).showSnackBar(new SnackBar(
76
        content: new Text('Save failed')
Hixie's avatar
Hixie committed
77
      ));
78
    }
79
    config.onCreated(new Measurement(when: _when, weight: parsedWeight));
Hixie's avatar
Hixie committed
80
    Navigator.pop(context);
81 82 83 84 85 86
  }

  Widget buildToolBar() {
    return new ToolBar(
      left: new IconButton(
        icon: "navigation/close",
Hixie's avatar
Hixie committed
87 88
        onPressed: () => Navigator.pop(context)
      ),
89
      center: new Text('New Measurement'),
Hixie's avatar
Hixie committed
90
      right: <Widget>[
91 92
        // TODO(abarth): Should this be a FlatButton?
        new InkWell(
93
          onTap: _handleSave,
94 95
          child: new Text('SAVE')
        )
96
      ]
97 98 99 100 101 102 103 104 105
    );
  }

  void _handleWeightChanged(String weight) {
    setState(() {
      _weight = weight;
    });
  }

106 107
  static final GlobalKey weightKey = new GlobalKey();

Adam Barth's avatar
Adam Barth committed
108
  Future _handleDatePressed() async {
109
    DateTime value = await showDatePicker(
Adam Barth's avatar
Adam Barth committed
110
      context: context,
111 112 113
      initialDate: _when,
      firstDate: new DateTime(2015, 8),
      lastDate: new DateTime(2101)
Adam Barth's avatar
Adam Barth committed
114
    );
115
    if (value != _when) {
116 117 118
      setState(() {
        _when = value;
      });
Adam Barth's avatar
Adam Barth committed
119
    }
120 121
  }

122
  Widget buildBody(BuildContext context) {
123 124
    Measurement measurement = new Measurement(when: _when);
    // TODO(jackson): Revisit the layout of this pane to be more maintainable
125 126
    return new Container(
      padding: const EdgeDims.all(20.0),
Hixie's avatar
Hixie committed
127
      child: new Column(<Widget>[
128 129 130 131
        new GestureDetector(
          onTap: _handleDatePressed,
          child: new Container(
            height: 50.0,
Hixie's avatar
Hixie committed
132
            child: new Column(<Widget>[
133 134 135 136 137 138 139 140 141 142 143 144
              new Text('Measurement Date'),
              new Text(measurement.displayDate, style: Theme.of(context).text.caption),
            ], alignItems: FlexAlignItems.start)
          )
        ),
        new Input(
          key: weightKey,
          placeholder: 'Enter weight',
          keyboardType: KeyboardType.NUMBER,
          onChanged: _handleWeightChanged
        ),
      ], alignItems: FlexAlignItems.stretch)
145 146 147
    );
  }

148
  Widget build(BuildContext context) {
149
    return new Scaffold(
Adam Barth's avatar
Adam Barth committed
150
      toolBar: buildToolBar(),
Hixie's avatar
Hixie committed
151
      body: buildBody(context)
152 153 154
    );
  }
}