fixed connection problem

This commit is contained in:
Mehul Ahal 2022-12-22 22:29:37 +01:00 committed by Mguy13
parent 127a09b597
commit 47945dbec7
11 changed files with 218 additions and 196 deletions

View file

@ -1,12 +1,15 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';
import 'package:mc_gallery/features/core/data/constants/const_colors.dart';
import 'package:mc_gallery/features/core/services/logging_service.dart';
import 'package:mc_gallery/l10n/generated/l10n.dart';
import '../data/constants/const_durations.dart';
import '../services/connection_service.dart';
import '/features/core/services/connections_service.dart';
import '/features/core/services/overlay_service.dart';
class McgScaffold extends StatelessWidget {
const McgScaffold({
class McgScaffold extends StatelessWidget with LoggingService {
McgScaffold({
this.appBar,
this.bodyBuilderWaiter,
this.body,
@ -28,36 +31,59 @@ class McgScaffold extends StatelessWidget {
/// Enabling listing to [ConnectionState], showing a small text at the top, when connectivity is lost.
final bool forceInternetCheck;
final ConnectionsService _connectionsService = ConnectionsService.locate;
final OverlayService _overlayService = OverlayService.locate;
@override
Widget build(BuildContext context) {
final Widget loginOptionsBody = bodyBuilderWaiter != null
? ValueListenableBuilder<bool>(
valueListenable: bodyBuilderWaiter!,
builder: (context, final isReady, child) => !isReady
? Center(child: waitingWidget ?? const CircularProgressIndicator())
: body ?? const SizedBox.shrink(),
)
: body ?? const SizedBox.shrink();
if (forceInternetCheck) {
_connectionsService.internetConnectionStatusListenable.addListener(
() => _handleOverlayDisplay(context: context),
);
if (_connectionsService.internetConnectionStatusListenable.value ==
InternetConnectionStatus.disconnected) _handleOverlayDisplay(context: context);
}
return Scaffold(
appBar: appBar,
body: forceInternetCheck
? SingleChildScrollView(
child: ValueListenableBuilder<bool>(
valueListenable: ConnectionService.locate.hasInternetConnectionListenable,
builder: (context, hasInternetConnection, _) => Column(
children: [
AnimatedSwitcher(
duration: ConstDurations.defaultAnimationDuration,
child: !hasInternetConnection ? Text('No internet') : const SizedBox.shrink(),
),
loginOptionsBody,
],
),
),
body: bodyBuilderWaiter != null
? ValueListenableBuilder<bool>(
valueListenable: bodyBuilderWaiter!,
builder: (context, final isReady, child) => !isReady
? Center(child: waitingWidget ?? const CircularProgressIndicator())
: body ?? const SizedBox.shrink(),
)
: loginOptionsBody,
: body ?? const SizedBox.shrink(),
);
}
void _handleOverlayDisplay({required BuildContext context}) {
switch (_connectionsService.internetConnectionStatusListenable.value) {
case InternetConnectionStatus.disconnected:
_overlayService.insertOverlayEntry(
context,
tag: runtimeType.toString(),
overlayEntry: OverlayEntry(
opaque: false,
builder: (_) => Align(
alignment: Alignment.topCenter,
child: Card(
elevation: 16,
surfaceTintColor: ConstColours.transparent,
color: ConstColours.transparent,
child: Padding(
padding: const EdgeInsets.all(16),
child: Text(
Strings.current.noInternetMessage,
),
),
),
),
),
);
break;
case InternetConnectionStatus.connected:
_overlayService.removeOverlayEntry(tag: runtimeType.toString());
}
}
}