Created with <3 with dartpad.dev.
Last active
June 17, 2023 18:18
-
-
Save raphaelrss/25e8fa45bfbcc313e3700a9d0b52286e to your computer and use it in GitHub Desktop.
trabalho mobile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
List<Map<String, String>>? resultList; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Dropdown App', | |
theme: ThemeData(primarySwatch: Colors.blue), | |
routes: { | |
'/': (context) => HomePage(), | |
'/result': (context) => ResultPage(), | |
}, | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
String? _selectedOption1; | |
String? _selectedOption2; | |
final List<Map<String, String>> _options1 = [ | |
{'cod':'ARS', 'moeda': 'Peso Argentino'}, | |
{'cod':'BRL', 'moeda': 'Real Brasileiro'}, | |
{'cod':'BTC', 'moeda': 'Bitcoin'}, | |
{'cod':'BWP', 'moeda': 'Pula de Botswana'}, | |
{'cod':'BYN', 'moeda': 'Rublo Bielorrusso'}, | |
{'cod':'BZD', 'moeda': 'Dólar de Belize'}, | |
{'cod':'CAD', 'moeda': 'Dólar Canadense'}, | |
{'cod':'CHF', 'moeda': 'Franco Suíço'}, | |
{'cod':'CHFRTS', 'moeda': 'Franco Suíço'}, | |
{'cod':'CLP', 'moeda': 'Peso Chileno'}, | |
{'cod':'CNY', 'moeda': 'Yuan Chinês'}, | |
{'cod':'COP', 'moeda': 'Peso Colombiano'}, | |
{'cod':'DOGE', 'moeda': 'Dogecoin'}, | |
{'cod':'ETH', 'moeda': 'Ethereum'}, | |
{'cod':'EUR', 'moeda': 'Euro'}, | |
{'cod':'GBP', 'moeda': 'Libra Esterlina'}, | |
{'cod':'HKD', 'moeda': 'Dólar de Hong Kong'}, | |
{'cod':'JPY', 'moeda': 'Iene Japonês'}, | |
{'cod':'KYD', 'moeda': 'Dólar das Ilhas Cayman'}, | |
{'cod':'LTC', 'moeda': 'Litecoin'}, | |
{'cod':'MXN', 'moeda': 'Peso Mexicano'}, | |
{'cod':'RUB', 'moeda': 'Rublo Russo'}, | |
{'cod':'USD', 'moeda': 'Dólar Americano'}, | |
{'cod':'UYU', 'moeda': 'Peso Uruguaio'}, | |
{'cod':'UZS', 'moeda': 'Som Uzbequistanês'}, | |
{'cod':'VEF', 'moeda': 'Bolívar Venezuelano'}, | |
{'cod':'VND', 'moeda': 'Dong Vietnamita'}, | |
{'cod':'VUV', 'moeda': 'Vatu de Vanuatu'}, | |
{'cod':'XAF', 'moeda': 'Franco CFA Central'}, | |
] | |
; | |
final List<Map<String, String>> _options2 = [ | |
{'cod':'ARS', 'moeda': 'Peso Argentino'}, | |
{'cod':'BRL', 'moeda': 'Real Brasileiro'}, | |
{'cod':'BTC', 'moeda': 'Bitcoin'}, | |
{'cod':'BWP', 'moeda': 'Pula de Botswana'}, | |
{'cod':'BYN', 'moeda': 'Rublo Bielorrusso'}, | |
{'cod':'BZD', 'moeda': 'Dólar de Belize'}, | |
{'cod':'CAD', 'moeda': 'Dólar Canadense'}, | |
{'cod':'CHF', 'moeda': 'Franco Suíço'}, | |
{'cod':'CHFRTS', 'moeda': 'Franco Suíço'}, | |
{'cod':'CLP', 'moeda': 'Peso Chileno'}, | |
{'cod':'CNY', 'moeda': 'Yuan Chinês'}, | |
{'cod':'COP', 'moeda': 'Peso Colombiano'}, | |
{'cod':'DOGE', 'moeda': 'Dogecoin'}, | |
{'cod':'ETH', 'moeda': 'Ethereum'}, | |
{'cod':'EUR', 'moeda': 'Euro'}, | |
{'cod':'GBP', 'moeda': 'Libra Esterlina'}, | |
{'cod':'HKD', 'moeda': 'Dólar de Hong Kong'}, | |
{'cod':'JPY', 'moeda': 'Iene Japonês'}, | |
{'cod':'KYD', 'moeda': 'Dólar das Ilhas Cayman'}, | |
{'cod':'LTC', 'moeda': 'Litecoin'}, | |
{'cod':'MXN', 'moeda': 'Peso Mexicano'}, | |
{'cod':'RUB', 'moeda': 'Rublo Russo'}, | |
{'cod':'USD', 'moeda': 'Dólar Americano'}, | |
{'cod':'UYU', 'moeda': 'Peso Uruguaio'}, | |
{'cod':'UZS', 'moeda': 'Som Uzbequistanês'}, | |
{'cod':'VEF', 'moeda': 'Bolívar Venezuelano'}, | |
{'cod':'VND', 'moeda': 'Dong Vietnamita'}, | |
{'cod':'VUV', 'moeda': 'Vatu de Vanuatu'}, | |
{'cod':'XAF', 'moeda': 'Franco CFA Central'}, | |
]; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Conversion App'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Center( | |
child: Column( | |
children: [ | |
DropdownButton<String>( | |
hint: Text('Selecione uma opção'), | |
value: _selectedOption1, | |
onChanged: (newValue) { | |
setState(() { | |
_selectedOption1 = newValue; | |
}); | |
}, | |
items: _options1.map((option) { | |
return DropdownMenuItem( | |
value: option['cod'], | |
child: Text('${option['moeda']}'), | |
); | |
}).toList(), | |
), | |
SizedBox(height: 16.0), | |
DropdownButton<String>( | |
hint: Text('Selecione uma opção'), | |
value: _selectedOption2, | |
onChanged: (newValue) { | |
setState(() { | |
_selectedOption2 = newValue; | |
}); | |
}, | |
items: _options2.map((option) { | |
return DropdownMenuItem( | |
value: option['cod'], | |
child: Text('${option['moeda']}'), | |
); | |
}).toList(), | |
), | |
SizedBox(height: 16.0), | |
ElevatedButton( | |
onPressed: () { | |
Navigator.pushNamed( | |
context, | |
'/result', | |
arguments: ResultArguments( | |
option1: _selectedOption1, | |
option2: _selectedOption2, | |
data: fetchData(_selectedOption1, _selectedOption2) | |
), | |
); | |
}, | |
child: Text('Enviar'), | |
), | |
], | |
), | |
) | |
), | |
); | |
} | |
} | |
class ResultArguments { | |
final String? option1; | |
final String? option2; | |
final Future<List<Map<String, String>>>? data; | |
ResultArguments({required this.option1, required this.option2, required this.data}); | |
} | |
class ResultPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final args = ModalRoute.of(context)!.settings.arguments as ResultArguments; | |
final data = args.data; | |
data?.then((value) { | |
resultList = value; | |
}); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Resultado'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: FutureBuilder<List<Map<String, String>>>( | |
future: data, | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.waiting) { | |
return Center(child: CircularProgressIndicator()); | |
} else if (snapshot.hasData) { | |
final result = snapshot.data; | |
return Column( | |
children: [ | |
Text( | |
'Opção 1: ${args.option1}', | |
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold), | |
), | |
SizedBox(height: 8.0), | |
Text( | |
'Opção 2: ${args.option2}', | |
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold), | |
), | |
SizedBox(height: 16.0), | |
Expanded( | |
child: Container( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text( | |
'High', | |
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold), | |
), | |
Text( | |
'${resultList?[1]['high']}', | |
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold), | |
), | |
Text( | |
'Low', | |
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold), | |
), | |
Text( | |
'${resultList?[1]['low']}', | |
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold), | |
), | |
], | |
), | |
), | |
), | |
SizedBox(height: 16.0), | |
Text( | |
'Histórico', | |
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold), | |
), | |
SizedBox(height: 8.0), | |
Table( | |
border: TableBorder.all(), | |
children: [ | |
TableRow( | |
children: [ | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
' ', | |
style: TextStyle(fontWeight: FontWeight.bold), | |
), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
'Dia', | |
style: TextStyle(fontWeight: FontWeight.bold), | |
), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
'Mês', | |
style: TextStyle(fontWeight: FontWeight.bold), | |
), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
'Ano', | |
style: TextStyle(fontWeight: FontWeight.bold), | |
), | |
), | |
), | |
], | |
), | |
TableRow( | |
children: [ | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text('Low'), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text('${resultList?[2]['low']}'), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text('${resultList?[3]['low']}'), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text('${resultList?[4]['low']}'), | |
), | |
), | |
], | |
), | |
TableRow( | |
children: [ | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text('High'), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text('${resultList?[2]['high']}'), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text('${resultList?[3]['high']}'), | |
), | |
), | |
TableCell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text('${resultList?[4]['high']}'), | |
), | |
), | |
], | |
), | |
], | |
), | |
], | |
); | |
} else if (snapshot.hasError) { | |
return Text('Error: ${snapshot.error}'); | |
} else { | |
return Text('No data available'); | |
} | |
}, | |
), | |
), | |
); | |
} | |
} | |
Future<List<Map<String, String>>>? fetchData(String? option1, String? option2) async { | |
final url_current = Uri.parse('https://economia.awesomeapi.com.br/last/${option1}-${option2}'); | |
final url_day = Uri.parse('https://economia.awesomeapi.com.br/daily/${option1}-${option2}/1'); | |
final url_month = Uri.parse('https://economia.awesomeapi.com.br/daily/${option1}-${option2}/30'); | |
final url_year = Uri.parse('https://economia.awesomeapi.com.br/daily/${option1}-${option2}/360'); | |
final List<Map<String, String>> data = [{'first': 'first'}]; | |
print('${option1}${option2}'); | |
try { | |
final response_current = await http.get(url_current); | |
if (response_current.statusCode == 200) { | |
data.add( | |
{ | |
'high': json.decode(response_current.body)['${option1}${option2}']['high'], | |
'low': json.decode(response_current.body)['${option1}${option2}']['low'] | |
} | |
); | |
print(data); | |
} else { | |
print('Request failed with status: ${response_current.statusCode}'); | |
} | |
final response_day = await http.get(url_day); | |
if (response_day.statusCode == 200) { | |
data.add( | |
{ | |
'high': json.decode(response_day.body)[0]['high'], | |
'low': json.decode(response_day.body)[0]['low'] | |
} | |
); | |
print(data); | |
} else { | |
print('Request failed with status: ${response_day.statusCode}'); | |
} | |
final response_month = await http.get(url_month); | |
if (response_month.statusCode == 200) { | |
double high = double.parse(json.decode(response_month.body)[0]['high']); | |
double low = double.parse(json.decode(response_month.body)[0]['low']); | |
json.decode(response_month.body).forEach((map) { | |
if(double.parse(map['high']) > high){ | |
high = double.parse(map['high']); | |
} | |
if(double.parse(map['low']) < low){ | |
low = double.parse(map['low']); | |
} | |
}); | |
data.add( | |
{ | |
'high': high.toString(), | |
'low': low.toString() | |
} | |
); | |
print(data); | |
} else { | |
print('Request failed with status: ${response_month.statusCode}'); | |
} | |
final response_year = await http.get(url_year); | |
if (response_year.statusCode == 200) { | |
print(json.decode(response_year.body)); | |
double high = double.parse(json.decode(response_year.body)[0]['high']); | |
double low = double.parse(json.decode(response_year.body)[0]['low']); | |
json.decode(response_year.body).forEach((map) { | |
if(double.parse(map['high']) > high){ | |
high = double.parse(map['high']); | |
} | |
if(double.parse(map['low']) < low){ | |
low = double.parse(map['low']); | |
} | |
}); | |
data.add( | |
{ | |
'high': high.toString(), | |
'low': low.toString() | |
} | |
); | |
print(data); | |
} else { | |
print('Request failed with status: ${response_year.statusCode}'); | |
} | |
} catch (e) { | |
print('Error: $e'); | |
} | |
return data; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment