Skip to content

Instantly share code, notes, and snippets.

@paolorotolo
Created November 27, 2018 17:18
Show Gist options
  • Save paolorotolo/354d307c1da8874449bb3ed6f596456e to your computer and use it in GitHub Desktop.
Save paolorotolo/354d307c1da8874449bb3ed6f596456e 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<Unit>> 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();
}
fromUnit = widget.units[0];
toUnit = widget.units[1];
} // 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 = Padding(
padding: EdgeInsets.all(16) ,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextField(
onChanged: (String text){
setState(() {
inputValue=double.parse(text);
});
},
decoration: InputDecoration(
labelText: "Input" ,
),
keyboardType: TextInputType.number,
),
DropdownButtonHideUnderline(
child: DropdownButton(
items: unitsDropDownItems,
onChanged: (Unit unit) {
setState(() {
fromUnit = unit;
});
},
value: fromUnit,
),
)
],
),
);
// TODO: Create a compare arrows icon.
var arrow = RotatedBox(
child: Icon(Icons.compare_arrows),
quarterTurns: 1,
);
// TODO: Create the 'output' group of widgets. This is a Column that
// includes the output value, and 'to' unit [Dropdown].
var output = Padding(
padding: EdgeInsets.all(16) ,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(inputValue.toString()),
DropdownButtonHideUnderline(
child: DropdownButton(
items: unitsDropDownItems,
onChanged: (Unit unit) {
setState(() {
toUnit = unit;
});
},
value: toUnit,
),
)
],
),
);
// TODO: Return the input, arrows, and output widgets, wrapped in a Column.
// TODO: Delete the below placeholder code.
return Column(
children: <Widget>[
input,
arrow,
output,
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment