Skip to content

Commit

Permalink
Toggle between metric and imperial unit, fix atmosphere weather condi…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
fwrhine committed Nov 24, 2020
1 parent 28946df commit 0cdc19b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 43 deletions.
107 changes: 72 additions & 35 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart' as http;
import 'package:strings/strings.dart';
import 'package:toggle_switch/toggle_switch.dart';
import 'dart:convert';
import 'weatherConditions.dart';

Expand All @@ -30,7 +31,6 @@ class _HomeState extends State<Home> {
var currently;
var humidity;
var windSpeed;
var randomColor;
var city;

var longitude;
Expand All @@ -39,7 +39,8 @@ class _HomeState extends State<Home> {
var conditionCode;
var conditionColor;

var unit = "metric";
var unit = 0; // 0 metric, 1 imperial
var degree = "C"; // C metric, F imperial

Future getLocation() async {
Position position = await Geolocator.getCurrentPosition(
Expand All @@ -52,14 +53,15 @@ class _HomeState extends State<Home> {
}

Future getWeather() async {
String unit = this.unit == 0 ? "metric" : "imperial";
String url = "http://api.openweathermap.org/data/2.5/weather?lat=" +
this.latitude.toString() +
"&lon=" +
this.longitude.toString() +
"&appid=" +
apiKey +
"&units=" +
this.unit;
unit;

http.Response response = await http.get(url);
var results = jsonDecode(response.body);
Expand All @@ -74,6 +76,7 @@ class _HomeState extends State<Home> {
this.city = results['name'];
this.conditionCode = setConditionCode(this.conditionMain);
this.conditionColor = setConditionColor(this.conditionMain);
this.degree = this.unit == 0 ? "C" : "F";
});
}

Expand Down Expand Up @@ -115,40 +118,73 @@ class _HomeState extends State<Home> {
? conditionColor
: [Color(0xFFFFFFFF), Color(0xFFFFFFFF)])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 7.0, top: 100.0),
// child: Icon(Icons.cloud,
// color: Colors.white, size: 50.0),
// child: Image.asset('assets/weather_icons/cloud.png')
child: Container(
width: 100,
height: 100,
child: FlareActor(
"assets/weather_conditions.flr",
animation: conditionCode,
),
),
),
Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: Text(city != null ? city.toString() : "",
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w400)),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Padding(
padding:
EdgeInsets.only(right: 25.0, top: 50.0),
child: ToggleSwitch(
initialLabelIndex: this.unit,
minWidth: 75.0,
activeBgColor: Color(0xFFfa6482),
inactiveFgColor: Colors.white,
labels: ['Metric', 'Imperial'],
onToggle: (index) {
setState(() {
this.unit = index;
_onRefresh();
});
},
))
],
),
Text(
temp != null
? (temp.toInt().round()).toString() +
"\u00B0C"
: "",
style: TextStyle(
color: Colors.white,
fontSize: 50.0,
fontWeight: FontWeight.w400)),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 7.0),
// child: Icon(Icons.cloud,
// color: Colors.white, size: 50.0),
// child: Image.asset('assets/weather_icons/cloud.png')
child: Container(
width: 100,
height: 100,
child: FlareActor(
"assets/weather_conditions.flr",
animation: conditionCode,
),
),
),
Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: Text(
city != null ? city.toString() : "",
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w400)),
),
Padding(
padding: EdgeInsets.only(bottom: 60.0),
child: Text(
temp != null
? (temp.toInt().round()).toString() +
"\u00B0" +
degree
: "",
style: TextStyle(
color: Colors.white,
fontSize: 50.0,
fontWeight: FontWeight.w400)),
),
],
)),
])),
Container(
height: MediaQuery.of(context).size.height / 3,
Expand All @@ -175,7 +211,8 @@ class _HomeState extends State<Home> {
temp != null
? (temp.toInt().round())
.toString() +
"\u00B0C"
"\u00B0" +
degree
: "",
style: TextStyle(fontSize: 16.0)),
),
Expand Down
10 changes: 2 additions & 8 deletions lib/weatherConditions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:math';

String setConditionCode(String condition) {
/* Set Flare animation code based on weather condition.*/
var code;
var code = "50d";

switch (condition) {
case "Thunderstorm":
Expand All @@ -18,9 +18,6 @@ String setConditionCode(String condition) {
case "Snow":
code = "13d";
break;
case "Atmosphere":
code = "50d";
break;
case "Clear":
code = "01d";
break;
Expand All @@ -34,7 +31,7 @@ String setConditionCode(String condition) {

List<Color> setConditionColor(String condition) {
/* Set background color based on weather condition.*/
var color;
var color = [Color(0xFF3C9CF8), Color(0xFFB0D87E)];

switch (condition) {
case "Thunderstorm":
Expand All @@ -49,9 +46,6 @@ List<Color> setConditionColor(String condition) {
case "Snow":
color = [Color(0xFF7EA1E6), Color(0xFF7EE1E6)];
break;
case "Atmosphere":
color = [Color(0xFF3C9CF8), Color(0xFFB0D87E)];
break;
case "Clear":
color = [Color(0xFFFF7474), Color(0xFFFFD0A1)];
break;
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies:
strings: ^0.1.4
geolocator: ^6.1.7
flare_flutter: ^2.0.6
toggle_switch: ^0.1.8

dev_dependencies:
flutter_launcher_icons: ^0.8.1
Expand Down

0 comments on commit 0cdc19b

Please sign in to comment.