Created
July 12, 2021 14:13
-
-
Save a-k-14/551da96d7a3c73fe056c5251a3bd9d77 to your computer and use it in GitHub Desktop.
This file contains hidden or 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:clima/utilities/constants.dart'; | |
| import 'package:clima/services/weather.dart'; | |
| class LocationScreen extends StatefulWidget { | |
| // final test = 'ABC'; | |
| final locationWeather; // to get the weather data from loading screen | |
| LocationScreen({this.locationWeather}); | |
| @override | |
| _LocationScreenState createState() => _LocationScreenState(); | |
| } | |
| class _LocationScreenState extends State<LocationScreen> { | |
| // final s; | |
| // _LocationScreenState(this.s); | |
| int temperature = 10; | |
| // int condition; | |
| String weatherIcon; | |
| String city; | |
| String message; | |
| WeatherModel weatherModel = WeatherModel(); // to get icon and message | |
| @override | |
| void initState() { | |
| super.initState(); | |
| // print('Location Screen output: ${widget.locationWeather}'); | |
| // print(widget.runtimeType); | |
| updateUI(widget.locationWeather); | |
| print('Location Screen init called'); | |
| } | |
| @override | |
| void deactivate() { | |
| super.deactivate(); | |
| print('Location screen deactivated'); | |
| } | |
| // we can use dynamic or var | |
| void updateUI(var weatherData) { | |
| setState(() { | |
| // To handle cases where there is error in getting location | |
| if (weatherData == null) { | |
| temperature = 0; | |
| message = 'Error in getting location'; | |
| weatherIcon = 'š'; | |
| city = ''; | |
| // return; | |
| } else { | |
| temperature = weatherData['main']['temp'] | |
| .toInt(); // toInt() just of UI purpose to avoid decimals | |
| var condition = weatherData['weather'][0]['id']; | |
| weatherIcon = weatherModel.getWeatherIcon(condition); | |
| city = weatherData['name']; | |
| message = weatherModel.getMessage(temperature); | |
| print('Location Screen output: $temperature $condition $city'); | |
| } | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| // print(s); | |
| print('Location screen build called'); | |
| updateUI(widget.locationWeather); | |
| return Scaffold( | |
| body: Container( | |
| decoration: BoxDecoration( | |
| image: DecorationImage( | |
| image: AssetImage('images/location_background.jpg'), | |
| fit: BoxFit.cover, | |
| colorFilter: ColorFilter.mode( | |
| Colors.white.withOpacity(0.8), BlendMode.dstATop), | |
| ), | |
| ), | |
| constraints: BoxConstraints.expand(), | |
| child: SafeArea( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
| crossAxisAlignment: CrossAxisAlignment.stretch, | |
| children: <Widget>[ | |
| Row( | |
| // default width is full width of the screen. only height is that of tallest child. | |
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
| children: [ | |
| TextButton( | |
| onPressed: () async { | |
| var weatherData = await weatherModel.getLocationWeather(); | |
| updateUI(weatherData); | |
| }, | |
| child: Icon( | |
| Icons.near_me, | |
| size: 50.0, | |
| // color: Colors.white, | |
| ), | |
| style: TextButton.styleFrom(primary: Colors.white), | |
| ), | |
| TextButton( | |
| onPressed: () async { | |
| // To get and store the city name entered in city screen | |
| var typedName = | |
| await Navigator.pushNamed(context, 'CityScreen'); | |
| // print(typedName); | |
| var weatherData = | |
| await weatherModel.getCityWeather(typedName); | |
| updateUI(weatherData); | |
| }, | |
| child: Icon( | |
| Icons.location_city, | |
| size: 50.0, | |
| ), | |
| style: TextButton.styleFrom(primary: Colors.white), | |
| ), | |
| ], | |
| ), | |
| Padding( | |
| padding: EdgeInsets.only(left: 15.0), | |
| child: Row( | |
| children: <Widget>[ | |
| Text( | |
| '$temperature°', | |
| style: kTempTextStyle, | |
| ), | |
| Text( | |
| weatherIcon, | |
| style: kConditionTextStyle, | |
| ), | |
| ], | |
| ), | |
| ), | |
| Padding( | |
| padding: EdgeInsets.only(right: 15.0), | |
| child: Text( | |
| "$message in $city", | |
| textAlign: TextAlign.right, | |
| style: kMessageTextStyle, | |
| ), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment