Skip to content

Commit

Permalink
fix api search error and some ui updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ArizArmeidi committed Jun 12, 2023
1 parent 09918c1 commit 03cd5c0
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 120 deletions.
8 changes: 2 additions & 6 deletions lib/Screens/homeScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ class _HomeScreenState extends State<HomeScreen> {
body: SafeArea(
child: Consumer<WeatherProvider>(
builder: (context, weatherProv, _) {
if (weatherProv.isLocationError) {
return LocationError();
}
if (weatherProv.isRequestError) {
return RequestError();
}
if (weatherProv.isRequestError) return RequestError();
if (weatherProv.isLocationError) return LocationError();
return Column(
children: [
SearchBar(),
Expand Down
2 changes: 1 addition & 1 deletion lib/Screens/hourlyWeatherScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class HourlyScreen extends StatelessWidget {
),
),
const SizedBox(width: 16.0),
MapString.mapStringToIcon(context, weather.condition, 25),
MapString.mapStringToIcon(weather.condition, 25),
],
),
),
Expand Down
3 changes: 1 addition & 2 deletions lib/Screens/weeklyWeatherScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ class WeeklyScreen extends StatelessWidget {
),
Padding(
padding: const EdgeInsets.only(left: 15, bottom: 15),
child:
MapString.mapStringToIcon(context, weather.condition, 25),
child: MapString.mapStringToIcon(weather.condition, 25),
),
],
),
Expand Down
12 changes: 2 additions & 10 deletions lib/helper/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ class MapString {
);
}

