/
weather_page.dart
73 lines (71 loc) · 2.31 KB
/
weather_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_weather/search/search.dart';
import 'package:flutter_weather/settings/settings.dart';
import 'package:flutter_weather/theme/theme.dart';
import 'package:flutter_weather/weather/weather.dart';
import 'package:weather_repository/weather_repository.dart';
class WeatherPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => WeatherCubit(context.read<WeatherRepository>()),
child: WeatherView(),
);
}
}
class WeatherView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Weather'),
actions: [
IconButton(
icon: const Icon(Icons.settings),
onPressed: () {
Navigator.of(context).push<void>(SettingsPage.route(
context.read<WeatherCubit>(),
));
},
),
],
),
body: Center(
child: BlocConsumer<WeatherCubit, WeatherState>(
listener: (context, state) {
if (state.status.isSuccess) {
context.read<ThemeCubit>().updateTheme(state.weather);
}
},
builder: (context, state) {
switch (state.status) {
case WeatherStatus.initial:
return const WeatherEmpty();
case WeatherStatus.loading:
return const WeatherLoading();
case WeatherStatus.success:
return WeatherPopulated(
weather: state.weather,
units: state.temperatureUnits,
onRefresh: () {
return context.read<WeatherCubit>().refreshWeather();
},
);
case WeatherStatus.failure:
default:
return const WeatherError();
}
},
),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.search),
onPressed: () async {
final city = await Navigator.of(context).push(SearchPage.route());
await context.read<WeatherCubit>().fetchWeather(city);
},
),
);
}
}