brady_flutter_plugin 1.0.0+1 brady_flutter_plugin: ^1.0.0+1 copied to clipboard
The official Flutter Plugin to integrate the Brady SDK within a Flutter application. The Brady SDK will allow users to discovery, connect, and print to Brady printers.
brady_flutter_plugin #
The official Flutter Plugin to integrate the Brady SDK within a Flutter application. The Brady SDK will allow users to discovery, connect, and print to Brady printers.
Getting Started #
To demonstrate the basic functionalities of the Brady SDK Flutter Plugin, the Dart code used to test this plugin is copy and pasted below.
MAIN.DART #
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:brady_flutter_plugin/brady_flutter_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _bradyFlutterPlugin = BradyFlutterPlugin();
@override
void initState() {
super.initState();
initPlatformState();
}
// 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 _bradyFlutterPlugin.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;
});
}
Future<void> discover() async {
_bradyFlutterPlugin.startPrinterDiscovery();
String? lastConnectedPrinter = await _bradyFlutterPlugin.getLastConnectedPrinterName();
if (lastConnectedPrinter != null) {
connect();
}
}
Future<void> connect() async {
final printerNames = await _bradyFlutterPlugin.getPrinters();
for (final printer in printerNames) {
if (printer! == "M211-PGM2112130501019") {
final connected = await _bradyFlutterPlugin.connectToPrinter(printer);
if (connected == true) {
debugPrint("CONNECTED! :)");
debugPrint(await _bradyFlutterPlugin.getPrinterDetails());
}
else {
debugPrint("FAILED TO CONNECT! :(");
}
}
}
}
void setTemplate() async {
final byteData = await rootBundle.load("assets/flutter_barcode_test.BWT");
final bytes = byteData.buffer.asUint8List();
_bradyFlutterPlugin.setTemplate(base64.encode(bytes));
_bradyFlutterPlugin.setPlaceholderValue("BARCODE 1", "54321");
}
Future<void> print() async {
final printed = await _bradyFlutterPlugin.print();
if (printed == true) {
debugPrint("PRINTING SUCCESSFUL! :)");
}
else {
debugPrint("PRINTING FAILED! :(");
}
}
Future<void> feed() async {
final fed = await _bradyFlutterPlugin.feed();
if (fed == true) {
debugPrint("FED SUCCESSFUL! :)");
}
else {
debugPrint("FED FAILED! :(");
}
}
Future<void> cut() async {
final cut = await _bradyFlutterPlugin.cut();
if (cut == true) {
debugPrint("CUT SUCCESSFUL! :)");
}
else {
debugPrint("CUT FAILED! :(");
}
}
Future<void> disconnect() async {
final disconnected = await _bradyFlutterPlugin.disconnect();
if (disconnected == true) {
debugPrint("DISCONNECT SUCCESSFUL! :)");
}
else {
debugPrint("DISCONNECT FAILED! :(");
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column (
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget> [
ElevatedButton(onPressed: () {
discover();
}, child: const Text('Start Discovery')),
ElevatedButton(onPressed: () {
connect();
}, child: const Text('Connect')),
ElevatedButton(onPressed: () {
setTemplate();
}, child: const Text('Set Template')),
ElevatedButton(onPressed: () {
print();
}, child: const Text('Print')),
ElevatedButton(onPressed: () {
feed();
}, child: const Text('Feed')),
ElevatedButton(onPressed: () {
cut();
}, child: const Text('Cut')),
ElevatedButton(onPressed: () {
disconnect();
}, child: const Text('Disconnect')),
],
),
),
),
);
}
}