Skip to content

Instantly share code, notes, and snippets.

@Yuhtin
Created May 31, 2021 00:47
Show Gist options
  • Save Yuhtin/8aec26bd983ed0a298b3de251147970d to your computer and use it in GitHub Desktop.
Save Yuhtin/8aec26bd983ed0a298b3de251147970d to your computer and use it in GitHub Desktop.
Calculator from Real (BRL) to Euro (EUR) using API
class Conversor {
double value;
double price;
Conversor(double value, double price) {
this.value = value;
this.price = price;
}
double convert() {
return value / price;
}
@override
String toString() {
return '€ ${convert().toStringAsFixed(2)}';
}
}
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'webRequest.dart';
import 'conversorProcessor.dart';
class Homepage extends StatefulWidget {
Homepage({Key key, title: 'Homepage'}) : super(key: key);
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
TextEditingController _requiredValue = TextEditingController();
TextEditingController _euroValue = TextEditingController();
String _result;
Future<Map> _futureData;
void clearData() {
_requiredValue.text = '';
_euroValue.text = 'Loading data...';
setState(() {
var request = new WebRequest();
_result = 'Insert value to convert';
_futureData = request.collectData();
});
}
@override
void initState() {
super.initState();
clearData();
}
void convertCoin() {
double requiredValue = double.parse(_requiredValue.text);
double coinValue = double.parse(_euroValue.text);
setState(
() => _result = new Conversor(requiredValue, coinValue).toString());
}
FloatingActionButton calcButton() {
return FloatingActionButton(
onPressed: () {
if (_formKey.currentState.validate()) {
convertCoin();
}
},
tooltip: 'Calcular',
child: Icon(Icons.calculate_outlined));
}
IconButton clearButton() {
return IconButton(
icon: Icon(Icons.refresh),
onPressed: () {
clearData();
},
);
}
AppBar appBarBuilder() {
return AppBar(
title: Text('Home'),
actions: <Widget>[
clearButton(),
],
);
}
TextFormField textField(
{TextEditingController controller,
String errorMessage,
String requisiteMessage}) {
return TextFormField(
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp('[0-9.,]'))
],
decoration: InputDecoration(labelText: requisiteMessage),
controller: controller,
validator: (text) {
return text.isEmpty ? errorMessage : null;
},
);
}
Padding resultText() {
return Padding(
padding: EdgeInsets.symmetric(vertical: 36),
child: Text(
_result,
textAlign: TextAlign.center,
));
}
Form formBuilder() {
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
textField(
requisiteMessage: 'R\$ to convert',
errorMessage: 'Insert the value to convert',
controller: _requiredValue),
textField(
requisiteMessage: 'Value of 1€ in R\$',
errorMessage: 'Insert euro value',
controller: _euroValue),
resultText(),
],
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appBarBuilder(),
body: FutureBuilder<Map>(
future: _futureData,
builder: (context, snapshot) {
if (snapshot.hasData) {
var priceData = snapshot.data["EURBRL"]["bid"];
_euroValue.text = priceData.toString();
return SingleChildScrollView(
padding: EdgeInsets.all(25), child: formBuilder());
} else if (snapshot.hasError) {
return Text(snapshot.error);
} else {
return Center(
child: LinearProgressIndicator(backgroundColor: Colors.purple));
}
},
),
floatingActionButton: calcButton(),
);
}
}
import 'package:flutter/material.dart';
import 'conversorWidget.dart';
const appName = "Flutter Class";
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appName,
darkTheme: ThemeData.dark().copyWith(primaryColor: Colors.green),
theme: ThemeData(
primarySwatch: Colors.red,
),
home: Homepage(),
debugShowCheckedModeBanner: false,
);
}
}
import 'dart:convert';
import 'package:http/http.dart' as http;
class WebRequest {
static const endpoint = 'https://economia.awesomeapi.com.br/json/last/EUR';
Future<Map> collectData() async {
http.Response response = await http.get(endpoint);
return json.decode(response.body);
}
}
@Yuhtin
Copy link
Author

Yuhtin commented May 31, 2021

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment