added connectivity service

This commit is contained in:
Mehul Ahal 2022-12-20 09:47:07 +01:00
parent 8e54cfffc5
commit 193ae3b0ea
12 changed files with 384 additions and 8 deletions

View file

@ -0,0 +1,71 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import '../data/constants/const_durations.dart';
import '../services/connection_service.dart';
class McgScaffold extends StatelessWidget {
const McgScaffold({
this.appBar,
this.bodyBuilderCompleter,
this.body,
this.waitingWidget,
this.forceInternetCheck = false,
super.key,
});
final AppBar? appBar;
/// Awaits an external signal (complete) before building the body.
final Completer? bodyBuilderCompleter;
final Widget? body;
/// Custom widget to be used while awaiting [bodyBuilderCompleter].
///
/// Defaults to using [PlatformCircularProgressIndicator].
final Widget? waitingWidget;
/// Enabling listing to [ConnectionState], showing a small text at the top, when connectivity is lost.
final bool forceInternetCheck;
@override
Widget build(BuildContext context) {
final Widget loginOptionsBody = bodyBuilderCompleter != null
? FutureBuilder(
future: bodyBuilderCompleter!.future,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
return Center(child: waitingWidget ?? const CircularProgressIndicator());
case ConnectionState.done:
return body ?? const SizedBox.shrink();
}
},
)
: body ?? const SizedBox.shrink();
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,
],
),
),
)
: loginOptionsBody,
);
}
}