static Icon mapStringToIcon(
BuildContext context,
String input,
double iconSize,
) {
static Icon mapStringToIcon(String input, double iconSize) {
IconData icon;
switch (input) {
case 'Thunderstorm':
Expand Down Expand Up @@ -130,10 +126,6 @@ class MapString {
default:
icon = MdiIcons.weatherCloudy;
}
return Icon(
icon,
size: iconSize,
color: Theme.of(context).primaryColor,
);
return Icon(icon, size: iconSize, color: Colors.blue);
}
}
34 changes: 17 additions & 17 deletions lib/provider/weatherProvider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import '../models/weather.dart';
class WeatherProvider with ChangeNotifier {
String apiKey = 'Paste Your API Key Here';
LatLng? currentLocation;
late Weather weather;
Weather? weather;
DailyWeather currentWeather = DailyWeather();
List<DailyWeather> hourlyWeather = [];
List<DailyWeather> hourly24Weather = [];
Expand Down Expand Up @@ -53,8 +53,8 @@ class WeatherProvider with ChangeNotifier {
weather = Weather.fromJson(extractedData);
} catch (error) {
print(error);
isLoading = false;
this.isRequestError = true;
throw error;
} finally {
isLoading = false;
notifyListeners();
Expand All @@ -73,36 +73,29 @@ class WeatherProvider with ChangeNotifier {
inspect(response.body);
final dailyData = json.decode(response.body) as Map<String, dynamic>;
currentWeather = DailyWeather.fromJson(dailyData);
List<DailyWeather> tempHourly = [];
List<DailyWeather> temp24Hour = [];
List<DailyWeather> tempSevenDay = [];
List items = dailyData['daily'];
List itemsHourly = dailyData['hourly'];
tempHourly = itemsHourly
hourlyWeather = itemsHourly
.map((item) => DailyWeather.fromHourlyJson(item))
.toList()
.skip(1)
.take(3)
.toList();
temp24Hour = itemsHourly
hourly24Weather = itemsHourly
.map((item) => DailyWeather.fromHourlyJson(item))
.toList()
.skip(1)
.take(24)
.toList();
tempSevenDay = items
sevenDayWeather = items
.map((item) => DailyWeather.fromDailyJson(item))
.toList()
.skip(1)
.take(7)
.toList();
hourlyWeather = tempHourly;
hourly24Weather = temp24Hour;
sevenDayWeather = tempSevenDay;
} catch (error) {
print(error);
this.isRequestError = true;
throw error;
} finally {
isLoading = false;
notifyListeners();
Expand All @@ -118,18 +111,25 @@ class WeatherProvider with ChangeNotifier {
final extractedData = json.decode(response.body) as Map<String, dynamic>;
weather = Weather.fromJson(extractedData);
} catch (error) {
print(error);
this.isRequestError = true;
throw error;
} finally {
isLoading = false;
notifyListeners();
}
}

Future<void> searchWeather({required String location}) async {
Future<void> searchWeather(String location) async {
isLoading = true;
notifyListeners();
isRequestError = false;
isLocationError = false;
double latitude = weather.lat;
double longitude = weather.long;
await searchWeatherWithLocation(location);
await getDailyWeather(LatLng(latitude, longitude));
if (weather == null) {
isRequestError = true;
notifyListeners();
return;
}
await getDailyWeather(LatLng(weather!.lat, weather!.long));
}
}
1 change: 0 additions & 1 deletion lib/widgets/hourlyForecast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class HourlyForecast extends StatelessWidget {
),
),
MapString.mapStringToIcon(
context,
'${weather.condition}',
50,
),
Expand Down
12 changes: 6 additions & 6 deletions lib/widgets/mainWeather.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MainWeather extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.location_on_outlined),
Text('${weatherProv.weather.cityName}', style: _style1),
Text('${weatherProv.weather!.cityName}', style: _style1),
],
),
const SizedBox(height: 5.0),
Expand All @@ -40,13 +40,12 @@ class MainWeather extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: [
MapString.mapStringToIcon(
context,
'${weatherProv.weather.currently}',
'${weatherProv.weather!.currently}',
55,
),
const SizedBox(width: 16.0),
Text(
'${weatherProv.weather.temp.toStringAsFixed(0)}°C',
'${weatherProv.weather!.temp.toStringAsFixed(0)}°C',
style: TextStyle(
fontSize: 55,
fontWeight: FontWeight.w600,
Expand All @@ -56,12 +55,13 @@ class MainWeather extends StatelessWidget {
),
const SizedBox(height: 10.0),
Text(
'${weatherProv.weather.tempMax.toStringAsFixed(0)}°/ ${weatherProv.weather.tempMin.toStringAsFixed(0)}° Feels like ${weatherProv.weather.feelsLike.toStringAsFixed(0)}°',
'${weatherProv.weather!.tempMax.toStringAsFixed(0)}°/ ${weatherProv.weather!.tempMin.toStringAsFixed(0)}° Feels like ${weatherProv.weather!.feelsLike.toStringAsFixed(0)}°',
style: _style1.copyWith(fontSize: 19),
),
const SizedBox(height: 5.0),
Text(
toBeginningOfSentenceCase('${weatherProv.weather.description}') ??
toBeginningOfSentenceCase(
'${weatherProv.weather!.description}') ??
'',
style: _style1.copyWith(fontSize: 19),
),
Expand Down
35 changes: 21 additions & 14 deletions lib/widgets/searchBar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@ class _SearchBarState extends State<SearchBar> {
Widget build(BuildContext context) {
return Consumer<WeatherProvider>(builder: (context, weatherProv, _) {
return Container(
margin: EdgeInsets.symmetric(
vertical: 25,
horizontal: MediaQuery.of(context).size.width * .05,
),
margin: const EdgeInsets.symmetric(vertical: 24.0, horizontal: 16.0),
child: Material(
elevation: 5,
borderRadius: BorderRadius.circular(15),
elevation: 2,
borderRadius: BorderRadius.circular(16.0),
color: Colors.white,
child: TextField(
enabled: !weatherProv.isLoading,
style: TextStyle(color: Colors.black),
maxLines: 1,
controller: _textController,
decoration: InputDecoration(
hintText: 'Search Location',
suffixIcon: _textController.text.isEmpty
? null
: InkWell(
radius: 4.0,
onTap: () {
setState(() {
_textController.clear();
});
},
child: Icon(Icons.close, color: Colors.blue),
),
hintStyle: TextStyle(color: Colors.grey),
errorText: _validate ? null : null,
border: InputBorder.none,
Expand All @@ -49,20 +58,18 @@ class _SearchBarState extends State<SearchBar> {
),
),
contentPadding: EdgeInsets.only(
left: 0,
bottom: 11,
top: 11,
right: 15,
top: _textController.text.isEmpty ? 12.0 : 14.0,
bottom: _textController.text.isEmpty ? 12.0 : 0.0,
),
hintText: "Search Location",
),
onSubmitted: (value) {
onChanged: (value) => setState(() {}),
onSubmitted: (query) {
setState(() {
_textController.text.isEmpty
? _validate = true
: Provider.of<WeatherProvider>(context, listen: false)
.searchWeather(location: value);
: weatherProv.searchWeather(query);
});
FocusScope.of(context).unfocus();
},
),
),
Expand Down

0 comments on commit 03cd5c0

Please sign in to comment.