Commit 8ba0f2df authored by Quentin DU BOIS's avatar Quentin DU BOIS

Upload New File

parent 158d47da
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
const entryStyle = TextStyle(
fontSize: 25,
color: Colors.indigo,
);
const labelStyle = TextStyle(
fontSize: 20,
color: Colors.black54,
);
class MyApp extends StatefulWidget {
const MyApp({ Key? key }) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double _enteredNumber = 0.0;
String _startingUnit = '';
String _arrivalUnit = '';
String? _message = '';
final Map<String, int> _measureMap = {
'meters' : 0,
'kilometers' : 1,
'grams' : 2,
'kilograms' : 3,
'feet' : 4,
'miles' : 5,
'pounds' : 6,
'ounces' : 7
};
final dynamic _formulas = {
'0': [1, 0.001, 0, 0, 3.28084, 0.000621371, 0, 0],
'1': [1000, 1, 0, 0, 3280.84, 0.621371, 0, 0],
'2': [0, 0, 1, 0.0001, 0, 0, 0.00220462, 0.035274],
'3': [0, 0, 1000, 1, 0, 0, 2.20462, 35.274],
'4': [0.3048, 0.0003048, 0, 0, 1 , 0.000189394, 0, 0],
'5': [1609.34, 1.60934, 0, 0, 5280 , 1, 0, 0],
'6': [0, 0, 453.592, 0.453592, 0, 0, 1, 16],
'7': [0, 0, 28.3495, 0.0283495, 0, 0, 0.0625, 1 ],
};
@override
void initState() {
_enteredNumber = 0.0;
_startingUnit = _measureMap.keys.first;
_arrivalUnit = _measureMap.keys.first;
super.initState();
}
void convert(double value, String from, String to) {
int numFrom = _measureMap[from] ?? 0;
int numTo = _measureMap[to] ?? 0;
var multiplier = _formulas[numFrom.toString()][numTo];
var result = value * multiplier;
String message;
if (result == 0) {
message = 'This conversion cannot be released, please try an other value';
}
else {
message = '${value.toString()} $to\n is equal to\n${result.toString()} $to';
setState(() {
_message = message;
});
}
}
// Widgets
//============================================================================
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Measures convertor',
home: Scaffold(
appBar: AppBar(
title: const Text('Measures convertor'),
backgroundColor: Colors.indigo,
),
body: Container(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
children: [
const Spacer(),
const Text(
'Enter a measure to convert :',
style: entryStyle,
),
TextField(
onChanged: (text) {
var vr = double.tryParse(text);
if (vr != null) {
setState(() {
_enteredNumber = vr;
});
}
},
),
const Spacer(),
const Text(
'From',
style: labelStyle,
),
DropdownButton(
value: _startingUnit,
items: _measureMap.keys.map((String unit) {
return DropdownMenuItem<String>(
value: unit,
child: Text(unit),
);
}).toList(),
onChanged: (String? unit) {
setState(() {
_startingUnit = unit ?? _measureMap.keys.first;
});
},
),
const Spacer(),
const Text(
'to',
style: labelStyle,
),
DropdownButton(
value: _arrivalUnit,
items: _measureMap.keys.map((String unit) {
return DropdownMenuItem<String>(
value: unit,
child: Text(unit),
);
}).toList(),
onChanged: (String? unit) {
setState(() {
_arrivalUnit = unit ?? _measureMap.keys.first;
});
},
),
const Spacer(
flex: 2,
),
ElevatedButton(
onPressed: () {
if (_enteredNumber == 0) {
return;
}
else {
convert(_enteredNumber, _startingUnit, _arrivalUnit);
}
},
child: const Text(
'Convert',
style: entryStyle,
),
),
const Spacer(),
Text(
_message ?? '',
textAlign: TextAlign.center,
style: labelStyle,
),
const Spacer(
flex: 8,
),
],
),
),
),
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment