clover_connect 0.0.2 clover_connect: ^0.0.2 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 _connectionStatus = 'Unknown';
String _currentStatus = 'Unknown';
String _pairingCode = 'Unknown';
String _saleStatus = '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.';
}
setState(() {
_connectionStatus = connectionStatus;
});
}
Future<void> disconnect() async {
String connectionStatus;
try {
connectionStatus = await CloverConnect.disconnect();
} on PlatformException {
connectionStatus = 'Failed to disconnect Clover.';
}
setState(() {
_connectionStatus = connectionStatus;
});
}
Future<void> getCurrentStatus() async {
String currentStatus;
try {
currentStatus = await CloverConnect.currentStatus;
} on PlatformException {
currentStatus = 'Failed to get current status.';
}
setState(() {
_currentStatus = currentStatus;
});
}
Future<void> getPairingCode() async {
String pairingCode;
try {
pairingCode = await CloverConnect.pairingCode;
} on PlatformException {
pairingCode = 'Failed to get pairing code.';
}
setState(() {
_pairingCode = pairingCode;
});
}
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 connect Clover.';
}
setState(() {
_saleStatus = 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('Disconnect'),
onPressed: disconnect,
),
Spacer(),
],
),
Text('Connection status: $_connectionStatus\n'),
Divider(),
ElevatedButton(
child: Text('Get Pairing Code'),
onPressed: getPairingCode,
),
Text('Pairing code: $_pairingCode\n'),
Divider(),
ElevatedButton(
child: Text('Get current status'),
onPressed: getCurrentStatus,
),
Text('Current status: $_currentStatus\n'),
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,
),
Text('Sale status: $_saleStatus\n'),
Divider(),
],
),
),
),
);
}
}