Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix focus loss when text field is present #46

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:label="qrcode_barcode_scanner_example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:label="qrcode_barcode_scanner_example">
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:exported="true"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
Expand All @@ -37,8 +37,8 @@
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
<action android:name="android.intent.action.PROCESS_TEXT" />
<data android:mimeType="text/plain" />
</intent>
</queries>
</manifest>
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
2 changes: 1 addition & 1 deletion example/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version '8.3.1' apply false
id "com.android.application" version '8.4.0' apply false
id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

Expand Down
14 changes: 11 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ class _MyAppState extends State<MyApp> {
children: [
Expanded(
child: Center(
child: Text(
"Scan value: ${_scanValue ?? 'none'}",
style: const TextStyle(fontSize: 30),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Scan value: ${_scanValue ?? 'none'}",
style: const TextStyle(fontSize: 30),
),
TextFormField(
keyboardType: TextInputType.text,
),
],
),
),
),
Expand Down
40 changes: 20 additions & 20 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ packages:
dependency: "direct main"
description:
name: cupertino_icons
sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d
sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6
url: "https://pub.dev"
source: hosted
version: "1.0.6"
version: "1.0.8"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -79,10 +79,10 @@ packages:
dependency: "direct dev"
description:
name: flutter_lints
sha256: e2a421b7e59244faef694ba7b30562e489c2b489866e505074eb005cd7060db7
sha256: "3f41d009ba7172d5ff9be5f6e6e6abb4300e263aab8866d2a0842ed2a70f8f0c"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
version: "4.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -102,34 +102,34 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
version: "10.0.4"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.3"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.1"
lints:
dependency: transitive
description:
name: lints
sha256: cbf8d4b858bb0134ef3ef87841abdf8d63bfc255c266b7bf6b39daa1085c4290
sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
version: "4.0.0"
matcher:
dependency: transitive
description:
Expand All @@ -150,10 +150,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.12.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -192,7 +192,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.0"
version: "3.0.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -250,10 +250,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
version: "0.7.0"
vector_math:
dependency: transitive
description:
Expand All @@ -266,10 +266,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
url: "https://pub.dev"
source: hosted
version: "13.0.0"
version: "14.2.1"
webdriver:
dependency: transitive
description:
Expand All @@ -280,4 +280,4 @@ packages:
version: "3.0.3"
sdks:
dart: ">=3.3.1 <4.0.0"
flutter: ">=3.3.0"
flutter: ">=3.18.0-18.0.pre.54"
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6
cupertino_icons: ^1.0.8

dev_dependencies:
integration_test:
Expand All @@ -40,7 +40,7 @@ dev_dependencies:
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^3.0.0
flutter_lints: ^4.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
41 changes: 31 additions & 10 deletions lib/qrcode_barcode_scanner.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qrcode_barcode_scanner/qrcode_barcode_scanner_platform_interface.dart';

Expand All @@ -10,10 +10,10 @@ import 'delayed_action_handler.dart';
/// [scannedCode] is the string representing the scanned barcode.
typedef ScannedCallback = void Function(String scannedCode);

/// Duration of 100 milliseconds.
/// Duration representing 100 milliseconds.
const Duration hundredMs = Duration(milliseconds: 100);

/// A class that handles scanning of QR codes and barcodes.
/// A class responsible for scanning QR codes and barcodes.
///
/// The [QrcodeBarcodeScanner] class listens to keyboard events to scan QR codes
/// and barcodes. It uses a [StreamController] to listen to keyboard events and
Expand Down Expand Up @@ -41,7 +41,10 @@ class QrcodeBarcodeScanner {
/// that handles scanned barcodes.
QrcodeBarcodeScanner({required this.onScannedCallback})
: _actionHandler = DelayedActionHandler(hundredMs) {
// Add keyboard listener
HardwareKeyboard.instance.addHandler(_keyBoardCallback);

// Listen to keyboard events
_controller.stream.where((char) => char != null).listen(onKeyEvent);
}

Expand All @@ -55,7 +58,7 @@ class QrcodeBarcodeScanner {
_pressedKeys.add(readChar);
_actionHandler.executeDelayed(() {
final String scannedCode =
_pressedKeys.isNotEmpty ? _pressedKeys.join() : "";
_pressedKeys.isNotEmpty ? _pressedKeys.join() : "";
onScannedCallback(scannedCode.trim());
_pressedKeys.clear();
});
Expand All @@ -66,14 +69,30 @@ class QrcodeBarcodeScanner {
///
/// [event] is the raw keyboard event that occurred.
bool _keyBoardCallback(KeyEvent event) {
if (event.character != "" &&
event.character != null &&
(List.of(event.character?.codeUnits ?? [])
..removeWhere((element) => element == 0))
.isNotEmpty) {
_controller.add(event.character ?? "");
if (_isTextInputFocused()) {
return false; // Bypass scanner logic
}

// Check if the event contains a character and add it to the controller
if (event.character != null &&
event.character!.isNotEmpty &&
event.character!.codeUnits.any((unit) => unit != 0)) {
_controller.add(event.character);
return true;
}

return false;
}

/// Checks if a text input field is focused.
bool _isTextInputFocused() {
final focus = FocusManager.instance.primaryFocus;
if (focus != null && focus.context != null) {
final context = focus.context!;
return context.findAncestorWidgetOfExactType<EditableText>() != null ||
context.findAncestorWidgetOfExactType<TextField>() != null ||
context.findAncestorWidgetOfExactType<TextFormField>() != null;
}
return false;
}

Expand All @@ -82,7 +101,9 @@ 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() {
// Remove keyboard listener
HardwareKeyboard.instance.removeHandler(_keyBoardCallback);
// Close stream controller
_controller.close();
}
}
Loading
Loading