SHARE
TWEET

Untitled

a guest Mar 17th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:connectivity/connectivity.dart';
  4. import 'package:weather_bloc/src/blocs/conditions_bloc.dart';
  5. import 'package:weather_bloc/src/models/condition_model.dart';
  6. import 'package:weather_bloc/src/ui/weather_conditions.dart';
  7. import 'package:weather_bloc/src/ui/weather_forecast.dart';
  8.  
  9. class WeatherApp extends StatefulWidget {
  10.   @override
  11.   _WeatherAppState createState() => _WeatherAppState();
  12. }
  13.  
  14. class _WeatherAppState extends State<WeatherApp> with WidgetsBindingObserver{
  15.  
  16.   Connectivity connectivity;
  17.   StreamSubscription<ConnectivityResult> subscription;
  18.  
  19.   @override
  20.   void initState() {
  21.     super.initState();
  22.     WidgetsBinding.instance.addObserver(this);
  23.     connectivity = Connectivity();
  24.     subscription = connectivity.onConnectivityChanged
  25.         .listen((ConnectivityResult result){
  26.       if(result != null) {
  27.         blocConditions.fetchConditions();
  28.       }
  29.     });
  30.   }
  31.  
  32.   @override
  33.   void dispose() {
  34.     WidgetsBinding.instance.removeObserver(this);
  35.     subscription.cancel();
  36.     super.dispose();
  37.   }
  38.  
  39.   @override
  40.   void didChangeAppLifecycleState(AppLifecycleState state) {
  41.     switch (state) {
  42.       case AppLifecycleState.paused:
  43.         print('paused');
  44.         break;
  45.       case AppLifecycleState.resumed:
  46.         print('resumed');
  47.         blocConditions.fetchConditions();
  48.         break;
  49.       case AppLifecycleState.inactive:
  50.         print('inactive');
  51.         break;
  52.       case AppLifecycleState.suspending:
  53.         print('suspending');
  54.         break;
  55.     }
  56.   }
  57.  
  58.   @override
  59.   Widget build(BuildContext context) {
  60.     return Container(
  61.       child: StreamBuilder(
  62.         stream: blocConditions.conditions,
  63.         builder: (context, AsyncSnapshot<ConditionsModel> snapshot){
  64.           if(snapshot.hasError)
  65.             return Center(child: Text('Hubo un error al recibir los datos'),);
  66.           switch (snapshot.connectionState){
  67.             case ConnectionState.waiting:
  68.               return Center(
  69.                   child: Column(
  70.                     mainAxisAlignment: MainAxisAlignment.center,
  71.                     crossAxisAlignment: CrossAxisAlignment.center,
  72.                     children: <Widget>[
  73.                       Text('Sin conexión a Internet'),
  74.                       FlatButton.icon(
  75.                         icon: Icon(Icons.refresh),
  76.                         label: Text(''),
  77.                         onPressed: fetchData,
  78.                       )
  79.                     ],
  80.                   )
  81.               );
  82.             case ConnectionState.active:
  83.               return Container(
  84.                 color: snapshot.data.current.isDay == 1 ? Colors.white70 : Colors.grey[700],
  85.                 child: ListView(
  86.                   children: <Widget>[
  87.                     WeatherConditions(data:snapshot.data),
  88.                     WeatherForecast(data: snapshot.data)
  89.                   ],
  90.                 ),
  91.               );
  92.             case ConnectionState.done:
  93.               return Text('Done');
  94.             case ConnectionState.none:
  95.               return Text('NONE');
  96.           }
  97.           return Center(child: CircularProgressIndicator());
  98.         },
  99.       ),
  100.     );
  101.   }
  102.  
  103.   fetchData() {
  104.     blocConditions.fetchConditions();
  105.   }
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top