Initial commit
This commit is contained in:
144
lib/widgets/qr_code_viewer.dart
Normal file
144
lib/widgets/qr_code_viewer.dart
Normal file
@@ -0,0 +1,144 @@
|
||||
// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
|
||||
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import 'package:tatchat/config/app_config.dart';
|
||||
import 'package:tatchat/l10n/l10n.dart';
|
||||
import 'package:tatchat/utils/tatu_share.dart';
|
||||
import 'package:tatchat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
|
||||
import 'package:tatchat/widgets/future_loading_dialog.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:image/image.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:particles_network/particles_network.dart';
|
||||
import 'package:pretty_qr_code/pretty_qr_code.dart';
|
||||
import 'package:qr_image/qr_image.dart';
|
||||
|
||||
import '../config/themes.dart';
|
||||
|
||||
Future<void> showQrCodeViewer(BuildContext context, String content) =>
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => QrCodeViewer(content: content),
|
||||
);
|
||||
|
||||
class QrCodeViewer extends StatelessWidget {
|
||||
final String content;
|
||||
|
||||
const QrCodeViewer({required this.content, super.key});
|
||||
|
||||
Future<void> _save(BuildContext context) async {
|
||||
final imageResult = await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () {
|
||||
final inviteLink = 'https://matrix.to/#/$content';
|
||||
final image = QRImage(inviteLink, size: 256, radius: 1).generate();
|
||||
return compute(encodePng, image);
|
||||
},
|
||||
);
|
||||
final bytes = imageResult.result;
|
||||
if (bytes == null) return;
|
||||
if (!context.mounted) return;
|
||||
|
||||
MatrixImageFile(
|
||||
bytes: bytes,
|
||||
name: 'QR_Code_$content.png',
|
||||
mimeType: 'image/png',
|
||||
).save(context);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final inviteLink = 'https://matrix.to/#/$content';
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black.withAlpha(128),
|
||||
extendBodyBehindAppBar: true,
|
||||
appBar: AppBar(
|
||||
elevation: 0,
|
||||
leading: IconButton(
|
||||
style: IconButton.styleFrom(
|
||||
backgroundColor: Colors.black.withAlpha(128),
|
||||
),
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: Navigator.of(context).pop,
|
||||
color: Colors.white,
|
||||
tooltip: L10n.of(context).close,
|
||||
),
|
||||
backgroundColor: Colors.transparent,
|
||||
actions: [
|
||||
IconButton(
|
||||
style: IconButton.styleFrom(
|
||||
backgroundColor: Colors.black.withAlpha(128),
|
||||
),
|
||||
icon: Icon(Icons.adaptive.share_outlined),
|
||||
onPressed: () => TatuShare.share(inviteLink, context),
|
||||
color: Colors.white,
|
||||
tooltip: L10n.of(context).share,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
IconButton(
|
||||
style: IconButton.styleFrom(
|
||||
backgroundColor: Colors.black.withAlpha(128),
|
||||
),
|
||||
icon: const Icon(Icons.download_outlined),
|
||||
onPressed: () => _save(context),
|
||||
color: Colors.white,
|
||||
tooltip: L10n.of(context).downloadFile,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
),
|
||||
body: Stack(
|
||||
children: [
|
||||
if (!MediaQuery.disableAnimationsOf(context))
|
||||
ParticleNetwork(maxSpeed: 0.25),
|
||||
Center(
|
||||
child: Container(
|
||||
margin: const EdgeInsets.all(32.0),
|
||||
padding: const EdgeInsets.all(32.0),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: theme.colorScheme.onSurface,
|
||||
width: 8,
|
||||
),
|
||||
color: theme.colorScheme.surfaceBright,
|
||||
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: .min,
|
||||
children: [
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(
|
||||
maxWidth: FluffyThemes.columnWidth,
|
||||
),
|
||||
child: PrettyQrView.data(
|
||||
data: inviteLink,
|
||||
decoration: PrettyQrDecoration(
|
||||
shape: PrettyQrSmoothSymbol(
|
||||
roundFactor: 1,
|
||||
color: theme.colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8.0),
|
||||
SelectableText(
|
||||
content,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: theme.colorScheme.onPrimaryContainer,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user