Last active
December 2, 2022 04:50
-
-
Save berkanaslan/d49ed6e9354c6bf96b51edeed0cf5559 to your computer and use it in GitHub Desktop.
pt-e550w print example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ui' as ui; | |
import 'package:another_brother/label_info.dart'; | |
import 'package:another_brother/printer_info.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'; | |
import 'package:qr_flutter/qr_flutter.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
final controller = PageController(initialPage: 1); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Another Brother Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: PageView(children: const [ | |
WifiPrintPage(title: 'PT-E550W WiFi Sample'), | |
]), | |
); | |
} | |
} | |
class WifiPrintPage extends StatefulWidget { | |
const WifiPrintPage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
_WifiPrintPageState createState() => _WifiPrintPageState(); | |
} | |
class _WifiPrintPageState extends State<WifiPrintPage> { | |
bool _error = false; | |
void print(BuildContext context) async { | |
final PermissionHandlerPlatform _permissionHandler = PermissionHandlerPlatform.instance; | |
final status = await _permissionHandler.requestPermissions([Permission.storage]); | |
var printer = Printer(); | |
var printInfo = PrinterInfo(); | |
printInfo.printerModel = Model.PT_E550W; | |
printInfo.printMode = PrintMode.FIT_TO_PAGE; | |
printInfo.port = Port.NET; | |
// Set the label type. | |
printInfo.labelNameIndex = PT.ordinalFromID(PT.W24.getId()); | |
// Set the label size. | |
await printer.setPrinterInfo(printInfo); | |
List<NetPrinter> printers = await printer.getNetPrinters([Model.PT_E550W.getName()]); | |
if (printers.isEmpty) { | |
// Show a message if no printers are found. | |
ScaffoldMessenger.of(context).showSnackBar(const SnackBar( | |
content: Padding( | |
padding: EdgeInsets.all(8.0), | |
child: Text("No printers found on your network."), | |
), | |
)); | |
return; | |
} | |
// Get the IP Address from the first printer found. | |
printInfo.ipAddress = printers.single.ipAddress; | |
printer.setPrinterInfo(printInfo); | |
QrValidationResult result = QrValidator.validate(data: "LABEL", version: QrVersions.auto, errorCorrectionLevel: QrErrorCorrectLevel.L); | |
QrPainter painter = QrPainter.withQr( | |
qr: result.qrCode!, | |
color: Colors.black, | |
emptyColor: Colors.white, | |
gapless: false, | |
); | |
ui.Image qrImage = await painter.toImage(2048); | |
if (await printer.startCommunication()) { | |
for (int i = 0; i < 2; i++) { | |
await printer.printImage(qrImage); | |
} | |
} | |
await printer.endCommunication(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: const <Widget>[ | |
Padding( | |
padding: EdgeInsets.all(8.0), | |
child: Text( | |
"Don't forget to grant permissions to your app in Settings.", | |
textAlign: TextAlign.center, | |
), | |
), | |
Image(image: AssetImage('assets/brother_hack.png')) | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () => print(context), | |
tooltip: 'Print', | |
child: const Icon(Icons.print), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment