Skip to content

Commit

Permalink
Use pure bloc
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonmendess committed Apr 25, 2019
1 parent 0fe9db0 commit fea6622
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 69 deletions.
21 changes: 15 additions & 6 deletions lib/blocs/weather-bloc.dart
@@ -1,10 +1,15 @@
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:weather/models/location.dart';
import 'package:weather/models/weather.dart';
import 'package:weather/models/search.dart';
import 'dart:async';

class WeatherBloc implements BlocBase {
class WeatherBloc {

static final WeatherBloc _bloc = new WeatherBloc._internal();
factory WeatherBloc(){
return _bloc;
}
WeatherBloc._internal();

Search search = Search();
Location location = Location();
Expand All @@ -20,13 +25,17 @@ class WeatherBloc implements BlocBase {

void main() async {
await location.getStoredLocation();
await weatherStart(location);
}

Future<void> weatherStart(location) async {
await weather.fetchForecast(location);
dayOrNight(weather);
_weatherController.add(weather);
dayOrNight();
}

void dayOrNight(Weather w) {
_weatherDNController.add(w.dyNght);
void dayOrNight() {
_weatherDNController.add(weather.dyNght);
}

void input(words) async {
Expand All @@ -37,9 +46,9 @@ class WeatherBloc implements BlocBase {
setLocation(Map newLocation) async {
location.fromMap(newLocation);
location.setStoredLocation();
await weatherStart(location);
}

@override
void dispose() {
_weatherController.close();
_weatherDNController.close();
Expand Down
31 changes: 13 additions & 18 deletions lib/main.dart
@@ -1,5 +1,3 @@
import 'package:weather/blocs/weather-bloc.dart';
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:weather/screens/home.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand All @@ -12,22 +10,19 @@ class App extends StatelessWidget {
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(statusBarColor: Colors.transparent));

return BlocProvider<WeatherBloc>(
child: MaterialApp(
title: "Weather",
home: Home(),
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: 'GoogleSans',
primaryColor: Colors.white,
hintColor: Colors.white,
accentColor: Colors.white,
cardColor: Color.fromARGB(230, 245, 245, 245),
textTheme: TextTheme(
title: TextStyle(fontSize: 25, color: Colors.grey[800]),
)),
),
bloc: WeatherBloc(),
return MaterialApp(
title: "Weather",
home: Home(),
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: 'GoogleSans',
primaryColor: Colors.white,
hintColor: Colors.white,
accentColor: Colors.white,
cardColor: Color.fromARGB(230, 245, 245, 245),
textTheme: TextTheme(
title: TextStyle(fontSize: 25, color: Colors.grey[800]),
)),
);
}
}
39 changes: 27 additions & 12 deletions lib/screens/home.dart
@@ -1,27 +1,40 @@
import 'package:weather/widgets/backgroundContainer.dart';
import 'package:weather/widgets/weatherCard.dart';
import 'package:weather/blocs/weather-bloc.dart';
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:weather/screens/search.dart';
import 'package:flutter/material.dart';

class Home extends StatelessWidget {
class Home extends StatefulWidget {
@override
Widget build(BuildContext context) {
TimeOfDay now = TimeOfDay.now();
_HomeState createState() => _HomeState();
}

final WeatherBloc weatherBloc = BlocProvider.of<WeatherBloc>(context);
class _HomeState extends State<Home> {
WeatherBloc bloc;

weatherBloc.main();
@override
void initState() {
super.initState();
bloc = WeatherBloc();
bloc.main();
}

@override
void dispose() {
bloc?.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Stack(children: <Widget>[
StreamBuilder(
stream: weatherBloc.getDayOrNight,
stream: bloc.getDayOrNight,
builder: (context, snapshot) {
if (snapshot.hasData) {
return BackgroudContainer(snapshot.data);
} else {
return BackgroudContainer(now.period.index == 0 ? 'D' : "N");
return BackgroudContainer("D");
}
},
),
Expand All @@ -34,7 +47,7 @@ class Home extends StatelessWidget {
leading: IconButton(
icon: Icon(Icons.refresh, color: Theme.of(context).accentColor),
onPressed: () {
weatherBloc.main();
bloc.main();
},
),
flexibleSpace: FlexibleSpaceBar(
Expand All @@ -46,15 +59,17 @@ class Home extends StatelessWidget {
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search, color: Theme.of(context).accentColor),
icon:
Icon(Icons.search, color: Theme.of(context).accentColor),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => Search()));
Navigator.push(context,
MaterialPageRoute(builder: (context) => Search()));
}),
],
),
SliverToBoxAdapter(
child: StreamBuilder(
stream: weatherBloc.getWeather,
stream: bloc.getWeather,
builder: (context, snapshot) {
if (snapshot.hasData) {
return WeatherCard(snapshot.data);
Expand Down
30 changes: 13 additions & 17 deletions lib/screens/search.dart
@@ -1,6 +1,5 @@
import 'package:weather/widgets/backgroundContainer.dart';
import 'package:weather/blocs/weather-bloc.dart';
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:flutter/material.dart';
import 'dart:convert';

Expand All @@ -10,24 +9,20 @@ class Search extends StatefulWidget {
}

class _SearchState extends State<Search> {
List locations = [];
TimeOfDay now = TimeOfDay.now();

WeatherBloc bloc;

@override
void initState() {
super.initState();
bloc = WeatherBloc();
}

@override
Widget build(BuildContext context) {
final WeatherBloc weatherBloc = BlocProvider.of<WeatherBloc>(context);

return Stack(children: <Widget>[
StreamBuilder(
stream: weatherBloc.getDayOrNight,
builder: (context, snapshot) {
if (snapshot.hasData) {
return BackgroudContainer(snapshot.data);
} else {
return BackgroudContainer(now.period.index == 0 ? 'D' : "N");
}
},
),
BackgroudContainer("D"),
CustomScrollView(
slivers: <Widget>[
SliverAppBar(
Expand Down Expand Up @@ -62,13 +57,14 @@ class _SearchState extends State<Search> {
hintStyle: TextStyle(color: Colors.grey[800])),
style: TextStyle(color: Colors.black, fontSize: 19),
textAlign: TextAlign.center,
autofocus: true,
onSubmitted: (e) {
weatherBloc.input(e);
bloc.input(e);
},
),
),
StreamBuilder(
stream: weatherBloc.getLocations,
stream: bloc.getLocations,
builder: (context, snapshot) {
if (snapshot.hasData) {
List<dynamic> locations = json.decode(snapshot.data);
Expand All @@ -84,7 +80,7 @@ class _SearchState extends State<Search> {
height: 65,
child: InkWell(
onTap: () {
weatherBloc.setLocation(locations[index]);
bloc.setLocation(locations[index]);
Navigator.pop(context);
},
child: ListTile(
Expand Down
14 changes: 0 additions & 14 deletions pubspec.lock
Expand Up @@ -8,13 +8,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
bloc_pattern:
dependency: "direct main"
description:
name: bloc_pattern
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -102,13 +95,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
rxdart:
dependency: "direct main"
description:
name: rxdart
url: "https://pub.dartlang.org"
source: hosted
version: "0.21.0"
shared_preferences:
dependency: "direct main"
description:
Expand Down
2 changes: 0 additions & 2 deletions pubspec.yaml
Expand Up @@ -23,9 +23,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
rxdart: ^0.21.0
http: ^0.12.0+2
bloc_pattern: ^1.5.2
shared_preferences: ^0.5.2

dev_dependencies:
Expand Down

0 comments on commit fea6622

Please sign in to comment.