app/lib/ui/home/input_method/server_qr_dialog_ui.dart

123 lines
3.4 KiB
Dart
Raw Normal View History

2024-11-07 00:34:11 +08:00
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:qr_flutter/qr_flutter.dart';
2024-11-07 20:16:08 +08:00
import 'package:riverpod_annotation/riverpod_annotation.dart';
2024-11-07 21:33:30 +08:00
import 'package:starcitizen_doctor/generated/l10n.dart';
2024-11-07 00:34:11 +08:00
import 'server.dart';
2024-11-07 20:16:08 +08:00
part 'server_qr_dialog_ui.g.dart';
@riverpod
class ServerQrState extends _$ServerQrState {
@override
bool build() {
return true;
}
BuildContext? _context;
// ignore: avoid_build_context_in_providers
void setupContext(BuildContext context) {
_context = context;
}
popDialog() {
_context?.pop();
}
}
2024-11-07 00:34:11 +08:00
class ServerQrDialogUI extends HookConsumerWidget {
const ServerQrDialogUI({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final serverState = ref.watch(inputMethodServerProvider);
final urls = serverState.serverAddressText?.split(",") ?? [""];
final hasMultipleUrls = urls.length > 1;
final index = useState(0);
2024-11-07 20:16:08 +08:00
final model = ref.watch(serverQrStateProvider.notifier);
model.setupContext(context);
2024-11-07 00:34:11 +08:00
return ContentDialog(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * .4,
),
title: makeTitle(context),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(width: double.infinity, height: 12),
if (hasMultipleUrls) ...[
2024-11-07 21:33:30 +08:00
Text(S.current.input_method_ip_address_not_found),
2024-11-07 00:34:11 +08:00
] else
Text(
2024-11-07 21:33:30 +08:00
S.current.input_method_scan_qr_code,
2024-11-07 00:34:11 +08:00
style: TextStyle(color: Colors.white.withOpacity(.8)),
),
SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (hasMultipleUrls)
IconButton(
icon: Icon(FluentIcons.chevron_left),
onPressed: () {
index.value = (index.value - 1) % urls.length;
}),
SizedBox(width: 24),
Container(
color: Colors.white,
child: QrImageView(
data: urls[index.value],
size: 200,
padding: EdgeInsets.all(12),
),
),
SizedBox(width: 24),
if (hasMultipleUrls)
IconButton(
icon: Icon(FluentIcons.chevron_right),
onPressed: () {
index.value = (index.value + 1) % urls.length;
}),
],
),
SizedBox(height: 12),
Text(
hasMultipleUrls
2024-11-07 20:16:08 +08:00
? "${urls[index.value]} (${index.value + 1} / ${urls.length})"
2024-11-07 00:34:11 +08:00
: urls[index.value],
style: TextStyle(fontSize: 13, color: Colors.white.withOpacity(.6)),
),
],
),
);
}
Widget makeTitle(BuildContext context) {
return Row(
children: [
IconButton(
icon: const Icon(
FluentIcons.back,
size: 22,
),
onPressed: () {
context.pop();
}),
const SizedBox(width: 12),
2024-11-07 21:33:30 +08:00
Text(S.current.input_method_service_qr_code),
2024-11-07 00:34:11 +08:00
],
);
}
2024-11-07 21:33:30 +08:00
}