setupConnectivityListner method

dynamic setupConnectivityListner({
  1. CustomNoInternetWrapper? offlineWidget,
  2. GlobalKey<NavigatorState>? navigatorKey,
  3. NoConnectivityStyle? connectivityStyle = NoConnectivityStyle.SNACKBAR,
  4. Widget? noInternetText,
})

Implementation

setupConnectivityListner(
    {CustomNoInternetWrapper? offlineWidget,
    GlobalKey<NavigatorState>? navigatorKey,
    NoConnectivityStyle? connectivityStyle = NoConnectivityStyle.SNACKBAR,
    Widget? noInternetText}) async {
  if (navigatorKey != null) {
    _contextKey = navigatorKey;
  }
  _userWidget = offlineWidget;
  _connectivityStyle = connectivityStyle;
  customNoInternetText = noInternetText;
  if (_connectivityStyle == NoConnectivityStyle.CUSTOM &&
      _userWidget == null) {
    throw ("widgetForNoInternet is missing");
  }

  checker.onStatusChange.listen((status) {
    if (_connectivityStyle == NoConnectivityStyle.CUSTOM &&
        (_contextKey.currentState?.overlay) != null) {
      _overlayState = (_contextKey.currentState!.overlay);
    }

    switch (status) {
      /// ConnectedState
      case InternetConnectionStatus.connected:
        try {
          _removeNoInternet();
          connectivityController.sink
              .add(ConnectivityWatcherStatus.connected);
        } catch (e) {
          print(e);
        }

        break;

      /// DisconnectedState
      case InternetConnectionStatus.disconnected:
        connectivityController.sink
            .add(ConnectivityWatcherStatus.disconnected);
        showNoInternet();
        break;
    }
  });
}