handleAllowedEmailsState function

Widget? handleAllowedEmailsState(
  1. BuildContext context,
  2. AllowedEmailsBlocState allowedEmailsState
)

Implementation

Widget? handleAllowedEmailsState(
  BuildContext context,
  AllowedEmailsBlocState allowedEmailsState,
) {
  Widget? errorWidget;
  final inScaffold = Scaffold.maybeOf(context) != null;

  final l10n = HyttaHubLocalizations.of(context)!;
  if (!allowedEmailsState.hasState() ||
      allowedEmailsState.state == AllowedEmailsBlocState_State.fetching) {
    errorWidget = const Center(child: CircularProgressIndicator());
  } else if (allowedEmailsState.state == AllowedEmailsBlocState_State.error) {
    errorWidget = Center(child: Text(l10n.unexpectedError));
  } else if (allowedEmailsState.state ==
      AllowedEmailsBlocState_State.permissionDenied) {
    errorWidget = Center(child: Text(l10n.permissionDenied));
  }

  if (errorWidget != null) {
    if (!inScaffold) {
      return Scaffold(
        appBar: AppBar(
          title: Text(l10n.errorTitle),
          leading: context.canPop()
              ? BackButton(onPressed: () => Navigator.of(context).pop())
              : null,
        ),
        body: Center(child: errorWidget),
      );
    } else {
      return errorWidget;
    }
  }
  return null;
}