Files
umbrix/lib/features/proxy/active/ip_widget.dart

108 lines
3.2 KiB
Dart
Raw Normal View History

2024-02-11 00:29:09 +03:30
import 'package:circle_flags/circle_flags.dart';
import 'package:flutter/material.dart';
2024-02-13 18:49:58 +03:30
import 'package:hiddify/core/localization/translations.dart';
2024-02-13 19:54:07 +03:30
import 'package:hiddify/core/utils/ip_utils.dart';
2024-02-11 00:29:09 +03:30
import 'package:hiddify/utils/riverpod_utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
final _showIp = StateProvider.autoDispose((ref) {
ref.disposeDelay(const Duration(seconds: 20));
return false;
});
class IPText extends HookConsumerWidget {
const IPText({
2024-02-17 12:32:11 +03:30
required String this.ip,
required VoidCallback this.onLongPress,
2024-02-11 00:29:09 +03:30
this.constrained = false,
super.key,
});
2024-02-17 12:32:11 +03:30
const IPText.unknown({
this.onLongPress,
this.constrained = false,
super.key,
}) : ip = null;
final String? ip;
final VoidCallback? onLongPress;
2024-02-11 00:29:09 +03:30
final bool constrained;
@override
Widget build(BuildContext context, WidgetRef ref) {
2024-02-13 18:49:58 +03:30
final t = ref.watch(translationsProvider);
2024-02-11 00:29:09 +03:30
final isVisible = ref.watch(_showIp);
final textTheme = Theme.of(context).textTheme;
2024-02-15 12:24:18 +03:30
final ipStyle = constrained ? textTheme.labelMedium : textTheme.labelLarge;
2024-02-11 00:29:09 +03:30
2024-02-13 18:49:58 +03:30
return Semantics(
label: t.proxies.ipInfoSemantics.address,
child: InkWell(
2024-02-17 12:32:11 +03:30
onTap: ip == null
? null
: () {
ref.read(_showIp.notifier).state = !isVisible;
},
2024-02-13 18:49:58 +03:30
onLongPress: onLongPress,
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 2),
2024-02-17 12:32:11 +03:30
child: switch (ip) {
final ip? => AnimatedCrossFade(
firstChild: Text(
ip,
style: ipStyle,
textDirection: TextDirection.ltr,
overflow: TextOverflow.ellipsis,
),
secondChild: Padding(
padding: constrained
? EdgeInsets.zero
: const EdgeInsetsDirectional.only(end: 48),
child: Text(
obscureIp(ip),
semanticsLabel: t.general.hidden,
style: ipStyle,
textDirection: TextDirection.ltr,
overflow: TextOverflow.ellipsis,
),
),
crossFadeState: isVisible
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
duration: const Duration(milliseconds: 200),
),
_ => Text(
t.general.unknown,
style: constrained ? textTheme.bodySmall : ipStyle,
2024-02-13 18:49:58 +03:30
overflow: TextOverflow.ellipsis,
),
2024-02-17 12:32:11 +03:30
},
2024-02-11 00:29:09 +03:30
),
),
);
}
}
class IPCountryFlag extends HookConsumerWidget {
const IPCountryFlag({required this.countryCode, this.size = 24, super.key});
final String countryCode;
final double size;
@override
Widget build(BuildContext context, WidgetRef ref) {
2024-02-13 18:49:58 +03:30
final t = ref.watch(translationsProvider);
2024-02-11 00:29:09 +03:30
2024-02-13 18:49:58 +03:30
return Semantics(
label: t.proxies.ipInfoSemantics.country,
2024-02-13 19:54:07 +03:30
child: Container(
width: size,
height: size,
padding: const EdgeInsets.all(2),
child: Center(child: CircleFlag(countryCode)),
2024-02-11 00:29:09 +03:30
),
);
}
}