Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #20 from warahiko/feature/fetch_with_json
Browse files Browse the repository at this point in the history
詳細な予報結果の表示
  • Loading branch information
warahiko authored Nov 27, 2023
2 parents 5b92750 + 358f5ed commit 68ea87d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
28 changes: 17 additions & 11 deletions lib/forecast.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
enum Forecast {
sunny(image: 'assets/sunny.svg'),
cloudy(image: 'assets/cloudy.svg'),
rainy(image: 'assets/rainy.svg'),
;
import 'dart:convert';

import 'package:flutter_training/weather.dart';

class Forecast {
const Forecast({
required this.image,
required this.weather,
required this.maxTemperature,
required this.minTemperature,
});

factory Forecast.from(String name) {
return Forecast.values.singleWhere(
(element) => element.name == name,
orElse: () => throw Exception('No enum value for `$name`.'),
factory Forecast.from(String jsonString) {
final decoded = jsonDecode(jsonString) as Map<String, dynamic>;

return Forecast(
weather: Weather.from(decoded['weather_condition'] as String),
minTemperature: decoded['min_temperature'] as int,
maxTemperature: decoded['max_temperature'] as int,
);
}

final String image;
final Weather weather;
final int maxTemperature;
final int minTemperature;
}
12 changes: 9 additions & 3 deletions lib/forecast_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class ForecastView extends StatelessWidget {

final Forecast? _forecast;

String _toTemperatureString(int? temperature) {
final temperatureValue =
temperature == null ? '**' : temperature.toString();
return '$temperatureValue ℃';
}

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
Expand All @@ -21,14 +27,14 @@ class ForecastView extends StatelessWidget {
aspectRatio: 1,
child: _forecast == null
? const Placeholder()
: SvgPicture.asset(_forecast.image),
: SvgPicture.asset(_forecast.weather.image),
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: Text(
'** ℃',
_toTemperatureString(_forecast?.minTemperature),
style: labelLarge.copyWith(
color: Colors.blue,
),
Expand All @@ -37,7 +43,7 @@ class ForecastView extends StatelessWidget {
),
Expanded(
child: Text(
'** ℃',
_toTemperatureString(_forecast?.maxTemperature),
style: labelLarge.copyWith(
color: Colors.red,
),
Expand Down
9 changes: 8 additions & 1 deletion lib/main_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_training/forecast.dart';
Expand All @@ -18,9 +19,15 @@ class _MainScreenState extends State<MainScreen> {
Forecast? _forecast;

void _fetchForecast() {
const request = {
'area': 'tokyo',
'date': '2023-11-22T00:00:00+09:00',
};
final requestString = jsonEncode(request);

final Forecast newForecast;
try {
newForecast = Forecast.from(_yumemiWeather.fetchThrowsWeather('tokyo'));
newForecast = Forecast.from(_yumemiWeather.fetchWeather(requestString));
} on YumemiWeatherError catch (e) {
unawaited(_showErrorDialog(e.toMessage()));
return;
Expand Down
19 changes: 19 additions & 0 deletions lib/weather.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum Weather {
sunny(image: 'assets/sunny.svg'),
cloudy(image: 'assets/cloudy.svg'),
rainy(image: 'assets/rainy.svg'),
;

const Weather({
required this.image,
});

factory Weather.from(String name) {
return Weather.values.singleWhere(
(element) => element.name == name,
orElse: () => throw Exception('No enum value for `$name`.'),
);
}

final String image;
}

0 comments on commit 68ea87d

Please sign in to comment.