veilidchat/lib/contact_invitation/views/contact_invitation_display.dart

137 lines
4.7 KiB
Dart
Raw Normal View History

2023-09-22 11:20:28 -04:00
import 'dart:math';
2023-08-03 00:49:48 -04:00
2023-08-09 02:33:31 -04:00
import 'package:awesome_extensions/awesome_extensions.dart';
2023-08-04 01:00:38 -04:00
import 'package:basic_utils/basic_utils.dart';
2024-02-26 23:34:17 -05:00
import 'package:bloc_tools/bloc_tools.dart';
2023-08-01 00:39:50 -04:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2023-08-04 01:00:38 -04:00
import 'package:flutter/services.dart';
2023-08-02 21:09:28 -04:00
import 'package:flutter_translate/flutter_translate.dart';
2024-01-29 22:38:19 -05:00
import 'package:provider/provider.dart';
2023-08-04 01:00:38 -04:00
import 'package:qr_flutter/qr_flutter.dart';
2024-01-09 20:58:27 -05:00
import 'package:veilid_support/veilid_support.dart';
2023-08-01 00:39:50 -04:00
2024-01-09 20:58:27 -05:00
import '../../tools/tools.dart';
2023-08-02 21:09:28 -04:00
2024-01-29 22:38:19 -05:00
class InvitationGeneratorCubit extends FutureCubit<Uint8List> {
InvitationGeneratorCubit(super.fut);
}
2024-01-09 20:58:27 -05:00
class ContactInvitationDisplayDialog extends StatefulWidget {
2023-08-02 21:09:28 -04:00
const ContactInvitationDisplayDialog({
2023-08-03 00:49:48 -04:00
required this.message,
2023-08-02 21:09:28 -04:00
super.key,
});
2023-08-03 00:49:48 -04:00
final String message;
2023-08-02 21:09:28 -04:00
@override
2024-02-29 13:54:03 -05:00
State<ContactInvitationDisplayDialog> createState() =>
_ContactInvitationDisplayDialogState();
2023-08-03 00:49:48 -04:00
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
2024-02-14 21:33:15 -05:00
properties.add(StringProperty('message', message));
2023-08-03 00:49:48 -04:00
}
2023-08-02 21:09:28 -04:00
}
2024-02-29 13:54:03 -05:00
class _ContactInvitationDisplayDialogState
2024-01-09 20:58:27 -05:00
extends State<ContactInvitationDisplayDialog> {
2023-08-02 21:09:28 -04:00
final focusNode = FocusNode();
final formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
}
2023-08-01 00:39:50 -04:00
@override
2023-08-02 21:09:28 -04:00
void dispose() {
focusNode.dispose();
super.dispose();
2023-08-01 00:39:50 -04:00
}
2023-08-04 01:00:38 -04:00
String makeTextInvite(String message, Uint8List data) {
final invite = StringUtils.addCharAtPosition(
base64UrlNoPadEncode(data), '\n', 40,
repeat: true);
final msg = message.isNotEmpty ? '$message\n' : '';
return '$msg'
2023-08-05 01:00:46 -04:00
'--- BEGIN VEILIDCHAT CONTACT INVITE ----\n'
2023-08-04 01:00:38 -04:00
'$invite\n'
2023-08-05 01:00:46 -04:00
'---- END VEILIDCHAT CONTACT INVITE -----\n';
2023-08-04 01:00:38 -04:00
}
2023-08-01 00:39:50 -04:00
@override
2023-08-02 21:09:28 -04:00
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
final theme = Theme.of(context);
2023-08-05 23:58:13 -04:00
//final scale = theme.extension<ScaleScheme>()!;
2023-08-04 01:00:38 -04:00
final textTheme = theme.textTheme;
2024-01-29 22:38:19 -05:00
final signedContactInvitationBytesV =
context.watch<InvitationGeneratorCubit>().state;
2023-09-22 11:20:28 -04:00
final cardsize =
min<double>(MediaQuery.of(context).size.shortestSide - 48.0, 400);
2023-08-02 21:09:28 -04:00
return Dialog(
backgroundColor: Colors.white,
2023-09-22 11:20:28 -04:00
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth: cardsize,
maxWidth: cardsize,
minHeight: cardsize,
maxHeight: cardsize),
2023-08-03 00:49:48 -04:00
child: signedContactInvitationBytesV.when(
2024-02-11 14:17:10 -05:00
loading: buildProgressIndicator,
2024-01-29 22:38:19 -05:00
data: (data) => Form(
key: formKey,
child: Column(children: [
FittedBox(
child: Text(
translate(
'send_invite_dialog.contact_invitation'),
style: textTheme.headlineSmall!
.copyWith(color: Colors.black)))
.paddingAll(8),
FittedBox(
child: QrImageView.withQr(
size: 300,
qr: QrCode.fromUint8List(
data: data,
errorCorrectLevel:
QrErrorCorrectLevel.L)))
.expanded(),
Text(widget.message,
softWrap: true,
style: textTheme.labelLarge!
.copyWith(color: Colors.black))
.paddingAll(8),
ElevatedButton.icon(
icon: const Icon(Icons.copy),
label: Text(
translate('send_invite_dialog.copy_invitation')),
onPressed: () async {
showInfoToast(
context,
translate(
'send_invite_dialog.invitation_copied'));
await Clipboard.setData(ClipboardData(
text: makeTextInvite(widget.message, data)));
},
).paddingAll(16),
])),
2024-02-29 14:37:50 -05:00
error: errorPage)));
2023-08-03 00:49:48 -04:00
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty<FocusNode>('focusNode', focusNode))
..add(DiagnosticsProperty<GlobalKey<FormState>>('formKey', formKey));
2023-08-01 00:39:50 -04:00
}
}