-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.dart
134 lines (114 loc) · 3.94 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_weather/src/api/api_keys.dart';
import 'package:flutter_weather/src/bloc/weather_bloc_observre.dart';
import 'package:flutter_weather/src/screens/routes.dart';
import 'package:flutter_weather/src/screens/weather_screen.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_weather/src/themes.dart';
import 'package:flutter_weather/src/utils/constants.dart';
import 'package:flutter_weather/src/utils/converters.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'src/api/weather_api_client.dart';
import 'src/bloc/weather_bloc.dart';
import 'src/repository/weather_repository.dart';
void main() {
Bloc.observer = SimpleBlocObserver();
final WeatherRepository weatherRepository = WeatherRepository(
weatherApiClient: WeatherApiClient(
httpClient: http.Client(),
apiKey: ApiKey.OPEN_WEATHER_MAP,
),
);
runApp(AppStateContainer(
child: WeatherApp(weatherRepository: weatherRepository),
));
}
class WeatherApp extends StatelessWidget {
final WeatherRepository weatherRepository;
WeatherApp({Key key, @required this.weatherRepository})
: assert(weatherRepository != null),
super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Weather App',
theme: AppStateContainer.of(context).theme,
home: BlocProvider(
create: (context) => WeatherBloc(weatherRepository: weatherRepository),
child: WeatherScreen(),
),
routes: Routes.mainRoute,
);
}
}
/// top level widget to hold application state
/// state is passed down with an inherited widget
/// inherited widget state is mainly used to hold app theme and temerature unit
class AppStateContainer extends StatefulWidget {
final Widget child;
AppStateContainer({@required this.child});
@override
_AppStateContainerState createState() => _AppStateContainerState();
static _AppStateContainerState of(BuildContext context) {
var widget =
context.dependOnInheritedWidgetOfExactType<_InheritedStateContainer>();
return widget.data;
}
}
class _AppStateContainerState extends State<AppStateContainer> {
ThemeData _theme = Themes.getTheme(Themes.DARK_THEME_CODE);
int themeCode = Themes.DARK_THEME_CODE;
TemperatureUnit temperatureUnit = TemperatureUnit.celsius;
@override
initState() {
super.initState();
SharedPreferences.getInstance().then((sharedPref) {
setState(() {
themeCode = sharedPref.getInt(CONSTANTS.SHARED_PREF_KEY_THEME) ??
Themes.DARK_THEME_CODE;
temperatureUnit = TemperatureUnit.values[
sharedPref.getInt(CONSTANTS.SHARED_PREF_KEY_TEMPERATURE_UNIT) ??
TemperatureUnit.celsius.index];
this._theme = Themes.getTheme(themeCode);
});
});
}
@override
Widget build(BuildContext context) {
print(theme.accentColor);
return _InheritedStateContainer(
data: this,
child: widget.child,
);
}
ThemeData get theme => _theme;
updateTheme(int themeCode) {
setState(() {
_theme = Themes.getTheme(themeCode);
this.themeCode = themeCode;
});
SharedPreferences.getInstance().then((sharedPref) {
sharedPref.setInt(CONSTANTS.SHARED_PREF_KEY_THEME, themeCode);
});
}
updateTemperatureUnit(TemperatureUnit unit) {
setState(() {
this.temperatureUnit = unit;
});
SharedPreferences.getInstance().then((sharedPref) {
sharedPref.setInt(CONSTANTS.SHARED_PREF_KEY_TEMPERATURE_UNIT, unit.index);
});
}
}
class _InheritedStateContainer extends InheritedWidget {
final _AppStateContainerState data;
const _InheritedStateContainer({
Key key,
@required this.data,
@required Widget child,
}) : super(key: key, child: child);
@override
bool updateShouldNotify(_InheritedStateContainer oldWidget) => true;
}