Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit a2dbd11

Browse files
committed
Display WeatherImage when Reload Button Tapped
1 parent 7510397 commit a2dbd11

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lib/view/weather_view/component/weather_forecast.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_svg/flutter_svg.dart';
23

34
class WeatherForecast extends StatelessWidget {
4-
const WeatherForecast({super.key});
5+
const WeatherForecast({super.key, this.weatherCondition});
6+
7+
final String? weatherCondition;
58

69
@override
710
Widget build(BuildContext context) {
811
final textTheme = Theme.of(context).textTheme;
912

1013
return Column(
1114
children: [
12-
const AspectRatio(
15+
AspectRatio(
1316
aspectRatio: 1,
14-
child: Placeholder(),
17+
child: weatherCondition == null
18+
? const Placeholder()
19+
: SvgPicture.asset(
20+
'images/$weatherCondition.svg',
21+
semanticsLabel: '$weatherCondition image',
22+
),
1523
),
1624
Padding(
1725
padding: const EdgeInsets.all(16),

lib/view/weather_view/weather_page.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_training/model/weather.dart';
23
import 'package:flutter_training/view/weather_view/component/weather_forecast.dart';
34

4-
class WeatherPage extends StatelessWidget {
5+
class WeatherPage extends StatefulWidget {
56
const WeatherPage({super.key});
67

8+
@override
9+
State<WeatherPage> createState() => _WeatherPageState();
10+
}
11+
12+
class _WeatherPageState extends State<WeatherPage> {
13+
final _weather = Weather();
14+
String? _weatherCondition;
15+
716
@override
817
Widget build(BuildContext context) {
918
return Scaffold(
@@ -13,7 +22,7 @@ class WeatherPage extends StatelessWidget {
1322
child: Column(
1423
children: [
1524
const Spacer(),
16-
const WeatherForecast(),
25+
WeatherForecast(weatherCondition: _weatherCondition),
1726
Flexible(
1827
child: Column(
1928
children: [
@@ -31,7 +40,11 @@ class WeatherPage extends StatelessWidget {
3140
Expanded(
3241
child: TextButton(
3342
child: const Text('Reload'),
34-
onPressed: () {},
43+
onPressed: () {
44+
setState(() {
45+
_weatherCondition = _weather.fetchWeather();
46+
});
47+
},
3548
),
3649
),
3750
],

0 commit comments

Comments
 (0)