2024-12-09 01:40:39 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import 'package:fl_clash/common/common.dart';
|
|
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
|
|
|
|
import 'package:fl_clash/models/models.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:fl_clash/providers/app.dart';
|
2024-12-09 01:40:39 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
|
|
|
|
import 'package:fl_clash/widgets/widgets.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-12-09 01:40:39 +08:00
|
|
|
|
|
|
|
|
final _networkDetectionState = ValueNotifier<NetworkDetectionState>(
|
|
|
|
|
const NetworkDetectionState(
|
2025-03-12 17:15:31 +08:00
|
|
|
isTesting: false,
|
|
|
|
|
isLoading: true,
|
2024-12-09 01:40:39 +08:00
|
|
|
ipInfo: null,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class NetworkDetection extends ConsumerStatefulWidget {
|
2024-12-09 01:40:39 +08:00
|
|
|
const NetworkDetection({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
2025-02-09 18:39:38 +08:00
|
|
|
ConsumerState<NetworkDetection> createState() => _NetworkDetectionState();
|
2024-12-09 01:40:39 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class _NetworkDetectionState extends ConsumerState<NetworkDetection> {
|
2024-12-09 01:40:39 +08:00
|
|
|
bool? _preIsStart;
|
|
|
|
|
Timer? _setTimeoutTimer;
|
|
|
|
|
CancelToken? cancelToken;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
2025-02-09 18:39:38 +08:00
|
|
|
ref.listenManual(checkIpNumProvider, (prev, next) {
|
|
|
|
|
if (prev != next) {
|
|
|
|
|
_startCheck();
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-03-12 17:15:31 +08:00
|
|
|
if (!_networkDetectionState.value.isTesting &&
|
|
|
|
|
_networkDetectionState.value.isLoading) {
|
|
|
|
|
_startCheck();
|
|
|
|
|
}
|
2024-12-09 01:40:39 +08:00
|
|
|
super.initState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_startCheck() async {
|
|
|
|
|
if (cancelToken != null) {
|
|
|
|
|
cancelToken!.cancel();
|
|
|
|
|
cancelToken = null;
|
|
|
|
|
}
|
|
|
|
|
debouncer.call(
|
|
|
|
|
DebounceTag.checkIp,
|
|
|
|
|
_checkIp,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_checkIp() async {
|
2025-02-09 18:39:38 +08:00
|
|
|
final appState = globalState.appState;
|
2024-12-09 01:40:39 +08:00
|
|
|
final isInit = appState.isInit;
|
|
|
|
|
if (!isInit) return;
|
2025-02-09 18:39:38 +08:00
|
|
|
final isStart = appState.runTime != null;
|
|
|
|
|
if (_preIsStart == false &&
|
|
|
|
|
_preIsStart == isStart &&
|
2025-03-08 04:22:03 +08:00
|
|
|
_networkDetectionState.value.ipInfo != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-09 01:40:39 +08:00
|
|
|
_clearSetTimeoutTimer();
|
|
|
|
|
_networkDetectionState.value = _networkDetectionState.value.copyWith(
|
2025-03-12 17:15:31 +08:00
|
|
|
isLoading: true,
|
2024-12-09 01:40:39 +08:00
|
|
|
ipInfo: null,
|
|
|
|
|
);
|
|
|
|
|
_preIsStart = isStart;
|
|
|
|
|
if (cancelToken != null) {
|
|
|
|
|
cancelToken!.cancel();
|
|
|
|
|
cancelToken = null;
|
|
|
|
|
}
|
|
|
|
|
cancelToken = CancelToken();
|
|
|
|
|
try {
|
2025-03-12 17:15:31 +08:00
|
|
|
_networkDetectionState.value = _networkDetectionState.value.copyWith(
|
|
|
|
|
isTesting: true,
|
|
|
|
|
);
|
2024-12-09 01:40:39 +08:00
|
|
|
final ipInfo = await request.checkIp(cancelToken: cancelToken);
|
2025-03-12 17:15:31 +08:00
|
|
|
_networkDetectionState.value = _networkDetectionState.value.copyWith(
|
|
|
|
|
isTesting: false,
|
|
|
|
|
);
|
2024-12-09 01:40:39 +08:00
|
|
|
if (ipInfo != null) {
|
|
|
|
|
_networkDetectionState.value = _networkDetectionState.value.copyWith(
|
2025-03-12 17:15:31 +08:00
|
|
|
isLoading: false,
|
2024-12-09 01:40:39 +08:00
|
|
|
ipInfo: ipInfo,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_clearSetTimeoutTimer();
|
|
|
|
|
_setTimeoutTimer = Timer(const Duration(milliseconds: 300), () {
|
|
|
|
|
_networkDetectionState.value = _networkDetectionState.value.copyWith(
|
2025-03-12 17:15:31 +08:00
|
|
|
isLoading: false,
|
2024-12-09 01:40:39 +08:00
|
|
|
ipInfo: null,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e.toString() == "cancelled") {
|
|
|
|
|
_networkDetectionState.value = _networkDetectionState.value.copyWith(
|
2025-03-12 17:15:31 +08:00
|
|
|
isLoading: true,
|
2024-12-09 01:40:39 +08:00
|
|
|
ipInfo: null,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_clearSetTimeoutTimer();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_clearSetTimeoutTimer() {
|
|
|
|
|
if (_setTimeoutTimer != null) {
|
|
|
|
|
_setTimeoutTimer?.cancel();
|
|
|
|
|
_setTimeoutTimer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_countryCodeToEmoji(String countryCode) {
|
|
|
|
|
final String code = countryCode.toUpperCase();
|
|
|
|
|
if (code.length != 2) {
|
|
|
|
|
return countryCode;
|
|
|
|
|
}
|
|
|
|
|
final int firstLetter = code.codeUnitAt(0) - 0x41 + 0x1F1E6;
|
|
|
|
|
final int secondLetter = code.codeUnitAt(1) - 0x41 + 0x1F1E6;
|
|
|
|
|
return String.fromCharCode(firstLetter) + String.fromCharCode(secondLetter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return SizedBox(
|
|
|
|
|
height: getWidgetHeight(1),
|
2025-02-09 18:39:38 +08:00
|
|
|
child: ValueListenableBuilder<NetworkDetectionState>(
|
|
|
|
|
valueListenable: _networkDetectionState,
|
|
|
|
|
builder: (_, state, __) {
|
|
|
|
|
final ipInfo = state.ipInfo;
|
2025-03-12 17:15:31 +08:00
|
|
|
final isLoading = state.isLoading;
|
2025-02-09 18:39:38 +08:00
|
|
|
return CommonCard(
|
|
|
|
|
onPressed: () {},
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
height: globalState.measure.titleMediumHeight + 16,
|
|
|
|
|
padding: baseInfoEdgeInsets.copyWith(
|
|
|
|
|
bottom: 0,
|
|
|
|
|
),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
|
children: [
|
|
|
|
|
ipInfo != null
|
|
|
|
|
? Text(
|
|
|
|
|
_countryCodeToEmoji(
|
|
|
|
|
ipInfo.countryCode,
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
|
|
|
|
style: Theme.of(context)
|
|
|
|
|
.textTheme
|
2025-02-09 18:39:38 +08:00
|
|
|
.titleMedium
|
|
|
|
|
?.toLight
|
|
|
|
|
.copyWith(
|
|
|
|
|
fontFamily: FontFamily.twEmoji.value,
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
)
|
|
|
|
|
: Icon(
|
|
|
|
|
Icons.network_check,
|
|
|
|
|
color: Theme.of(context)
|
|
|
|
|
.colorScheme
|
|
|
|
|
.onSurfaceVariant,
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
const SizedBox(
|
|
|
|
|
width: 8,
|
|
|
|
|
),
|
|
|
|
|
Flexible(
|
|
|
|
|
flex: 1,
|
|
|
|
|
child: TooltipText(
|
|
|
|
|
text: Text(
|
|
|
|
|
appLocalizations.networkDetection,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: Theme.of(context)
|
|
|
|
|
.textTheme
|
|
|
|
|
.titleSmall
|
|
|
|
|
?.copyWith(
|
|
|
|
|
color: context.colorScheme.onSurfaceVariant,
|
|
|
|
|
),
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
|
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
),
|
|
|
|
|
SizedBox(width: 2),
|
|
|
|
|
AspectRatio(
|
|
|
|
|
aspectRatio: 1,
|
|
|
|
|
child: IconButton(
|
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
globalState.showMessage(
|
|
|
|
|
title: appLocalizations.tip,
|
|
|
|
|
message: TextSpan(
|
|
|
|
|
text: appLocalizations.detectionTip,
|
|
|
|
|
),
|
|
|
|
|
cancelable: false,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
icon: Icon(
|
2025-04-18 17:50:46 +08:00
|
|
|
size: 16.ap,
|
2025-02-09 18:39:38 +08:00
|
|
|
Icons.info_outline,
|
|
|
|
|
color: context.colorScheme.onSurfaceVariant,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
],
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
),
|
|
|
|
|
Container(
|
|
|
|
|
padding: baseInfoEdgeInsets.copyWith(
|
|
|
|
|
top: 0,
|
|
|
|
|
),
|
|
|
|
|
child: SizedBox(
|
|
|
|
|
height: globalState.measure.bodyMediumHeight + 2,
|
2025-03-12 17:15:31 +08:00
|
|
|
child: FadeThroughBox(
|
2025-02-09 18:39:38 +08:00
|
|
|
child: ipInfo != null
|
|
|
|
|
? TooltipText(
|
|
|
|
|
text: Text(
|
|
|
|
|
ipInfo.ip,
|
|
|
|
|
style: context.textTheme.bodyMedium?.toLight
|
|
|
|
|
.adjustSize(1),
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
),
|
|
|
|
|
)
|
2025-03-12 17:15:31 +08:00
|
|
|
: FadeThroughBox(
|
|
|
|
|
child: isLoading == false && ipInfo == null
|
2025-02-09 18:39:38 +08:00
|
|
|
? Text(
|
|
|
|
|
"timeout",
|
|
|
|
|
style: context.textTheme.bodyMedium
|
|
|
|
|
?.copyWith(color: Colors.red)
|
|
|
|
|
.adjustSize(1),
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
)
|
|
|
|
|
: Container(
|
|
|
|
|
padding: const EdgeInsets.all(2),
|
|
|
|
|
child: const AspectRatio(
|
|
|
|
|
aspectRatio: 1,
|
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
|
strokeWidth: 2,
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
|
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
),
|
|
|
|
|
),
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-12-09 01:40:39 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|