sdk_nfc_flutter 4.7.1 copy "sdk_nfc_flutter: ^4.7.1" to clipboard
sdk_nfc_flutter: ^4.7.1 copied to clipboard

A flutter plugin for the Au10tix NFC module SDK

example/lib/main.dart

// ignore_for_file: use_key_in_widget_constructors

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sdk_core_flutter/sdk_core_flutter.dart';
import 'package:sdk_core_flutter/ui_config.dart';
import 'package:sdk_nfc_flutter/sdk_nfc_flutter.dart';

import './constants.dart';
import './nfc_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Au10tix Flutter NFC Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
      routes: {
        NFCPage.routeName: (ctx) => NFCPage(),
      },
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  static const String _authToken = workflowResponse;
  static var isPrepared = false;
  NFCScannerType _scannerType = NFCScannerType.mrz; // Scanner type for UI flow only

  Future<void> _prepareSDK(BuildContext context) async {
    try {
      final result = await Au10tix.init(_authToken);
      if (result.containsKey("init")) {
        isPrepared = true;
        _showToast(context, result["init"].toString(), Colors.green);
      }
    } on PlatformException catch (error) {
      isPrepared = false;
      _showToast(context, error.message!, Colors.red);
    } catch (error) {
      isPrepared = false;
      _showToast(context, "Unknown error occurred", Colors.red);
    }
  }

  Future<void> _startNfcUI(bool isId) async {
    if (!isPrepared) {
      _showToast(context, "Please prepare the SDK first", Colors.blue);
      return;
    }
    try {
      final result = await SdkNfcFlutter.startUI(
        isID: isId,
        scannerType: _scannerType,
        uiConfig: UIConfig(
          showIntroScreen: true,
          showCloseButton: true,
          showPrimaryButton: true,
          canUpload: true,
        ),
      );
      if (kDebugMode) {
        print(result.toString());
      }
    } on PlatformException catch (error) {
      if (kDebugMode) {
        print(error.message);
      }
    } catch (error) {
      if (kDebugMode) {
        print("Error: $error");
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin NFC example app'),
      ),
      body: Column(
        children: [
          // Scanner Type Toggle (for UI flow only) - at the top
          Container(
            width: double.infinity,
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
            decoration: BoxDecoration(
              color: Colors.blue[50],
              border: Border(
                bottom: BorderSide(color: Colors.blue[200]!, width: 2),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const Text(
                  'UI Scanner Type:',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: Colors.black87,
                  ),
                ),
                Row(
                  children: [
                    Text(
                      _scannerType == NFCScannerType.mrz ? 'MRZ' : 'SDC',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                        color: Colors.blue[700],
                      ),
                    ),
                    const SizedBox(width: 8),
                    Switch(
                      value: _scannerType == NFCScannerType.sdc,
                      onChanged: (value) {
                        setState(() {
                          _scannerType = value ? NFCScannerType.sdc : NFCScannerType.mrz;
                        });
                      },
                      activeColor: Colors.blue,
                    ),
                  ],
                ),
              ],
            ),
          ),
          // Main content centered
          Expanded(
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () => _prepareSDK(context),
                    child: const Text("Prepare SDK"),
                  ),
                  ElevatedButton(
                    onPressed: () => Navigator.of(context)
                        .pushNamed(NFCPage.routeName, arguments: {"isID": false}),
                    child: const Text("Start NFC - Passport"),
                  ),
                  ElevatedButton(
                    onPressed: () => Navigator.of(context)
                        .pushNamed(NFCPage.routeName, arguments: {"isID": true}),
                    child: const Text("Start NFC - ID Card"),
                  ),
                  ElevatedButton(
                    onPressed: () => _startNfcUI(false),
                    child: const Text("Start Passport UI"),
                  ),
                  ElevatedButton(
                    onPressed: () => _startNfcUI(true),
                    child: const Text("Start NFC UI"),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

void _showToast(BuildContext context, String message, Color bgColor) {
  final scaffold = ScaffoldMessenger.of(context);
  scaffold.showSnackBar(
    SnackBar(
      content: Text(message),
      backgroundColor: bgColor,
    ),
  );
}