Skip to content

Instantly share code, notes, and snippets.

@paolorotolo
Last active November 27, 2018 17:18
Show Gist options
  • Save paolorotolo/9daa7b9f51ddbae32d80dbd31c1830e1 to your computer and use it in GitHub Desktop.
Save paolorotolo/9daa7b9f51ddbae32d80dbd31c1830e1 to your computer and use it in GitHub Desktop.
// Copyright 2018 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.
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
import 'unit.dart';
const _padding = EdgeInsets.all(16.0);
/// [ConverterRoute] where users can input amounts to convert in one [Unit]
/// and retrieve the conversion in another [Unit] for a specific [Category].
///
/// While it is named ConverterRoute, a more apt name would be ConverterScreen,
/// because it is responsible for the UI at the route's destination.
class ConverterRoute extends StatefulWidget {
/// This [Category]'s name.
final String name;
/// Color for this [Category].
final Color color;
/// Units for this [Category].
final List<Unit> units;
/// This [ConverterRoute] requires the name, color, and units to not be null.
const ConverterRoute({
@required this.name,
@required this.color,
@required this.units,
}) : assert(name != null),
assert(color != null),
assert(units != null);
@override
_ConverterRouteState createState() => _ConverterRouteState();
}
class _ConverterRouteState extends State<ConverterRoute> {
// TODO: Set some variables, such as for keeping track of the user's input
// value and units
List<DropdownMenuItem> unitsDropDownItems = List();
Unit fromUnit; // unit di partenza
Unit toUnit; // unit d'arrivo
double inputValue; // valore immesso
String covertedValue; // valore convertito
@override
void initState() {
for (var u in widget.units){
unitsDropDownItems.add(DropdownMenuItem(
child: Text(u.name),
value: u,
));
super.initState();
}
} // TODO: Determine whether you need to override anything, such as initState()
// TODO: Add other helper functions. We've given you one, _format()
/// Clean up conversion; trim trailing zeros, e.g. 5.500 -> 5.5, 10.0 -> 10
String _format(double conversion) {
var outputNum = conversion.toStringAsPrecision(7);
if (outputNum.contains('.') && outputNum.endsWith('0')) {
var i = outputNum.length - 1;
while (outputNum[i] == '0') {
i -= 1;
}
outputNum = outputNum.substring(0, i + 1);
}
if (outputNum.endsWith('.')) {
return outputNum.substring(0, outputNum.length - 1);
}
return outputNum;
}
@override
Widget build(BuildContext context) {
// TODO: Create the 'input' group of widgets. This is a Column that
// includes the input value, and 'from' unit [Dropdown].
var input = Column(
children: <Widget>[
TextField(),
DropdownButton(
items: null,
onChanged: null,
value: null,
)
],
);
// TODO: Create a compare arrows icon.
// TODO: Create the 'output' group of widgets. This is a Column that
// includes the output value, and 'to' unit [Dropdown].
// TODO: Return the input, arrows, and output widgets, wrapped in a Column.
// TODO: Delete the below placeholder code.
final unitWidgets = widget.units.map((Unit unit) {
return Container(
color: widget.color,
margin: EdgeInsets.all(8.0),
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Text(
unit.name,
style: Theme.of(context).textTheme.headline,
),
Text(
'Conversion: ${unit.conversion}',
style: Theme.of(context).textTheme.subhead,
),
],
),
);
}).toList();
return ListView(
children: unitWidgets,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment