clover_connect 0.0.4 clover_connect: ^0.0.4 copied to clipboard
A flutter plugin to connect flutter app with Clover payment devices.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:clover_connect/clover_connect.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _pairingCode = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
final cloverEndpointTextEditingController =
TextEditingController(text: 'wss://192.168.1.123:12345/remote_pay');
Future<void> connect() async {
String connectionStatus;
String endpoint = cloverEndpointTextEditingController.text;
try {
var cloverConfig = CloverConfig(endpoint: endpoint);
connectionStatus = await CloverConnect.connect(cloverConfig);
} on PlatformException {
connectionStatus = 'Failed to connect Clover.';
}
print(connectionStatus);
}
Future<void> reset() async {
String connectionStatus;
try {
connectionStatus = await CloverConnect.reset();
} on PlatformException {
connectionStatus = 'Failed to disconnect Clover.';
}
print(connectionStatus);
}
Future<void> disconnect() async {
String connectionStatus;
try {
connectionStatus = await CloverConnect.disconnect();
} on PlatformException {
connectionStatus = 'Failed to disconnect Clover.';
}
print(connectionStatus);
}
Future<void> getPairingCode() async {
String pairingCode;
try {
pairingCode = await CloverConnect.pairingCode;
} on PlatformException {
pairingCode = 'Failed to get pairing code.';
}
setState(() {
_pairingCode = pairingCode;
});
}
Future<void> getLastEvent() async {
String lastEvent = await CloverConnect.lastEvent;
print(lastEvent);
}
Future<void> getDeviceEvent() async {
Map deviceEvent = await CloverConnect.deviceEvent;
print(deviceEvent);
}
final saleAmountTextEditingController = TextEditingController(text: '1.23');
final saleExternalIdTextEditingController =
TextEditingController(text: '123456');
Future<void> sale() async {
String saleStatus;
int amount =
(double.parse(saleAmountTextEditingController.text) * 100).toInt();
String externalId = saleExternalIdTextEditingController.text;
try {
var saleRequest = SaleRequest(amount: amount, externalId: externalId);
saleStatus = await CloverConnect.sale(saleRequest);
} on PlatformException {
saleStatus = 'Failed to do sale.';
}
print(saleStatus);
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// Platform messages may fail, so we use a try/catch PlatformException.
// 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;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Clover Connect Plugin example app'),
),
body: Center(
child: Column(
children: [
Text('https://github.com/phan-dev/flutter-clover-connect'),
Divider(),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Clover Endpoint',
),
controller: cloverEndpointTextEditingController,
),
Row(
children: [
Spacer(),
ElevatedButton(
child: Text('Connect'),
onPressed: connect,
),
Spacer(),
ElevatedButton(
child: Text('Reset'),
onPressed: reset,
),
Spacer(),
ElevatedButton(
child: Text('Disconnect'),
onPressed: disconnect,
),
Spacer(),
],
),
Divider(),
ElevatedButton(
child: Text('Get Pairing Code'),
onPressed: getPairingCode,
),
Text('Pairing code: $_pairingCode\n'),
Divider(),
Row(
children: [
Spacer(),
ElevatedButton(
child: Text('Get Last Event'),
onPressed: getLastEvent,
),
Spacer(),
ElevatedButton(
child: Text('Get Device Event'),
onPressed: getDeviceEvent,
),
Spacer(),
],
),
Divider(),
Row(
children: [
Spacer(),
Expanded(
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Amount',
),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(
RegExp(r'^\d*\.?\d*$'))
],
controller: saleAmountTextEditingController,
),
),
Spacer(),
Expanded(
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'External Id',
),
controller: saleExternalIdTextEditingController,
),
),
Spacer(),
],
),
ElevatedButton(
child: Text('Sale'),
onPressed: sale,
),
Divider(),
],
),
),
),
);
}
}