Created
February 11, 2019 06:01
-
-
Save felangel/bed1d113dc3c721394dbc58eee4cd494 to your computer and use it in GitHub Desktop.
[flutter_weather] Weather Widget (gradient container)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:flutter_weather/widgets/widgets.dart'; | |
import 'package:flutter_weather/repositories/repositories.dart'; | |
import 'package:flutter_weather/blocs/blocs.dart'; | |
class Weather extends StatefulWidget { | |
final WeatherRepository weatherRepository; | |
Weather({Key key, @required this.weatherRepository}) | |
: assert(weatherRepository != null), | |
super(key: key); | |
@override | |
State<Weather> createState() => _WeatherState(); | |
} | |
class _WeatherState extends State<Weather> { | |
WeatherBloc _weatherBloc; | |
Completer<void> _refreshCompleter; | |
@override | |
void initState() { | |
super.initState(); | |
_refreshCompleter = Completer<void>(); | |
_weatherBloc = WeatherBloc(weatherRepository: widget.weatherRepository); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Flutter Weather'), | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon(Icons.settings), | |
onPressed: () async { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => Settings(), | |
), | |
); | |
}, | |
), | |
IconButton( | |
icon: Icon(Icons.search), | |
onPressed: () async { | |
final city = await Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => CitySelection(), | |
), | |
); | |
if (city != null) { | |
_weatherBloc.dispatch(FetchWeather(city: city)); | |
} | |
}, | |
) | |
], | |
), | |
body: Center( | |
child: BlocBuilder( | |
bloc: _weatherBloc, | |
builder: (_, WeatherState state) { | |
if (state is WeatherEmpty) { | |
return Center(child: Text('Please Select a Location')); | |
} | |
if (state is WeatherLoading) { | |
return Center(child: CircularProgressIndicator()); | |
} | |
if (state is WeatherLoaded) { | |
final weather = state.weather; | |
final themeBloc = BlocProvider.of<ThemeBloc>(context); | |
themeBloc.dispatch(WeatherChanged(condition: weather.condition)); | |
_refreshCompleter?.complete(); | |
_refreshCompleter = Completer(); | |
return BlocBuilder( | |
bloc: themeBloc, | |
builder: (_, ThemeState themeState) { | |
return GradientContainer( | |
color: themeState.color, | |
child: RefreshIndicator( | |
onRefresh: () { | |
_weatherBloc.dispatch( | |
RefreshWeather(city: state.weather.location), | |
); | |
return _refreshCompleter.future; | |
}, | |
child: ListView( | |
children: <Widget>[ | |
Padding( | |
padding: EdgeInsets.only(top: 100.0), | |
child: Center( | |
child: Location(location: weather.location), | |
), | |
), | |
Center( | |
child: LastUpdated(dateTime: weather.lastUpdated), | |
), | |
Padding( | |
padding: EdgeInsets.symmetric(vertical: 50.0), | |
child: Center( | |
child: CombinedWeatherTemperature( | |
weather: weather, | |
), | |
), | |
), | |
], | |
), | |
), | |
); | |
}, | |
); | |
} | |
if (state is WeatherError) { | |
return Text( | |
'Something went wrong!', | |
style: TextStyle(color: Colors.red), | |
); | |
} | |
}, | |
), | |
), | |
); | |
} | |
@override | |
void dispose() { | |
_weatherBloc.dispose(); | |
super.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment