diff --git a/example/lib/main.dart b/example/lib/main.dart index 613464e..b66425c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -15,15 +15,21 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { String? _scanValue; + void setScannedValue(String value) { + debugPrint("$value ${value.length}"); + setState(() { + _scanValue = value; + }); + } + @override void initState() { super.initState(); QrcodeBarcodeScanner( - onScannedCallback: (String value) => setState( - () { - _scanValue = value; - }, - ), + onScannedCallback: (String value) { + // debugPrint("Scan value vase64: '${utf8.decode(base64.decode(value))}'"); + setScannedValue(value); + }, ); } @@ -34,11 +40,25 @@ class _MyAppState extends State { appBar: AppBar( title: const Text('Plugin example app'), ), - body: Center( - child: Text( - 'Scan value: ${_scanValue ?? "none"}', - style: const TextStyle(fontSize: 30), - ), + body: Column( + children: [ + Expanded( + child: Center( + child: Text( + _scanValue ?? 'none', + style: const TextStyle(fontSize: 30), + ), + ), + ), + ElevatedButton( + onPressed: () { + setState(() { + _scanValue = null; + }); + }, + child: const Text("Clear scanned"), + ) + ], ), ), ); diff --git a/example/pubspec.lock b/example/pubspec.lock index 9465c0d..b63e25a 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -37,18 +37,18 @@ packages: dependency: transitive description: name: collection - sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.2" + version: "1.18.0" cupertino_icons: dependency: "direct main" description: name: cupertino_icons - sha256: e35129dc44c9118cee2a5603506d823bab99c68393879edb440e0090d07586be + sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d url: "https://pub.dev" source: hosted - version: "1.0.5" + version: "1.0.6" fake_async: dependency: transitive description: @@ -61,10 +61,10 @@ packages: dependency: transitive description: name: file - sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d" + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" url: "https://pub.dev" source: hosted - version: "6.1.4" + version: "7.0.0" flutter: dependency: "direct main" description: flutter @@ -79,10 +79,10 @@ packages: dependency: "direct dev" description: name: flutter_lints - sha256: "2118df84ef0c3ca93f96123a616ae8540879991b8b57af2f81b76a7ada49b2a4" + sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04 url: "https://pub.dev" source: hosted - version: "2.0.2" + version: "2.0.3" flutter_test: dependency: "direct dev" description: flutter @@ -98,6 +98,30 @@ packages: description: flutter source: sdk version: "0.0.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" + url: "https://pub.dev" + source: hosted + version: "10.0.0" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0 + url: "https://pub.dev" + source: hosted + version: "2.0.1" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 + url: "https://pub.dev" + source: hosted + version: "2.0.1" lints: dependency: transitive description: @@ -110,58 +134,58 @@ packages: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.11.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" platform: dependency: transitive description: name: platform - sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76" + sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec" url: "https://pub.dev" source: hosted - version: "3.1.0" + version: "3.1.4" plugin_platform_interface: dependency: transitive description: name: plugin_platform_interface - sha256: da3fdfeccc4d4ff2da8f8c556704c08f912542c5fb3cf2233ed75372384a034d + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" url: "https://pub.dev" source: hosted - version: "2.1.6" + version: "2.1.8" process: dependency: transitive description: name: process - sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09" + sha256: "21e54fd2faf1b5bdd5102afd25012184a6793927648ea81eea80552ac9405b32" url: "https://pub.dev" source: hosted - version: "4.2.4" + version: "5.0.2" qrcode_barcode_scanner: dependency: "direct main" description: @@ -186,18 +210,18 @@ packages: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -226,10 +250,10 @@ packages: dependency: transitive description: name: test_api - sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.6.1" vector_math: dependency: transitive description: @@ -242,26 +266,18 @@ packages: dependency: transitive description: name: vm_service - sha256: c620a6f783fa22436da68e42db7ebbf18b8c44b9a46ab911f666ff09ffd9153f - url: "https://pub.dev" - source: hosted - version: "11.7.1" - web: - dependency: transitive - description: - name: web - sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 url: "https://pub.dev" source: hosted - version: "0.1.4-beta" + version: "13.0.0" webdriver: dependency: transitive description: name: webdriver - sha256: "3c923e918918feeb90c4c9fdf1fe39220fa4c0e8e2c0fffaded174498ef86c49" + sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e" url: "https://pub.dev" source: hosted - version: "3.0.2" + version: "3.0.3" sdks: - dart: ">=3.1.0 <4.0.0" + dart: ">=3.2.0-0 <4.0.0" flutter: ">=3.3.0" diff --git a/lib/qrcode_barcode_scanner.dart b/lib/qrcode_barcode_scanner.dart index b04db50..e986190 100644 --- a/lib/qrcode_barcode_scanner.dart +++ b/lib/qrcode_barcode_scanner.dart @@ -31,86 +31,17 @@ class QrcodeBarcodeScanner { /// A delayed action handler to handle delayed events. final DelayedActionHandler _actionHandler; - /// A map that maps key labels to their corresponding normal and shift values. - final Map> _keyMappings = { - "a": {"normal": "a", "shift": "A"}, - "b": {"normal": "b", "shift": "B"}, - "c": {"normal": "c", "shift": "C"}, - "d": {"normal": "d", "shift": "D"}, - "e": {"normal": "e", "shift": "E"}, - "f": {"normal": "f", "shift": "F"}, - "g": {"normal": "g", "shift": "G"}, - "h": {"normal": "h", "shift": "H"}, - "i": {"normal": "i", "shift": "I"}, - "j": {"normal": "j", "shift": "J"}, - "k": {"normal": "k", "shift": "K"}, - "l": {"normal": "l", "shift": "L"}, - "m": {"normal": "m", "shift": "M"}, - "n": {"normal": "n", "shift": "N"}, - "o": {"normal": "o", "shift": "O"}, - "p": {"normal": "p", "shift": "P"}, - "q": {"normal": "q", "shift": "Q"}, - "r": {"normal": "r", "shift": "R"}, - "s": {"normal": "s", "shift": "S"}, - "t": {"normal": "t", "shift": "T"}, - "u": {"normal": "u", "shift": "U"}, - "v": {"normal": "v", "shift": "V"}, - "w": {"normal": "w", "shift": "W"}, - "x": {"normal": "x", "shift": "X"}, - "y": {"normal": "y", "shift": "Y"}, - "z": {"normal": "z", "shift": "Z"}, - "1": {"normal": "1", "shift": "!"}, - "2": {"normal": "2", "shift": "@"}, - "3": {"normal": "3", "shift": "#"}, - "4": {"normal": "4", "shift": "\$"}, - "5": {"normal": "5", "shift": "%"}, - "6": {"normal": "6", "shift": "^"}, - "7": {"normal": "7", "shift": "&"}, - "8": {"normal": "8", "shift": "*"}, - "9": {"normal": "9", "shift": "("}, - "0": {"normal": "0", "shift": ")"}, - "`": {"normal": "`", "shift": "~"}, - "-": {"normal": "-", "shift": "_"}, - "=": {"normal": "=", "shift": "+"}, - "[": {"normal": "[", "shift": "{"}, - "]": {"normal": "]", "shift": "}"}, - "\\": {"normal": "\\", "shift": "|"}, - ";": {"normal": ";", "shift": ":"}, - "'": {"normal": "'", "shift": "\""}, - ",": {"normal": ",", "shift": "<"}, - ".": {"normal": ".", "shift": ">"}, - "/": {"normal": "/", "shift": "?"}, - "Tab": {"normal": "\t", "shift": null}, - "Enter": {"normal": "\n", "shift": null}, - " ": {"normal": " ", "shift": " "}, - }; - Future getPlatformVersion() { return QrcodeBarcodeScannerPlatform.instance.getPlatformVersion(); } - /// Returns `true` if the [LogicalKeyboardKey] is the shift key. - /// - /// [key] is the logical keyboard key to check. - bool isShift(LogicalKeyboardKey key) => key.synonyms.isNotEmpty - ? key.synonyms.first == LogicalKeyboardKey.shift - : false; - - /// Returns `true` if the [RawKeyEvent] is a key down event. - /// - /// [event] is the raw keyboard event to check. - bool isKeyDown(RawKeyEvent event) => event is RawKeyDownEvent; - - /// The current modifier for the shift key. - String _modifier = "normal"; - /// Creates a new instance of [QrcodeBarcodeScanner]. /// /// The [onScannedCallback] parameter is a required callback function /// that handles scanned barcodes. QrcodeBarcodeScanner({required this.onScannedCallback}) : _actionHandler = DelayedActionHandler(hundredMs) { - RawKeyboard.instance.addListener(_keyBoardCallback); + HardwareKeyboard.instance.addHandler(_keyBoardCallback); _controller.stream.where((char) => char != null).listen(onKeyEvent); } @@ -133,37 +64,17 @@ class QrcodeBarcodeScanner { /// The callback function that is called when a keyboard event occurs. /// - /// If [event] is a key down event, the corresponding key label is retrieved and added to the stream controller - /// using [_getKeyForLogicalKey]. If the key is a shift key, [_modifier] is set to "shift" for the next key event. - /// - /// If [event] is a key up event, and the key is a shift key, [_modifier] is set to "normal". - /// /// [event] is the raw keyboard event that occurred. - void _keyBoardCallback(RawKeyEvent event) { - final LogicalKeyboardKey logicalKey = event.logicalKey; - if (!isKeyDown(event)) { - if (isShift(logicalKey)) { - _modifier = "shift"; - } else { - final String? key = _getKeyForLogicalKey(logicalKey); - _controller.add(key); - _modifier = "normal"; - } + bool _keyBoardCallback(KeyEvent event) { + if (event.character != "" && + event.character != null && + (List.of(event.character?.codeUnits ?? []) + ..removeWhere((element) => element == 0)) + .isNotEmpty) { + _controller.add(event.character?.trim() ?? ""); + return true; } - } - - /// Returns the mapped key based on the given logical keyboard key [key]. - /// - /// The mapped key is obtained from the [_keyMappings] map. The key mappings map - /// maps each key label to a map that maps each modifier (e.g. "normal", - /// "shift") to a corresponding value. If the key label is not found in the - /// map, null is returned. - /// - /// [key] is the logical keyboard key. - String? _getKeyForLogicalKey(LogicalKeyboardKey key) { - final Map? mappedKey = - _keyMappings[key.keyLabel.toLowerCase()]; - return mappedKey?[_modifier]; + return false; } /// Disposes the resources used by the `QrcodeBarcodeScanner`. @@ -171,7 +82,7 @@ class QrcodeBarcodeScanner { /// Call this method when the `QrcodeBarcodeScanner` is no longer needed to release /// any resources (such as keyboard listeners) it may have acquired. void dispose() { - RawKeyboard.instance.removeListener(_keyBoardCallback); + HardwareKeyboard.instance.removeHandler(_keyBoardCallback); _controller.close(); } } diff --git a/pubspec.lock b/pubspec.lock index 6dd9b42..3bdcb83 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: collection - sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.2" + version: "1.18.0" fake_async: dependency: transitive description: @@ -67,6 +67,30 @@ packages: description: flutter source: sdk version: "0.0.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" + url: "https://pub.dev" + source: hosted + version: "10.0.0" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0 + url: "https://pub.dev" + source: hosted + version: "2.0.1" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 + url: "https://pub.dev" + source: hosted + version: "2.0.1" lints: dependency: transitive description: @@ -79,42 +103,42 @@ packages: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.11.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" plugin_platform_interface: dependency: "direct main" description: name: plugin_platform_interface - sha256: da3fdfeccc4d4ff2da8f8c556704c08f912542c5fb3cf2233ed75372384a034d + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" url: "https://pub.dev" source: hosted - version: "2.1.6" + version: "2.1.8" sky_engine: dependency: transitive description: flutter @@ -132,18 +156,18 @@ packages: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -164,10 +188,10 @@ packages: dependency: transitive description: name: test_api - sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.6.1" vector_math: dependency: transitive description: @@ -176,14 +200,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: + vm_service: dependency: transitive description: - name: web - sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + name: vm_service + sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 url: "https://pub.dev" source: hosted - version: "0.1.4-beta" + version: "13.0.0" sdks: - dart: ">=3.1.0 <4.0.0" + dart: ">=3.2.0-0 <4.0.0" flutter: ">=3.3.0"