mxnfc 0.0.7 copy "mxnfc: ^0.0.7" to clipboard
mxnfc: ^0.0.7 copied to clipboard

1.优化获取NFC开关状态 2.增加初始化接口 3.后台常驻接口

example/lib/main.dart

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mxnfc/mxnfc.dart';
import 'package:mxnfc/mxnfcenum.dart';
import 'package:mxnfc/nfcresultmodel.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _mxnfcPlugin = Mxnfc.instance;
  String uuidMsg = "";

  @override
  void initState() {
    super.initState();
 //   initPlatformState();
   // readNFC();
  }

  readNFC(){
Mxnfc.instance.startScanNfc("贴近手臂顶部");
Mxnfc.instance.onMessageRecive(sucCallBack:(message) {
  if(message is String){
    print("message:$message");
    Map<String,dynamic> map = jsonDecode(message);
    if(map["key"]  == "success"){
      if(map["action"] == "startScanNfc"){ //扫描开始
          
      }else if(map["action"] == "read"){ //读取成功
        
      }
    }else{
      if(map["key"] == "error"){
       if(map["action"] == "startScanNfc"){ //扫描开始
          if(map["type"] == "noSupport"){ //不支持

          }else if(map["type"] == "timeout"){ //超时

          }
      }else if(map["action"] == "read"){ //失败
         
      }
      }
    }

    
  }
}, errCallBack:(error) {
  print("error:$error");
},);
 
}

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _mxnfcPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('info:$uuidMsg'),
              const SizedBox(height: 50,),
              ElevatedButton(onPressed: (){
                 Mxnfc.instance.closeNFCMessage();
                 setState(() {
                   uuidMsg = "";
                 });
              }, child: const Text("关闭NFC")),
              ElevatedButton(onPressed: (){
                 Mxnfc.instance.openNFCSettings();
              }, child: const Text("去打开NFC")),

               ElevatedButton(onPressed: ()async{
                int state = await Mxnfc.instance.getNFCSwitchState();
                 print("我的开关状态:$state");
                 setState(() {
                   uuidMsg = "$state";
                 });
              }, child: const Text("开关状态")),
           
            ],
          ),
        ),
          floatingActionButton:FloatingActionButton(
          child: const Text("开始扫描"),
          onPressed: (){
              Mxnfc.instance.scanNfc(message: "贴近手臂底部",successCallBack:(m) {
              if(m is NFCResultModel){
                print("->>m:${m.msg}");
                setState(() {
                   uuidMsg = m.uuid;
                });
              }
              }, failedCallBack:(m) {
                if(m is NFCResultModel){
                  if(m.resultType == NFCScanResult.isOpen){
                      Mxnfc.instance.settNFCBecomeEnableForeground();
                  }else{
                        NFCResultModel m1 = m;
                     print("->>m:$m1 type:${m1.resultType}");
                     setState(() {
                       uuidMsg = m1.msg;
                     });
                  }
               

                    //  Future.delayed(const Duration(seconds: 2)).then((value) {
                    //    Mxnfc.instance.openNFCSettings();
                    //  });

                }
                
              },);
          }),
      ),
    );
  }
}
1
likes
0
points
1
downloads

Publisher

unverified uploader

Weekly Downloads

1.优化获取NFC开关状态 2.增加初始化接口 3.后台常驻接口

Homepage

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on mxnfc

Packages that implement mxnfc