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

added support for android extern intent result #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions zxscanner/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
Expand Down
2 changes: 2 additions & 0 deletions zxscanner/lib/configs/app_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const String isVibrationOnPref = 'isVibrationOnPref';
const String languagePref = 'languagePref';

abstract class AppStoreBase with Store {
static bool isExternCall = false;

@observable
ThemeMode themeMode = ThemeMode.system;

Expand Down
19 changes: 19 additions & 0 deletions zxscanner/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_zxing/flutter_zxing.dart';
import 'package:receive_intent/receive_intent.dart';

import 'configs/app_store.dart';
import 'configs/app_theme.dart';
Expand Down Expand Up @@ -31,6 +33,23 @@ class MyApp extends StatefulWidget {

class _MyAppState extends State<MyApp> {
final AppRouter _appRouter = AppRouter();

@override
void initState() {
super.initState();
_init();
}

Future<void> _init() async {
if (Platform.isAndroid) {
final receivedIntent = await ReceiveIntent.getInitialIntent();
if(receivedIntent?.fromPackageName != null){
AppStoreBase.isExternCall = true;
}else{
AppStoreBase.isExternCall = false;
}
}
}

@override
Widget build(BuildContext context) {
Expand Down
8 changes: 8 additions & 0 deletions zxscanner/lib/pages/scanner_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:flutter_zxing/flutter_zxing.dart';
import 'package:receive_intent/receive_intent.dart';

import '../configs/app_store.dart';
import '../models/models.dart' as model;
import '../utils/db_service.dart';
import '../utils/extensions.dart';
Expand Down Expand Up @@ -34,5 +37,10 @@ class _ScannerPageState extends State<ScannerPage> {
final model.Code code = model.Code.fromCodeResult(result);
DbService.instance.addCode(code);
context.showToast('Barcode saved:\n${code.text ?? ''}');
if (Platform.isAndroid) {
if(AppStoreBase.isExternCall) {
ReceiveIntent.setResult(kActivityResultOk, data: {"SCAN_RESULT": "${code.text ?? ''}"}, shouldFinish:true);
}
}
}
}
1 change: 1 addition & 0 deletions zxscanner/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies:
share_plus: ^7.0.0
shared_preferences: ^2.0.0
url_launcher: ^6.1.11
receive_intent: any

flutter_intl:
main_locale: en_US
Expand Down