11import 'package:flutter/material.dart' ;
2+ import 'package:flutter_weather/main.dart' ;
23import 'package:flutter_weather/src/api/weather_api_client.dart' ;
34import 'package:flutter_weather/src/bloc/weather_bloc.dart' ;
45import 'package:flutter_weather/src/bloc/weather_event.dart' ;
56import 'package:flutter_weather/src/bloc/weather_state.dart' ;
67import 'package:flutter_weather/src/repository/weather_repository.dart' ;
78import 'package:flutter_weather/src/api/api_keys.dart' ;
89import 'package:flutter_bloc/flutter_bloc.dart' ;
10+ import 'package:flutter_weather/src/themes.dart' ;
911import 'package:flutter_weather/src/widgets/weather_widget.dart' ;
1012import 'package:http/http.dart' as http;
1113import 'package:intl/intl.dart' ;
1214
15+ enum OptionsMenu { changeCity, nightMode, lightMode }
16+
1317class WeatherScreen extends StatefulWidget {
1418 final WeatherRepository weatherRepository = WeatherRepository (
1519 weatherApiClient: WeatherApiClient (
@@ -18,62 +22,73 @@ class WeatherScreen extends StatefulWidget {
1822 _WeatherScreenState createState () => _WeatherScreenState ();
1923}
2024
21- class _WeatherScreenState extends State <WeatherScreen > with TickerProviderStateMixin {
25+ class _WeatherScreenState extends State <WeatherScreen >
26+ with TickerProviderStateMixin {
2227 WeatherBloc _weatherBloc;
2328 String _cityName = 'bengaluru' ;
2429 AnimationController _fadeController;
2530 Animation <double > _fadeAnimation;
2631
27-
2832 @override
2933 void initState () {
3034 super .initState ();
3135 _weatherBloc = WeatherBloc (weatherRepository: widget.weatherRepository);
3236 _weatherBloc.dispatch (FetchWeather (cityName: _cityName));
3337 _fadeController = AnimationController (
3438 duration: const Duration (milliseconds: 1000 ), vsync: this );
35- _fadeAnimation = CurvedAnimation (parent: _fadeController, curve: Curves .easeIn);
39+ _fadeAnimation =
40+ CurvedAnimation (parent: _fadeController, curve: Curves .easeIn);
3641 }
3742
3843 @override
3944 Widget build (BuildContext context) {
4045 return Scaffold (
4146 appBar: AppBar (
42- backgroundColor: Theme .of (context).primaryColor,
47+ backgroundColor: AppStateContainer .of (context).theme .primaryColor,
4348 elevation: 0 ,
4449 title: Column (
4550 mainAxisAlignment: MainAxisAlignment .center,
4651 children: < Widget > [
4752 Text (
4853 DateFormat ('EEEE, MMMM yyyy' ).format (DateTime .now ()),
4954 style: TextStyle (
50- color: Theme .of (context).accentColor.withAlpha (80 ),
55+ color: AppStateContainer .of (context)
56+ .theme
57+ .accentColor
58+ .withAlpha (80 ),
5159 fontSize: 14 ),
5260 )
5361 ],
5462 ),
5563 actions: < Widget > [
56- GestureDetector (
57-
58- child: Padding (
59- padding: EdgeInsets .all (20 ),
64+ PopupMenuButton <OptionsMenu >(
6065 child: Icon (
61- Icons .public ,
62- color: Theme .of (context).accentColor,
66+ Icons .more_vert ,
67+ color: AppStateContainer .of (context).theme .accentColor,
6368 ),
64- ),
65- onTap: () {
66- this .showCityChangeDialog ();
67- },
68- )
69+ onSelected: this ._onOptionMenuItemSelected,
70+ itemBuilder: (context) => < PopupMenuEntry <OptionsMenu >> [
71+ PopupMenuItem <OptionsMenu >(
72+ value: OptionsMenu .changeCity,
73+ child: Text ("change city" ),
74+ ),
75+ PopupMenuItem <OptionsMenu >(
76+ value: OptionsMenu .nightMode,
77+ child: Text ("night mode" ),
78+ ),
79+ PopupMenuItem <OptionsMenu >(
80+ value: OptionsMenu .lightMode,
81+ child: Text ("light mode" ),
82+ ),
83+ ])
6984 ],
7085 ),
7186 backgroundColor: Colors .white,
7287 body: Material (
73- color: Theme .of (context).accentColor,
7488 child: Container (
7589 constraints: BoxConstraints .expand (),
76- decoration: BoxDecoration (color: Theme .of (context).primaryColor),
90+ decoration: BoxDecoration (
91+ color: AppStateContainer .of (context).theme.primaryColor),
7792 child: FadeTransition (
7893 opacity: _fadeAnimation,
7994 child: BlocBuilder (
@@ -87,10 +102,12 @@ class _WeatherScreenState extends State<WeatherScreen> with TickerProviderStateM
87102 );
88103 } else if (weatherState is WeatherError ||
89104 weatherState is WeatherEmpty ) {
90- String errorText = 'There was an error fetching weather data' ;
91- if (weatherState is WeatherError ){
92- if (weatherState.errorCode == 404 ){
93- errorText = 'We have trouble fetching weather for $_cityName ' ;
105+ String errorText =
106+ 'There was an error fetching weather data' ;
107+ if (weatherState is WeatherError ) {
108+ if (weatherState.errorCode == 404 ) {
109+ errorText =
110+ 'We have trouble fetching weather for $_cityName ' ;
94111 }
95112 }
96113 return Column (
@@ -106,14 +123,18 @@ class _WeatherScreenState extends State<WeatherScreen> with TickerProviderStateM
106123 ),
107124 Text (
108125 errorText,
109- style:
110- TextStyle (color: Theme .of (context).accentColor),
126+ style: TextStyle (
127+ color: AppStateContainer .of (context)
128+ .theme
129+ .accentColor),
111130 ),
112131 FlatButton (
113132 child: Text (
114133 "Try Again" ,
115- style:
116- TextStyle (color: Theme .of (context).accentColor),
134+ style: TextStyle (
135+ color: AppStateContainer .of (context)
136+ .theme
137+ .accentColor),
117138 ),
118139 onPressed: _fetchWeather,
119140 )
@@ -122,7 +143,8 @@ class _WeatherScreenState extends State<WeatherScreen> with TickerProviderStateM
122143 } else if (weatherState is WeatherLoading ) {
123144 return Center (
124145 child: CircularProgressIndicator (
125- backgroundColor: Theme .of (context).primaryColor,
146+ backgroundColor:
147+ AppStateContainer .of (context).theme.primaryColor,
126148 ),
127149 );
128150 }
@@ -132,18 +154,19 @@ class _WeatherScreenState extends State<WeatherScreen> with TickerProviderStateM
132154 ));
133155 }
134156
135- void showCityChangeDialog () {
157+ void _showCityChangeDialog () {
136158 showDialog (
137159 context: context,
138160 barrierDismissible: true ,
139161 builder: (BuildContext context) {
140162 return AlertDialog (
141- title: Text ('Change city' ),
163+ backgroundColor: Colors .white,
164+ title: Text ('Change city' , style: TextStyle (color: Colors .black)),
142165 actions: < Widget > [
143166 FlatButton (
144167 child: Text (
145168 'ok' ,
146- style: TextStyle (color: Theme . of (context).primaryColor ),
169+ style: TextStyle (color: Colors .black ),
147170 ),
148171 onPressed: () {
149172 _fetchWeather ();
@@ -158,12 +181,28 @@ class _WeatherScreenState extends State<WeatherScreen> with TickerProviderStateM
158181 },
159182 decoration: InputDecoration (
160183 hintText: 'Enter the name of your city' ,
184+ hintStyle: TextStyle (color: Colors .black),
161185 ),
186+ style: TextStyle (color: Colors .black),
162187 ),
163188 );
164189 });
165190 }
166191
192+ _onOptionMenuItemSelected (OptionsMenu item) {
193+ switch (item) {
194+ case OptionsMenu .changeCity:
195+ this ._showCityChangeDialog ();
196+ break ;
197+ case OptionsMenu .nightMode:
198+ AppStateContainer .of (context).updateTheme (Themes .DARK_THEME_CODE );
199+ break ;
200+ case OptionsMenu .lightMode:
201+ AppStateContainer .of (context).updateTheme (Themes .LIGHT_THEME_CODE );
202+ break ;
203+ }
204+ }
205+
167206 _fetchWeather () {
168207 _weatherBloc.dispatch (FetchWeather (cityName: _cityName));
169208 }
0 commit comments