2025-12-16 11:23:09 +08:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
2025-07-31 17:09:18 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
|
|
|
|
import 'package:fl_clash/models/common.dart';
|
2025-04-18 17:50:46 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
2024-12-09 01:40:39 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
|
|
extension NumExt on num {
|
2025-06-07 01:48:34 +08:00
|
|
|
String fixed({int decimals = 2}) {
|
2025-02-09 18:39:38 +08:00
|
|
|
String formatted = toStringAsFixed(decimals);
|
|
|
|
|
if (formatted.contains('.')) {
|
|
|
|
|
formatted = formatted.replaceAll(RegExp(r'0*$'), '');
|
|
|
|
|
if (formatted.endsWith('.')) {
|
|
|
|
|
formatted = formatted.substring(0, formatted.length - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return formatted;
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2025-04-18 17:50:46 +08:00
|
|
|
|
|
|
|
|
double get ap {
|
|
|
|
|
return this * (1 + (globalState.theme.textScaleFactor - 1) * 0.5);
|
|
|
|
|
}
|
2025-07-31 17:09:18 +08:00
|
|
|
|
2025-12-16 11:23:09 +08:00
|
|
|
double get mAp {
|
|
|
|
|
return this * min((1 + (globalState.theme.textScaleFactor - 1) * 0.5), 1);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 17:09:18 +08:00
|
|
|
TrafficShow get traffic {
|
|
|
|
|
final units = TrafficUnit.values;
|
|
|
|
|
var size = toDouble();
|
|
|
|
|
var unitIndex = 0;
|
|
|
|
|
while (size >= 1024 && unitIndex < units.length - 1) {
|
|
|
|
|
size /= 1024;
|
|
|
|
|
unitIndex++;
|
|
|
|
|
}
|
|
|
|
|
return TrafficShow(
|
|
|
|
|
value: size.fixed(decimals: 1),
|
|
|
|
|
unit: units[unitIndex].name,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-10-14 15:13:52 +08:00
|
|
|
|
|
|
|
|
TrafficShow get shortTraffic {
|
|
|
|
|
final units = TrafficUnit.values;
|
|
|
|
|
var size = toDouble();
|
|
|
|
|
var unitIndex = 0;
|
|
|
|
|
while (size >= 1024 && unitIndex < units.length - 1) {
|
|
|
|
|
size /= 1024;
|
|
|
|
|
unitIndex++;
|
|
|
|
|
}
|
|
|
|
|
return TrafficShow(
|
|
|
|
|
value: size.toStringAsFixed(0),
|
|
|
|
|
unit: ' ${units[unitIndex].name}',
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2024-12-09 01:40:39 +08:00
|
|
|
|
|
|
|
|
extension DoubleExt on double {
|
2025-06-07 01:48:34 +08:00
|
|
|
bool moreOrEqual(double value) {
|
2025-12-16 11:23:09 +08:00
|
|
|
return this > value || (value - this).abs() < precisionErrorTolerance + 1;
|
2024-12-09 01:40:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension OffsetExt on Offset {
|
|
|
|
|
double getCrossAxisOffset(Axis direction) {
|
|
|
|
|
return direction == Axis.vertical ? dx : dy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double getMainAxisOffset(Axis direction) {
|
|
|
|
|
return direction == Axis.vertical ? dy : dx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool less(Offset offset) {
|
|
|
|
|
if (dy < offset.dy) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (dy == offset.dy && dx < offset.dx) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension RectExt on Rect {
|
2025-06-07 01:48:34 +08:00
|
|
|
bool doRectIntersect(Rect rect) {
|
2024-12-09 01:40:39 +08:00
|
|
|
return left < rect.right &&
|
|
|
|
|
right > rect.left &&
|
|
|
|
|
top < rect.bottom &&
|
|
|
|
|
bottom > rect.top;
|
|
|
|
|
}
|
|
|
|
|
}
|