okadan/flutter-nfc-manager

refreshes when I press cancel on popup

seobioh opened this issue · 0 comments

as video shown, it refreshes when I press cancel which makes somefunction i desinged in 2 pages before to perform.
As I know, I should be able to control this by onError. but I can't remove this refresh whatever I do

RPReplay_Final1706347317.mp4
class NFCWritePage extends StatefulWidget {
  final NdefMessage data;
  const NFCWritePage({super.key, required this.data});

  @override
  State<NFCWritePage> createState() => _NFCWritePageState();
}

class _NFCWritePageState extends State<NFCWritePage> {
  String? _alertMessage;
  String? _errorMessage;

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

  @override
  void dispose() {
    super.dispose();
    NfcManager.instance.stopSession();
  }

  void _startNFC() async {
    bool isAvailable = await NfcManager.instance.isAvailable();
    if (isAvailable) {
      _ndefWrite(widget.data);
    } else {
      _errorMessage = "NFC 사용 불가";    
    }
  }

  void _ndefWrite(NdefMessage nfcData) {
    NfcManager.instance.startSession(
        pollingOptions: {NfcPollingOption.iso14443, NfcPollingOption.iso15693},
        alertMessage: "NFC 태그를 스캔해주세요.",
        onDiscovered: (NfcTag tag) async {
          var ndef = Ndef.from(tag);
          if (ndef == null || !ndef.isWritable) {
            _errorMessage = "쓰기가 불가능한 NFC 태그입니다.";
            NfcManager.instance.stopSession(errorMessage: _errorMessage);
            return;
          }
          try {
            await ndef.write(nfcData);
            _alertMessage = "NFC에 성공적으로 기록하였습니다.";
            NfcManager.instance.stopSession();
          } catch (error) {
            _errorMessage = error.toString();
            NfcManager.instance.stopSession(errorMessage: error.toString());
            return;
          }
        },
        onError: (NfcError error) async {
          _errorMessage = error.message;
          if (error.type == NfcErrorType.userCanceled) {
            return;
          } else {
            return;
          }
        }
      );
  }
...
                RoundButton(text: Text("NFC에 쓰기", style: TextStyle(color: Colors.white),), width: 144, onPressed: () async {
                  _startNFC();
                  if (_alertMessage != null){
                    Navigator.push(context, MaterialPageRoute(builder: (_) => QRcodeCompletePage()));
...