internet connection handling

This commit is contained in:
Username1548
2025-02-03 15:32:12 +07:00
parent 4242265830
commit 589122581b
30 changed files with 1384 additions and 666 deletions

View File

@@ -0,0 +1,172 @@
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:easy_debounce/easy_debounce.dart';
import 'package:eatvio_app/core/theme/custom_text_style.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/custom_outlined_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
class InternetConnectionCheckerScreen extends StatefulWidget {
const InternetConnectionCheckerScreen({super.key, required this.child});
final Widget child;
@override
State<InternetConnectionCheckerScreen> createState() => _InternetConnectionCheckerScreenState();
}
class _InternetConnectionCheckerScreenState extends State<InternetConnectionCheckerScreen> {
bool _hasInternet = true;
final Connectivity _connectivity = Connectivity();
late StreamSubscription<List<ConnectivityResult>> _connectivitySubscription;
late final AppLifecycleListener _listener;
@override
void initState() {
super.initState();
_checkConnection();
_connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateAccessStatus);
_listener = AppLifecycleListener(
onResume: () async {
_connectivitySubscription.resume();
await _checkConnection();
},
onHide: _connectivitySubscription.pause,
onPause: _connectivitySubscription.pause,
);
}
Future<void> _checkConnection() async {
late List<ConnectivityResult> result;
try {
result = await _connectivity.checkConnectivity();
} on PlatformException catch (e) {
log('Couldn\'t check connectivity status', error: e);
return;
}
return _updateAccessStatus(result);
}
Future<void> _updateAccessStatus(List<ConnectivityResult> result) async {
final hasInternet = await _checkInternetAccess(result);
setState(() {
_hasInternet = hasInternet;
});
}
Future<bool> _checkInternetAccess(List<ConnectivityResult> result) async {
if (result.contains(ConnectivityResult.none)) {
return false;
}
try {
final response = await Future.any([
http.get(Uri.parse('https://google.com')),
http.get(Uri.parse('https://yandex.ru')),
http.get(Uri.parse('https://dev2.eatvio.com')),
http.get(Uri.parse('https://eatvio.ru')),
]).timeout(Duration(seconds: 5));
return response.statusCode >= 100 && response.statusCode < 600;
} on SocketException catch (_) {
return false;
} on TimeoutException catch (_) {
return false;
} on HandshakeException catch (_) {
return false;
} on CertificateException catch (_) {
return false;
} on http.ClientException catch (_) {
return false;
} catch (e, st) {
log(e.toString(), stackTrace: st);
return false;
}
}
@override
void dispose() {
_connectivitySubscription.cancel();
_listener.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
widget.child,
if (!_hasInternet)
NoInternetScreen(
onTryAgain: () async {
EasyDebounce.debounce(
'internet connection check',
Duration(seconds: 2),
() async {
await _checkConnection();
},
);
},
),
],
);
}
}
class NoInternetScreen extends StatelessWidget {
const NoInternetScreen({super.key, required this.onTryAgain});
final Future<void> Function() onTryAgain;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Spacer(),
SizedBox(height: 28),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 52),
child: Text(
'Отсутствует подключение к интернету',
textAlign: TextAlign.center,
style: TextStyles.headingXLSemibold.copyWith(
color: appTheme.black,
),
),
),
SizedBox(height: 16),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 40),
child: Text(
'Проверьте соединение с сетью и попробуйте ещё раз',
textAlign: TextAlign.center,
style: TextStyles.textMMedium.copyWith(
color: appTheme.gray,
),
),
),
SizedBox(height: 20),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: CustomOutlinedButton(
onPressed: onTryAgain,
height: 52,
text: 'Попробовать ещё раз',
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.whiteA700),
buttonStyle: ButtonStyle(
backgroundColor: WidgetStatePropertyAll(appTheme.blueA400),
side: WidgetStatePropertyAll(BorderSide.none),
),
),
),
Spacer(),
],
),
);
}
}