flutter/examples/fitness/lib/settings.dart
James Robinson e854d7457d Update to Mojo 4e4d51ce28a8edcb32b9c7f555e38e2ae84a825e, update deps
This updates to mojo 4e4d51ce28a and mojo sdk 711a0bcfb141b4 and updates the sky
package's pubspec.yaml dependency to '>=0.1.0 <0.2.0' to be compatible with
the current mojo package. This includes an update to the Mojo Dart generator to
produce real classes for enums and the corresponding updates for users of the
KeyboardType enum in Sky as well as one scoped_ptr->std::unique_ptr in shell
corresponding to a change in the Mojo EDK.

When a new version of the sky and sky_services package are pushed this will fix
domokit/mojo#440.
2015-09-23 17:26:46 -07:00

133 lines
3.5 KiB
Dart

// Copyright 2015 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.
part of fitness;
typedef void SettingsUpdater({
BackupMode backup,
double goalWeight
});
class SettingsFragment extends StatefulComponent {
SettingsFragment({ this.navigator, this.userData, this.updater });
Navigator navigator;
UserData userData;
SettingsUpdater updater;
void syncConstructorArguments(SettingsFragment source) {
navigator = source.navigator;
userData = source.userData;
updater = source.updater;
}
void _handleBackupChanged(bool value) {
assert(updater != null);
updater(backup: value ? BackupMode.enabled : BackupMode.disabled);
}
Widget buildToolBar() {
return new ToolBar(
left: new IconButton(
icon: "navigation/arrow_back",
onPressed: navigator.pop),
center: new Text('Settings')
);
}
String get goalWeightText {
if (userData.goalWeight == null || userData.goalWeight == 0.0)
return "None";
else
return "${userData.goalWeight}";
}
static final GlobalKey weightGoalKey = new GlobalKey();
double _goalWeight;
void _handleGoalWeightChanged(String goalWeight) {
// TODO(jackson): Looking for null characters to detect enter key is a hack
if (goalWeight.endsWith("\u{0}")) {
navigator.pop(double.parse(goalWeight.replaceAll("\u{0}", "")));
} else {
setState(() {
try {
_goalWeight = double.parse(goalWeight);
} on FormatException {
_goalWeight = 0.0;
}
});
}
}
void _handleGoalWeightPressed() {
showDialog(navigator, (navigator) {
return new Dialog(
title: new Text("Goal Weight"),
content: new Input(
key: weightGoalKey,
placeholder: 'Goal weight in lbs',
keyboardType: KeyboardType.NUMBER,
onChanged: _handleGoalWeightChanged
),
onDismiss: () {
navigator.pop();
},
actions: [
new FlatButton(
child: new Text('CANCEL'),
onPressed: () {
navigator.pop();
}
),
new FlatButton(
child: new Text('SAVE'),
onPressed: () {
navigator.pop(_goalWeight);
}
),
]
);
}).then((double goalWeight) => updater(goalWeight: goalWeight));
}
Widget buildSettingsPane() {
return new Material(
type: MaterialType.canvas,
child: new ScrollableViewport(
child: new Container(
padding: const EdgeDims.symmetric(vertical: 20.0),
child: new BlockBody([
new DrawerItem(
onPressed: () { _handleBackupChanged(!(userData.backupMode == BackupMode.enabled)); },
child: new Row([
new Flexible(child: new Text('Back up data to the cloud')),
new Switch(value: userData.backupMode == BackupMode.enabled, onChanged: _handleBackupChanged),
])
),
new DrawerItem(
onPressed: () => _handleGoalWeightPressed(),
child: new Column([
new Text('Goal Weight'),
new Text(goalWeightText, style: Theme.of(this).text.caption),
],
alignItems: FlexAlignItems.start
)
),
])
)
)
);
}
Widget build() {
return new Scaffold(
toolbar: buildToolBar(),
body: buildSettingsPane()
);
}
}