2022-10-04 13:12:54 +03:00
|
|
|
/*
|
2023-09-21 12:40:41 +03:00
|
|
|
* Copyright (C) 2022-2023 Yubico.
|
2022-10-04 13:12:54 +03:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-10-10 14:49:01 +03:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
2022-08-05 11:40:36 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2023-02-28 13:34:29 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-10-10 14:49:01 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:yubico_authenticator/app/state.dart';
|
2022-08-05 11:40:36 +03:00
|
|
|
|
2022-09-12 13:58:17 +03:00
|
|
|
import '../keys.dart' as keys;
|
2022-08-05 11:40:36 +03:00
|
|
|
import 'qr_scanner_scan_status.dart';
|
|
|
|
|
2023-10-10 14:49:01 +03:00
|
|
|
class QRScannerUI extends ConsumerWidget {
|
2022-08-05 11:40:36 +03:00
|
|
|
final ScanStatus status;
|
|
|
|
final Size screenSize;
|
2023-09-21 12:40:41 +03:00
|
|
|
final GlobalKey overlayWidgetKey;
|
2022-08-05 11:40:36 +03:00
|
|
|
|
2023-09-21 12:40:41 +03:00
|
|
|
const QRScannerUI(
|
|
|
|
{super.key,
|
|
|
|
required this.status,
|
|
|
|
required this.screenSize,
|
|
|
|
required this.overlayWidgetKey});
|
2022-08-05 11:40:36 +03:00
|
|
|
|
|
|
|
@override
|
2023-10-10 14:49:01 +03:00
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2023-02-28 13:34:29 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-08-05 11:40:36 +03:00
|
|
|
|
2023-09-21 12:40:41 +03:00
|
|
|
return Stack(
|
|
|
|
fit: StackFit.expand,
|
|
|
|
children: [
|
|
|
|
SafeArea(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
left: 16, right: 16, top: 0, bottom: 0),
|
|
|
|
child: SizedBox(
|
|
|
|
// other widgets can find the RenderObject of this
|
|
|
|
// widget by its key value and query its size and offset.
|
|
|
|
key: overlayWidgetKey,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 0.0),
|
2023-02-28 13:34:29 +03:00
|
|
|
child: Text(
|
2023-09-21 12:40:41 +03:00
|
|
|
status != ScanStatus.error
|
|
|
|
? l10n.l_point_camera_scan
|
|
|
|
: l10n.l_invalid_qr,
|
2023-02-28 13:34:29 +03:00
|
|
|
style: const TextStyle(color: Colors.white),
|
2023-09-21 12:40:41 +03:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
l10n.q_no_qr,
|
|
|
|
textScaleFactor: 0.7,
|
|
|
|
style: const TextStyle(color: Colors.white),
|
|
|
|
),
|
2023-10-10 14:49:01 +03:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: [
|
|
|
|
OutlinedButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop('');
|
|
|
|
},
|
|
|
|
key: keys.manualEntryButton,
|
|
|
|
child: Text(
|
|
|
|
l10n.s_enter_manually,
|
|
|
|
style: const TextStyle(color: Colors.white),
|
|
|
|
)),
|
|
|
|
OutlinedButton(
|
|
|
|
onPressed: () async {
|
|
|
|
Navigator.of(context).pop('');
|
|
|
|
final result = await FilePicker.platform.pickFiles(
|
|
|
|
allowedExtensions: ['png', 'jpg'],
|
|
|
|
type: FileType.custom,
|
|
|
|
allowMultiple: false,
|
|
|
|
lockParentWindow: true,
|
|
|
|
dialogTitle: 'Select file with QR code');
|
|
|
|
if (result != null && result.files.isNotEmpty) {
|
|
|
|
final fileWithCode = result.files.first;
|
|
|
|
final bytes = fileWithCode.bytes;
|
|
|
|
if (bytes == null || bytes.isEmpty) {
|
|
|
|
//err return
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bytes.length > 3 * 1024 * 1024) {
|
|
|
|
// too big file
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final scanner = ref.read(qrScannerProvider);
|
|
|
|
if (scanner != null) {
|
|
|
|
await scanner.scanQr(base64UrlEncode(bytes));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
key: keys.readFromImage,
|
|
|
|
child: Text(
|
|
|
|
l10n.s_read_from_image,
|
|
|
|
style: const TextStyle(color: Colors.white),
|
|
|
|
)),
|
|
|
|
],
|
|
|
|
),
|
2023-09-21 12:40:41 +03:00
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 8)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
2022-08-05 11:40:36 +03:00
|
|
|
}
|
|
|
|
}
|