【发布时间】: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 和颤振的好教程。 任何帮助将不胜感激, 提前致谢
【问题讨论】: