feat: dialig for conectivity
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../config.dart';
|
||||
|
||||
/// وضعیتِ اینترنت را نگه میدارد: هم قطعِ رابطِ شبکه (وایفای/دیتا) و هم دسترسیِ
|
||||
/// واقعی به سرور (با پینگِ سبکِ /health). [online] برای نمایشِ اوورلیِ «اتصال نیست».
|
||||
class ConnectivityService {
|
||||
ConnectivityService._();
|
||||
static final ConnectivityService I = ConnectivityService._();
|
||||
|
||||
final ValueNotifier<bool> online = ValueNotifier<bool>(true);
|
||||
|
||||
final Connectivity _conn = Connectivity();
|
||||
final Dio _dio = Dio(BaseOptions(
|
||||
connectTimeout: const Duration(seconds: 4),
|
||||
receiveTimeout: const Duration(seconds: 4),
|
||||
));
|
||||
StreamSubscription? _sub;
|
||||
bool _checking = false;
|
||||
|
||||
Future<void> init() async {
|
||||
_sub = _conn.onConnectivityChanged.listen(_onChange);
|
||||
await recheck();
|
||||
}
|
||||
|
||||
void _onChange(List<ConnectivityResult> results) {
|
||||
final hasInterface =
|
||||
results.any((r) => r != ConnectivityResult.none);
|
||||
if (!hasInterface) {
|
||||
online.value = false; // بیدرنگ: رابطِ شبکه قطع است
|
||||
} else {
|
||||
_verify(); // رابط هست؛ دسترسیِ واقعی را بررسی کن
|
||||
}
|
||||
}
|
||||
|
||||
/// بررسیِ دستیِ اتصال (دکمهی «تلاش مجدد»).
|
||||
Future<void> recheck() async {
|
||||
final results = await _conn.checkConnectivity();
|
||||
final hasInterface = results.any((r) => r != ConnectivityResult.none);
|
||||
if (!hasInterface) {
|
||||
online.value = false;
|
||||
return;
|
||||
}
|
||||
await _verify();
|
||||
}
|
||||
|
||||
Future<void> _verify() async {
|
||||
if (_checking) return;
|
||||
_checking = true;
|
||||
try {
|
||||
final res = await _dio.get('${AppConfig.baseUrl}/health');
|
||||
online.value = res.statusCode == 200;
|
||||
} catch (_) {
|
||||
online.value = false;
|
||||
} finally {
|
||||
_checking = false;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() => _sub?.cancel();
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../network/connectivity_service.dart';
|
||||
import '../theme/app_theme.dart';
|
||||
|
||||
/// [child] را میپوشاند و هنگامِ قطعِ اینترنت یک اوورلیِ «اتصال برقرار نیست» با
|
||||
/// دکمهی «تلاش مجدد» نشان میدهد. بهمحضِ برقراریِ اتصال، خودش کنار میرود.
|
||||
class OfflineGate extends StatelessWidget {
|
||||
final Widget child;
|
||||
const OfflineGate({super.key, required this.child});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Stack(
|
||||
children: [
|
||||
child,
|
||||
ValueListenableBuilder<bool>(
|
||||
valueListenable: ConnectivityService.I.online,
|
||||
builder: (context, online, _) =>
|
||||
online ? const SizedBox.shrink() : const _OfflineOverlay(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _OfflineOverlay extends StatefulWidget {
|
||||
const _OfflineOverlay();
|
||||
@override
|
||||
State<_OfflineOverlay> createState() => _OfflineOverlayState();
|
||||
}
|
||||
|
||||
class _OfflineOverlayState extends State<_OfflineOverlay> {
|
||||
bool _busy = false;
|
||||
|
||||
Future<void> _retry() async {
|
||||
setState(() => _busy = true);
|
||||
await ConnectivityService.I.recheck();
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Positioned.fill(
|
||||
child: Material(
|
||||
color: Colors.black.withValues(alpha: 0.82),
|
||||
child: Center(
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 32),
|
||||
padding: const EdgeInsets.fromLTRB(24, 28, 24, 22),
|
||||
decoration: BoxDecoration(
|
||||
gradient: const LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [AppColors.lapisMid, AppColors.lapisLo],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
border: Border.all(color: AppColors.gold, width: 2),
|
||||
boxShadow: const [
|
||||
BoxShadow(color: Colors.black54, blurRadius: 20),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 72,
|
||||
height: 72,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: AppColors.bgDark,
|
||||
border: Border.all(color: AppColors.goldDark, width: 2),
|
||||
),
|
||||
child: const Icon(Icons.wifi_off_rounded,
|
||||
color: AppColors.gold, size: 38),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'اتصال به اینترنت برقرار نیست',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: AppColors.gold,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'اینترنتِ دستگاهت را بررسی کن و دوباره تلاش کن.',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Colors.white70, fontSize: 13),
|
||||
),
|
||||
const SizedBox(height: 22),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: _busy ? null : _retry,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.gold,
|
||||
foregroundColor: AppColors.bg,
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
icon: _busy
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2, color: AppColors.bg),
|
||||
)
|
||||
: const Icon(Icons.refresh, size: 20),
|
||||
label: Text(_busy ? 'در حال بررسی…' : 'تلاش مجدد'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user