0

I have made a streaming provider with a network connectivity library that tell me if the user is offline or online. According to this then I show an online widget to show or an offline widget to show. I am just frustrated to use it on every screen. I want to use it once it will behave like this as any time app goes offline it will overlay the offline widget on the present screen where the user is present.

How can I achieve this?

Here is my Stream Provider.

import 'dart:async';
import 'dart:io';
import 'package:catcher/catcher.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:data_connection_checker/data_connection_checker.dart';
import 'package:edge_alerts/edge_alerts.dart';
import 'package:flutter/material.dart';

enum NetworkStatus { online, offline }

class NetworkStatusService {
  //From which the data is connectec WIFI or Mobile.
  final StreamController<ConnectivityResult> _networkStatusController =
      StreamController<ConnectivityResult>.broadcast();
  Connectivity connectivityoftheNetwork = Connectivity();
//Checking if the User is Online or Offline
  final StreamController<NetworkStatus> _dataConnectionController =
      StreamController<NetworkStatus>.broadcast();
  DataConnectionChecker connectivityoftheDataConnection =
      DataConnectionChecker();
  final List<AddressCheckOptions> _dEFAULTADDRESSES =
      List<AddressCheckOptions>.unmodifiable([
    AddressCheckOptions(
      InternetAddress('2001:4860:4860::8888',
          type: InternetAddressType.IPv6), // Google
      port: DataConnectionChecker.DEFAULT_PORT,
      timeout: DataConnectionChecker.DEFAULT_TIMEOUT,
    ),
    AddressCheckOptions(
      InternetAddress('2001:4860:4860::8844',
          type: InternetAddressType.IPv6), // Google
      port: DataConnectionChecker.DEFAULT_PORT,
      timeout: DataConnectionChecker.DEFAULT_TIMEOUT,
    ),
    AddressCheckOptions(
      InternetAddress('2606:4700:4700::64',
          type: InternetAddressType.IPv6), // CloudFlare
      port: DataConnectionChecker.DEFAULT_PORT,
      timeout: DataConnectionChecker.DEFAULT_TIMEOUT,
    ),
    AddressCheckOptions(
      InternetAddress('2606:4700:4700::6400',
          type: InternetAddressType.IPv6), // CloudFlare
      port: DataConnectionChecker.DEFAULT_PORT,
      timeout: DataConnectionChecker.DEFAULT_TIMEOUT,
    ),
    AddressCheckOptions(
      InternetAddress('2620:119:35::35',
          type: InternetAddressType.IPv6), // OpenDNS
      port: DataConnectionChecker.DEFAULT_PORT,
      timeout: DataConnectionChecker.DEFAULT_TIMEOUT,
    ),
    AddressCheckOptions(
      InternetAddress('2620:119:53::53',
          type: InternetAddressType.IPv6), // OpenDNS
      port: DataConnectionChecker.DEFAULT_PORT,
      timeout: DataConnectionChecker.DEFAULT_TIMEOUT,
    ),
  ]);
// Network Getter Stream
  Stream<ConnectivityResult> get networkGetterStream {
    return _networkStatusController.stream;
  }

// data Connection Stream
  Stream<NetworkStatus> get dataConncectionStream {
    return _dataConnectionController.stream;
  }

//
  NetworkStatusService() {
    Connectivity().onConnectivityChanged.listen((event) {
      _networkStatusController.add(event);
    });

    DataConnectionChecker().onStatusChange.listen((dataConnectionEvent) {
      _dataConnectionController.add(_dataConnectionStatus(dataConnectionEvent));
    });
  }
  NetworkStatus _dataConnectionStatus(DataConnectionStatus status) {
    return status == DataConnectionStatus.connected
        ? NetworkStatus.online
        : NetworkStatus.offline;
  }
}


Then I will show the widget as the Stream returns.

Shahryar Rafique
  • 1,211
  • 15
  • 35
  • Would creating a parent class work for you? It might be helpful if you included a minimal code sample. – Developer Extraordinare Jan 25 '22 at 18:14
  • @developerextraordinare I just added the code. Can you give me an example regarding the parent class in a flutter in this scenario or any scenario? – Shahryar Rafique Jan 25 '22 at 19:03
  • Have you already built the widget that you are trying to show? or is your question how to show it? – Developer Extraordinare Jan 25 '22 at 19:11
  • I built the widget where it checks network is online or offline but wants to write a code once like this if the internet disconnect it overlay the widget on top of the screen without writing the code every time on a different screen for this. – Shahryar Rafique Jan 25 '22 at 19:15
  • 1
    This might be helpful https://stackoverflow.com/questions/51659805/persisting-appbar-drawer-across-all-pages-flutter It may also be possible to wrap your entire app in the widget, but that seems like a bad solution. – Developer Extraordinare Jan 25 '22 at 19:51

0 Answers0