Skip to content

Instantly share code, notes, and snippets.



Last active Nov 27, 2018
What would you like to do?
// 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.color,
@required this.units,
}) : assert(name != null),
assert(color != null),
assert(units != null);
_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<Unit>> unitsDropDownItems = List();
Unit fromUnit; // unit di partenza
Unit toUnit; // unit d'arrivo
double inputValue; // valore immesso
String covertedValue; // valore convertito
void initState() {
for (var u in widget.units){
child: Text(,
value: u,
} // 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;
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 = Padding(
padding: EdgeInsets.all(16) ,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
onChanged: (String text){
decoration: InputDecoration(
labelText: "Input" ,
child: DropdownButton(
items: unitsDropDownItems,
onChanged: (Unit unit) {
// TODO: Setstate
value: unitsDropDownItems[0].value,
// 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.
return Column(
children: <Widget>[
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.