|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
| 3 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 4 | + |
| 5 | +import '../../../providers/global/barcode_scanner_provider.dart'; |
| 6 | +import '../../../themes/stack_colors.dart'; |
| 7 | +import '../../../utilities/barcode_scanner_interface.dart'; |
| 8 | +import '../../../utilities/clipboard_interface.dart'; |
| 9 | +import '../../../utilities/constants.dart'; |
| 10 | +import '../../../utilities/logger.dart'; |
| 11 | +import '../../../utilities/text_styles.dart'; |
| 12 | +import '../../../widgets/desktop/primary_button.dart'; |
| 13 | +import '../../../widgets/desktop/secondary_button.dart'; |
| 14 | +import '../../../widgets/icon_widgets/clipboard_icon.dart'; |
| 15 | +import '../../../widgets/icon_widgets/qrcode_icon.dart'; |
| 16 | +import '../../../widgets/icon_widgets/x_icon.dart'; |
| 17 | +import '../../../widgets/stack_dialog.dart'; |
| 18 | +import '../../../widgets/stack_text_field.dart'; |
| 19 | +import '../../../widgets/textfield_icon_button.dart'; |
| 20 | + |
| 21 | +class SlatepackEntryDialog extends ConsumerStatefulWidget { |
| 22 | + const SlatepackEntryDialog({ |
| 23 | + super.key, |
| 24 | + this.clipboard = const ClipboardWrapper(), |
| 25 | + }); |
| 26 | + |
| 27 | + final ClipboardInterface clipboard; |
| 28 | + |
| 29 | + @override |
| 30 | + ConsumerState<SlatepackEntryDialog> createState() => |
| 31 | + _SlatepackEntryDialogState(); |
| 32 | +} |
| 33 | + |
| 34 | +class _SlatepackEntryDialogState extends ConsumerState<SlatepackEntryDialog> { |
| 35 | + final _receiveSlateController = TextEditingController(); |
| 36 | + final _slateFocusNode = FocusNode(); |
| 37 | + |
| 38 | + bool _slateToggleFlag = false; |
| 39 | + |
| 40 | + Future<void> _pasteSlatepack() async { |
| 41 | + final ClipboardData? data = await widget.clipboard.getData( |
| 42 | + Clipboard.kTextPlain, |
| 43 | + ); |
| 44 | + if (data?.text != null && data!.text!.isNotEmpty) { |
| 45 | + _receiveSlateController.text = data.text!; |
| 46 | + setState(() { |
| 47 | + _slateToggleFlag = _receiveSlateController.text.isNotEmpty; |
| 48 | + }); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + Future<void> _scanQr() async { |
| 53 | + try { |
| 54 | + if (_slateFocusNode.hasFocus) { |
| 55 | + _slateFocusNode.unfocus(); |
| 56 | + await Future<void>.delayed(const Duration(milliseconds: 75)); |
| 57 | + } |
| 58 | + |
| 59 | + if (mounted) { |
| 60 | + final qrResult = await ref.read(pBarcodeScanner).scan(context: context); |
| 61 | + if (qrResult.rawContent.isNotEmpty && qrResult.rawContent != "null") { |
| 62 | + _receiveSlateController.text = qrResult.rawContent; |
| 63 | + setState(() { |
| 64 | + _slateToggleFlag = _receiveSlateController.text.isNotEmpty; |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + } on PlatformException catch (e, s) { |
| 69 | + if (mounted) { |
| 70 | + try { |
| 71 | + await checkCamPermDeniedMobileAndOpenAppSettings( |
| 72 | + context, |
| 73 | + logging: Logging.instance, |
| 74 | + ); |
| 75 | + } catch (e, s) { |
| 76 | + Logging.instance.e( |
| 77 | + "Failed to check cam permissions", |
| 78 | + error: e, |
| 79 | + stackTrace: s, |
| 80 | + ); |
| 81 | + } |
| 82 | + } else { |
| 83 | + Logging.instance.e( |
| 84 | + "Failed to get camera permissions while trying to scan qr code in SendView: ", |
| 85 | + error: e, |
| 86 | + stackTrace: s, |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @override |
| 93 | + void dispose() { |
| 94 | + _receiveSlateController.dispose(); |
| 95 | + _slateFocusNode.dispose(); |
| 96 | + super.dispose(); |
| 97 | + } |
| 98 | + |
| 99 | + @override |
| 100 | + Widget build(BuildContext context) { |
| 101 | + return StackDialogBase( |
| 102 | + child: Column( |
| 103 | + mainAxisSize: MainAxisSize.min, |
| 104 | + children: [ |
| 105 | + Text( |
| 106 | + "Receive Slatepack", |
| 107 | + style: STextStyles.desktopTextExtraSmall(context).copyWith( |
| 108 | + color: |
| 109 | + Theme.of( |
| 110 | + context, |
| 111 | + ).extension<StackColors>()!.textFieldActiveSearchIconRight, |
| 112 | + ), |
| 113 | + textAlign: TextAlign.left, |
| 114 | + ), |
| 115 | + const SizedBox(height: 12), |
| 116 | + ClipRRect( |
| 117 | + borderRadius: BorderRadius.circular( |
| 118 | + Constants.size.circularBorderRadius, |
| 119 | + ), |
| 120 | + child: TextField( |
| 121 | + minLines: 1, |
| 122 | + maxLines: 5, |
| 123 | + key: const Key("receiveViewSlatepackFieldKey"), |
| 124 | + controller: _receiveSlateController, |
| 125 | + readOnly: false, |
| 126 | + autocorrect: false, |
| 127 | + enableSuggestions: false, |
| 128 | + toolbarOptions: const ToolbarOptions( |
| 129 | + copy: false, |
| 130 | + cut: false, |
| 131 | + paste: true, |
| 132 | + selectAll: false, |
| 133 | + ), |
| 134 | + onChanged: (newValue) { |
| 135 | + setState(() { |
| 136 | + _slateToggleFlag = newValue.isNotEmpty; |
| 137 | + }); |
| 138 | + }, |
| 139 | + focusNode: _slateFocusNode, |
| 140 | + style: STextStyles.desktopTextExtraSmall(context).copyWith( |
| 141 | + color: |
| 142 | + Theme.of( |
| 143 | + context, |
| 144 | + ).extension<StackColors>()!.textFieldActiveText, |
| 145 | + height: 1.8, |
| 146 | + ), |
| 147 | + decoration: standardInputDecoration( |
| 148 | + "Enter Slatepack Message", |
| 149 | + _slateFocusNode, |
| 150 | + context, |
| 151 | + desktopMed: true, |
| 152 | + ).copyWith( |
| 153 | + contentPadding: const EdgeInsets.symmetric( |
| 154 | + horizontal: 16, |
| 155 | + vertical: 12, // Adjust vertical padding for better alignment |
| 156 | + ), |
| 157 | + suffixIcon: Padding( |
| 158 | + padding: |
| 159 | + _receiveSlateController.text.isEmpty |
| 160 | + ? const EdgeInsets.only(right: 8) |
| 161 | + : const EdgeInsets.only(right: 0), |
| 162 | + child: UnconstrainedBox( |
| 163 | + child: Row( |
| 164 | + mainAxisAlignment: MainAxisAlignment.spaceAround, |
| 165 | + children: [ |
| 166 | + _slateToggleFlag |
| 167 | + ? TextFieldIconButton( |
| 168 | + key: const Key( |
| 169 | + "receiveViewClearSlatepackFieldButtonKey", |
| 170 | + ), |
| 171 | + onTap: () { |
| 172 | + _receiveSlateController.text = ""; |
| 173 | + setState(() { |
| 174 | + _slateToggleFlag = false; |
| 175 | + }); |
| 176 | + }, |
| 177 | + child: const XIcon(), |
| 178 | + ) |
| 179 | + : TextFieldIconButton( |
| 180 | + key: const Key( |
| 181 | + "receiveViewPasteSlatepackFieldButtonKey", |
| 182 | + ), |
| 183 | + onTap: _pasteSlatepack, |
| 184 | + child: |
| 185 | + _receiveSlateController.text.isEmpty |
| 186 | + ? const ClipboardIcon() |
| 187 | + : const XIcon(), |
| 188 | + ), |
| 189 | + if (_receiveSlateController.text.isEmpty) |
| 190 | + TextFieldIconButton( |
| 191 | + semanticsLabel: |
| 192 | + "Scan QR Button. Opens Camera For Scanning QR Code.", |
| 193 | + key: const Key("sendViewScanQrButtonKey"), |
| 194 | + onTap: _scanQr, |
| 195 | + child: const QrCodeIcon(), |
| 196 | + ), |
| 197 | + ], |
| 198 | + ), |
| 199 | + ), |
| 200 | + ), |
| 201 | + ), |
| 202 | + ), |
| 203 | + ), |
| 204 | + const SizedBox(height: 16), |
| 205 | + PrimaryButton( |
| 206 | + label: "Import", |
| 207 | + enabled: _slateToggleFlag, |
| 208 | + onPressed: |
| 209 | + !_slateToggleFlag |
| 210 | + ? null |
| 211 | + : () => |
| 212 | + Navigator.of(context).pop(_receiveSlateController.text), |
| 213 | + ), |
| 214 | + const SizedBox(height: 16), |
| 215 | + SecondaryButton( |
| 216 | + label: "Cancel", |
| 217 | + onPressed: Navigator.of(context).pop, |
| 218 | + ), |
| 219 | + ], |
| 220 | + ), |
| 221 | + ); |
| 222 | + } |
| 223 | +} |
0 commit comments