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

修复android 有些机型闪退问题 原因是通知销毁导致的

example/lib/main.dart

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

import 'package:flutter/cupertino.dart';
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.sectionInfo??"";
                });
              }
              }, failedCallBack:(m) {
                if(m is NFCResultModel){
                  if(m.resultType == NFCScanResult.isOpen){
                      Mxnfc.instance.settNFCBecomeEnableForeground();
                  }else if(m.resultType == NFCScanResult.isNotOpen){
                      singleButtonDialog(content: "去设置", mycontext: context);
                  }
                  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();
                    //  });

                }
                
              },);
          }),
      ),
    );
  }

  ///单个按钮对话框
singleButtonDialog(
    {String title = "124",
    TextAlign titleAlign = TextAlign.center,
    Color titleColor = const Color.fromARGB(155, 155, 155, 1),
    barrierDismissible = true,
    required String content,
    Function()? confirmCallBack,
    TextAlign textAlign = TextAlign.start,
    Color confirmTitleColor = const Color.fromARGB(155, 155, 155, 1),
    String confirmButtonTitle = "确定",
    required BuildContext mycontext,
    }) {
  showCupertinoDialog(
      context: context,
      builder: (context) {
        return CupertinoAlertDialog(
          title: Container(
            padding: const EdgeInsets.only(bottom: 16),
            child: Text(title,
                textAlign: titleAlign,
                style: TextStyle(color: titleColor, fontSize: 32)),
          ),
          content: Text(content,
              textAlign: textAlign,
              style: TextStyle(color: titleColor, fontSize: 30)),
          actions: [
            CupertinoDialogAction(
                onPressed: () {
                 // Get.back();
                  if (confirmCallBack != null) {
                    confirmCallBack();
                  }
                },
                child: Text(confirmButtonTitle,
                    style: TextStyle(
                        color: confirmTitleColor,
                        fontSize: 32,
                        fontWeight: FontWeight.bold))),
          ],
        );
      });
}
}
1
likes
135
points
1
downloads

Publisher

unverified uploader

Weekly Downloads

修复android 有些机型闪退问题 原因是通知销毁导致的

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on mxnfc

Packages that implement mxnfc