nfc_taplinx 0.0.1
nfc_taplinx: ^0.0.1 copied to clipboard

A new flutter plugin project.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:nfc/nfc.dart';
import './generate_token_screen.dart';
import "./read_token_screen.dart";
import "./firestore/services.dart";
import 'package:firebase_core/firebase_core.dart';

main() {
    WidgetsFlutterBinding.ensureInitialized();
    runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _token = "Token not available !";
  
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  @override
  void initState() {
    print("... Inside initState ...!");
    super.initState();
    initPlatformState();
  }

  Future<dynamic> initializeToken() async {
    String token;
    try {
      token = await nfcInitialize() ?? "";
    } on PlatformException catch (err) {
      token = "initializeToken() Exception !";
      print(err);
    }
    if (!mounted) {
      return;
    }
    setState(() {
      _token = token;
    });
  }

  // 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 NfcPlugin.platformVersion ?? '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;
    });
  }

  Widget getMainUI(token) { 
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin Harris example app'),
        ),
        body: Builder(builder: (context) {
          return ListView(
            children: <Widget>[
              ListTile(
                title: const Text("Generate Token"),
                onTap: () {
                  Navigator.pushNamed(context, "/generate_token");
                },
              ),
              ListTile(
                title: const Text("Read Token"),
                onTap: () {
                  Navigator.pushNamed(context, "/read_token");
                },
              ),
              ListTile(
                title: Text("Tap to initialize : $token !"),
                onTap: () {
                  NfcPlugin.initialize();
                },
              ),
            ],
          );
        }),
      ),
      routes: {
        "/generate_token": (context) => GenerateTokenScreen(),
        "/read_token": (context) => ReadTokenScreen(),
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return  FutureBuilder(
      future: _initialization,
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          print(snapshot.error);
          return Center(child: new Text("Error while starting...${snapshot.error}", textDirection: TextDirection.ltr));
        }
        if (snapshot.connectionState == ConnectionState.done) {
          return getMainUI(_token);
        }
        return Center(child: new Text("LOADING...", textDirection: TextDirection.ltr)); 
      }
    );
  }
}
0
likes
90
pub points
0%
popularity

A new flutter plugin project.

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on nfc_taplinx