【问题标题】:How exactly do I use NFC with flutter?我究竟如何将 NFC 与颤振一起使用?
【发布时间】:2022-01-15 20:41:27
【问题描述】:

我对颤振几乎完全陌生,因此我遇到了很多问题。 今天我的问题是正确实施 NFC。 给你一个基本的想法: 我应该制作一个应用程序,在这个应用程序中我想使用 NFC 标签。应该有两个文本框,一个用于您的姓名,另一个用于您的学生证。 通过 NFC,输入的数据应传输到另一个 NFC 设备。 这就是想法。如果有人能给我一些关于如何实现这一点的提示,我将不胜感激

  // ignore_for_file: import_of_legacy_library_into_null_safe

import 'package:flutter/material.dart';
import 'package:ndef/ndef.dart';
import 'package:nfc_in_flutter/nfc_in_flutter.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'FH App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const FirstPage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('FH App'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const Center(
              child: Text(
                'Erste Seite',
                style: TextStyle(fontSize: 35),
              ),
            ),
            SizedBox(
              height: 150,
              child: ListView(
                children: <Widget>[
                  Container(
                    height: 50,
                    color: Colors.blue[600],
                    child: const Center(
                      child: Text('Blau 600'),
                    ),
                  ),
                  Container(
                    height: 50,
                    color: Colors.blue[500],
                    child: const Center(
                      child: Text('Blau 500'),
                    ),
                  ),
                  Container(
                    height: 50,
                    color: Colors.blue[400],
                    child: const Center(
                      child: Text('Blau 400'),
                    ),
                  ),
                  Container(
                    height: 50,
                    color: Colors.blue[300],
                    child: const Center(
                      child: Text('Blau 300'),
                    ),
                  ),
                  Container(
                    height: 50,
                    color: Colors.blue[200],
                    child: const Center(
                      child: Text('Blau 200'),
                    ),
                  ),
                  Container(
                    height: 50,
                    color: Colors.blue[100],
                    child: const Center(
                      child: Text('Blau 100'),
                    ),
                  ),
                  Container(
                    height: 50,
                    color: Colors.blue[0],
                    child: const Center(
                      child: Text('Blau 0'),
                    ),
                  ),
                ],
                scrollDirection: Axis.vertical,
              ),
            ),
            ElevatedButton(
              child: const Text('Seite 2'),
              onPressed: () {
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => const SecondPage()));
              },
            ),
            ElevatedButton(
              child: const Text('Seite 3'),
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => const ThirdPage()));
              },
            ),
          ],
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Seite 2')),
      body: Center(
          child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          const Align(
            Stream<NDEFMessage> stream = NFC.readNDEF(); 
//here I'm getting error message, can't find a way to implement it

          stream.listen((NDEFMessage message) {
        NDEFMessage newMessage = NDEFMessage.withRecords(
        NDEFRecord.mime("text/plain", "hello world")
      );
    message.tag.write(newMessage);
});
            alignment: Alignment.topCenter,
            child: Text('Anmelden mittels NFC', style: TextStyle(fontSize: 35)),
          ),
          ElevatedButton(
            child: const Text('Zurück zu Seite 1'),
            onPressed: () {
              Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => const FirstPage()));
            },
          ),
          ElevatedButton(
            child: const Text('Gehe zu Seite 3'),
            onPressed: () {
              Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => const ThirdPage()));
            },
          )
        ],
      )),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Seite 3')),
      body: Center(
          child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          const Text(
            'Dies ist die dritte Seite',
            style: TextStyle(fontSize: 35),
          ),
          ElevatedButton(
            child: const Text('Zurück zu Seite 1'),
            onPressed: () {
              Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => FirstPage()));
            },
          ),
          ElevatedButton(
            child: const Text('Zurück zu Seite 2'),
            onPressed: () {
              Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => const SecondPage()));
            },
          ),
        ],
      )),
    );
  }
}

其他页面可以忽略,但如果您发现可以改进的地方,我将不胜感激任何提示。我真的很挣扎,我在 YouTube 上也找不到关于如何使用 NFC 和颤振的好教程。 任何帮助将不胜感激, 提前致谢

【问题讨论】:

    标签: android flutter nfc


    【解决方案1】:

    从 NFC 设备向 NFC 设备发送数据并没有得到足够的支持,无法使用(一些较旧的 Android 手机可以,但现在不能,而且永远不会在 iOS 上),因此 Flutter 不支持此功能。只能将数据真正保存到物理 NFC 卡/标签中。

    【讨论】:

    • 我们在墙上有一个 NFC 扫描仪,用于验证我们是否被允许进入。这可能吗?如果没有 NFC 标签的任何实现也会很好
    • NFC 扫描仪是一个 NFC 设备,所以用处不大,你也许可以让它与所有使用本机代码的 Android 手机一起工作。 (但您可能需要重新编写运行扫描仪的软件)
    猜你喜欢
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    相关资源
    最近更新 更多