2024-08-13 21:42:23 +08:00
|
|
|
import 'package:dio/dio.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
|
|
|
|
import 'package:fl_clash/models/models.dart';
|
2024-05-10 10:11:27 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/widgets/widgets.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
|
|
class NetworkDetection extends StatefulWidget {
|
|
|
|
|
const NetworkDetection({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<NetworkDetection> createState() => _NetworkDetectionState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _NetworkDetectionState extends State<NetworkDetection> {
|
2024-08-15 16:18:00 +08:00
|
|
|
final networkDetectionState = ValueNotifier<NetworkDetectionState>(
|
|
|
|
|
const NetworkDetectionState(
|
|
|
|
|
isTesting: true,
|
|
|
|
|
ipInfo: null,
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-06-07 17:22:55 +08:00
|
|
|
bool? _preIsStart;
|
2024-06-19 13:13:31 +08:00
|
|
|
Function? _checkIpDebounce;
|
2024-08-13 21:42:23 +08:00
|
|
|
CancelToken? cancelToken;
|
2024-06-06 16:31:08 +08:00
|
|
|
|
2024-07-26 08:05:22 +08:00
|
|
|
_checkIp() async {
|
|
|
|
|
final appState = globalState.appController.appState;
|
|
|
|
|
final isInit = appState.isInit;
|
2024-06-06 16:31:08 +08:00
|
|
|
if (!isInit) return;
|
2024-08-15 16:18:00 +08:00
|
|
|
final isStart = appState.isStart;
|
2024-06-07 17:22:55 +08:00
|
|
|
if (_preIsStart == false && _preIsStart == isStart) return;
|
2024-08-15 16:18:00 +08:00
|
|
|
networkDetectionState.value = networkDetectionState.value.copyWith(
|
|
|
|
|
isTesting: true,
|
|
|
|
|
ipInfo: null,
|
|
|
|
|
);
|
2024-07-26 08:05:22 +08:00
|
|
|
_preIsStart = isStart;
|
2024-08-13 21:42:23 +08:00
|
|
|
if (cancelToken != null) {
|
|
|
|
|
cancelToken!.cancel();
|
|
|
|
|
cancelToken = null;
|
|
|
|
|
}
|
|
|
|
|
cancelToken = CancelToken();
|
2024-08-15 16:18:00 +08:00
|
|
|
try {
|
|
|
|
|
final ipInfo = await request.checkIp(cancelToken: cancelToken);
|
|
|
|
|
networkDetectionState.value = networkDetectionState.value.copyWith(
|
|
|
|
|
isTesting: false,
|
|
|
|
|
ipInfo: ipInfo,
|
|
|
|
|
);
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
2024-06-06 17:13:32 +08:00
|
|
|
}
|
2024-06-06 16:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_checkIpContainer(Widget child) {
|
2024-07-26 08:05:22 +08:00
|
|
|
return Selector<AppState, num>(
|
|
|
|
|
selector: (_, appState) {
|
|
|
|
|
return appState.checkIpNum;
|
2024-06-06 16:31:08 +08:00
|
|
|
},
|
2024-07-26 08:05:22 +08:00
|
|
|
builder: (_, checkIpNum, child) {
|
2024-06-19 13:13:31 +08:00
|
|
|
if (_checkIpDebounce != null) {
|
2024-07-26 08:05:22 +08:00
|
|
|
_checkIpDebounce!();
|
2024-06-19 13:13:31 +08:00
|
|
|
}
|
2024-07-26 08:05:22 +08:00
|
|
|
return child!;
|
2024-06-06 16:31:08 +08:00
|
|
|
},
|
|
|
|
|
child: child,
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
2024-06-19 13:13:31 +08:00
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
2024-08-15 16:18:00 +08:00
|
|
|
networkDetectionState.dispose();
|
2024-06-19 13:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 21:42:23 +08:00
|
|
|
String 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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 13:13:31 +08:00
|
|
|
@override
|
2024-04-30 23:38:49 +08:00
|
|
|
Widget build(BuildContext context) {
|
2024-08-13 21:42:23 +08:00
|
|
|
_checkIpDebounce ??= debounce(_checkIp);
|
2024-06-06 16:31:08 +08:00
|
|
|
return _checkIpContainer(
|
2024-08-15 16:18:00 +08:00
|
|
|
ValueListenableBuilder<NetworkDetectionState>(
|
|
|
|
|
valueListenable: networkDetectionState,
|
|
|
|
|
builder: (_, state, __) {
|
|
|
|
|
final ipInfo = state.ipInfo;
|
|
|
|
|
final isTesting = state.isTesting;
|
2024-06-06 16:31:08 +08:00
|
|
|
return CommonCard(
|
2024-06-23 00:26:24 +08:00
|
|
|
onPressed: () {},
|
2024-05-17 15:39:41 +08:00
|
|
|
child: Column(
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
flex: 0,
|
|
|
|
|
child: Container(
|
2024-06-06 16:31:08 +08:00
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
Icon(
|
|
|
|
|
Icons.network_check,
|
|
|
|
|
color: Theme.of(context).colorScheme.primary,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
width: 8,
|
|
|
|
|
),
|
|
|
|
|
Flexible(
|
|
|
|
|
flex: 1,
|
|
|
|
|
child: FadeBox(
|
2024-08-15 16:18:00 +08:00
|
|
|
child: isTesting
|
|
|
|
|
? Text(
|
|
|
|
|
appLocalizations.checking,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style:
|
|
|
|
|
Theme.of(context).textTheme.titleMedium,
|
2024-06-06 17:13:32 +08:00
|
|
|
)
|
2024-08-15 16:18:00 +08:00
|
|
|
: ipInfo != null
|
|
|
|
|
? Container(
|
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
|
height: globalState.appController
|
|
|
|
|
.measure.titleMediumHeight,
|
|
|
|
|
child: Text(
|
|
|
|
|
countryCodeToEmoji(
|
|
|
|
|
ipInfo.countryCode),
|
2024-06-07 17:22:55 +08:00
|
|
|
style: Theme.of(context)
|
|
|
|
|
.textTheme
|
2024-08-15 16:18:00 +08:00
|
|
|
.titleLarge
|
|
|
|
|
?.copyWith(
|
|
|
|
|
fontFamily: "Twemoji",
|
|
|
|
|
),
|
2024-06-07 17:22:55 +08:00
|
|
|
),
|
2024-08-15 16:18:00 +08:00
|
|
|
)
|
|
|
|
|
: Text(
|
|
|
|
|
appLocalizations.checkError,
|
|
|
|
|
style: Theme.of(context)
|
|
|
|
|
.textTheme
|
|
|
|
|
.titleMedium,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
),
|
2024-06-06 16:31:08 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
|
|
|
|
),
|
2024-05-17 15:39:41 +08:00
|
|
|
),
|
2024-06-06 16:31:08 +08:00
|
|
|
Container(
|
2024-06-23 00:26:24 +08:00
|
|
|
height: globalState.appController.measure.titleLargeHeight +
|
|
|
|
|
24 -
|
2024-06-29 21:42:00 +08:00
|
|
|
2,
|
2024-06-06 16:31:08 +08:00
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
|
padding: const EdgeInsets.all(16).copyWith(top: 0),
|
|
|
|
|
child: FadeBox(
|
|
|
|
|
child: ipInfo != null
|
|
|
|
|
? Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
flex: 1,
|
|
|
|
|
child: TooltipText(
|
|
|
|
|
text: Text(
|
|
|
|
|
ipInfo.ip,
|
|
|
|
|
style: context.textTheme.titleLarge
|
2024-06-23 00:26:24 +08:00
|
|
|
?.toSoftBold.toMinus,
|
2024-06-06 16:31:08 +08:00
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-08-15 16:18:00 +08:00
|
|
|
: FadeBox(
|
|
|
|
|
child: isTesting == false && ipInfo == null
|
|
|
|
|
? Text(
|
|
|
|
|
"timeout",
|
|
|
|
|
style: context.textTheme.titleLarge
|
|
|
|
|
?.copyWith(color: Colors.red)
|
|
|
|
|
.toSoftBold
|
|
|
|
|
.toMinus,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
)
|
|
|
|
|
: Container(
|
|
|
|
|
padding: const EdgeInsets.all(2),
|
|
|
|
|
child: const AspectRatio(
|
|
|
|
|
aspectRatio: 1,
|
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
|
),
|
|
|
|
|
),
|
2024-06-06 16:31:08 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
2024-05-17 15:39:41 +08:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|