yoco_flutter_sdk 0.0.1
yoco_flutter_sdk: ^0.0.1 copied to clipboard
An SDK to accept card payments with Yoco.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:yoco_flutter_sdk/yoco_flutter_sdk.dart';
import 'package:nfc_manager/nfc_manager.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
Stream<String> nfc(NfcManager manager) async* {
StreamController<String> streamController = StreamController<String>();
streamController.add("Checking availability: ");
// Check availability
bool isAvailable = await manager.isAvailable();
streamController.add('NFC available: $isAvailable');
manager.startSession(
onDiscovered: (NfcTag tag) async {
// Do something with an NfcTag instance.
streamController.add('NFC tag: ${tag.handle}');
},
);
streamController.add("Waiting for NFC tag...");
yield* streamController.stream;
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _nfcOutput = '';
final _yocoFlutterSdkPlugin = YocoFlutterSdk();
@override
void initState() {
super.initState();
initPlatformState();
nfc(NfcManager.instance);
}
void stopNfc(NfcManager manager) {
// Stop Session
manager.stopSession();
}
// 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 _yocoFlutterSdkPlugin.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(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextButton(
onPressed: () async {
setState(() {
_nfcOutput = "";
});
await for (final output in nfc(NfcManager.instance)) {
setState(() {
_nfcOutput += "$output\n\n";
});
}
},
child: const Text('Listen for NFC tags'),
),
TextButton(
onPressed: () {
stopNfc(NfcManager.instance);
setState(() {
_nfcOutput = "Nfc stopped.";
});
},
child: const Text('Stop NFC')),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(_nfcOutput),
),
],
),
),
),
);
}
}