· 4 years ago · May 28, 2021, 12:26 AM
1import 'dart:convert';
2
3import 'package:flutter/material.dart';
4import 'package:searchable_dropdown/searchable_dropdown.dart';
5import 'package:http/http.dart' as http;
6import 'package:uni_clima/model/clima_model.dart';
7import 'package:uni_clima/widgets/tempo_widget.dart';
8
9class Home extends StatefulWidget {
10 const Home({Key key}) : super(key: key);
11
12 @override
13 _HomeState createState() => _HomeState();
14}
15
16class _HomeState extends State<Home> {
17 ClimaData climaData;
18 bool isLoading = false;
19 List<String> _cidades = [
20 'Aracaju',
21 'Belém',
22 'Belo Horizonte',
23 'Boa Vista',
24 'Brasilia',
25 'Campo Grande',
26 'Cuiaba',
27 'Curitiba',
28 'Florianópolis',
29 'Fortaleza',
30 'Goiânia',
31 'João Pessoa',
32 'Macapá',
33 'Maceió',
34 'Manaus',
35 'Natal',
36 'Palmas',
37 'Porto Alegre',
38 'Porto Velho',
39 'Recife',
40 'Rio Branco',
41 'Rio de Janeiro',
42 'Salvador',
43 'São Luiz',
44 'São Paulo',
45 'Teresina',
46 'Vitoria'
47 ];
48
49 String _cidadeSelecionada = 'São Paulo';
50
51 carregaTempo() async {
52 setState(() {
53 isLoading = true;
54 });
55
56 final String _appid =
57 '70490ee3c06c559a659a5d846008bbd3'; //Coloque aqui a SUA chave de API
58 final String _lang = 'pt_br';
59 final String _units = 'metric';
60 final String _apiURL = 'api.openweathermap.org';
61 final String _path = '/data/2.5/weather';
62 final _params = {
63 "q": _cidadeSelecionada,
64 "appid": _appid,
65 "units": _units,
66 "lang": _lang
67 };
68
69 final climaResponse = await http.get(Uri.https(_apiURL, _path, _params));
70
71 print('Url montada: ' + climaResponse.request.url.toString());
72
73 if (climaResponse.statusCode == 200) {
74 setState(() {
75 isLoading = false;
76 climaData = ClimaData.fromJson(jsonDecode(climaResponse.body));
77 });
78 }
79 }
80
81 @override
82 Widget build(BuildContext context) {
83 return Scaffold(
84 appBar: AppBar(
85 title: Text(_cidadeSelecionada),
86 centerTitle: true,
87 ),
88 body: Center(
89 child: Column(
90 children: [
91 SearchableDropdown.single(
92 items: _cidades
93 .map((e) => DropdownMenuItem(value: e, child: Text(e)))
94 .toList(),
95 onChanged: (value) {
96 setState(() {
97 _cidadeSelecionada = value;
98 carregaTempo();
99 });
100 },
101 displayClearIcon: false,
102 value: _cidadeSelecionada,
103 icon: Icon(Icons.location_on),
104 isExpanded: true,
105 closeButton: "Fechar",
106 ),
107 Expanded(
108 child: Column(
109 mainAxisAlignment: MainAxisAlignment.center,
110 children: [
111 Padding(
112 padding: EdgeInsets.all(6.0),
113 child: isLoading
114 ? CircularProgressIndicator(
115 strokeWidth: 3.0,
116 valueColor: new AlwaysStoppedAnimation(Colors.blue),
117 )
118 : climaData != null
119 ? TempoWidget(climaData: climaData)
120 : Container(
121 child: Text(
122 'Sem dados para exibir',
123 style: Theme.of(context).textTheme.headline4,
124 ),
125 )),
126 Padding(
127 padding: EdgeInsets.all(8.0),
128 child: isLoading
129 ? Container(
130 child: Text(
131 'Carregando...',
132 style: Theme.of(context).textTheme.headline5,
133 ))
134 : IconButton(
135 icon: Icon(Icons.refresh),
136 onPressed: carregaTempo,
137 color: Colors.blue,
138 iconSize: 50.0,
139 tooltip: 'Recarregar',
140 ))
141 ],
142 ))
143 ],
144 ),
145 ),
146 );
147 }
148}
149