Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
E
esc_pos_print_plus
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dio Maulana
esc_pos_print_plus
Commits
6d0a5c12
Commit
6d0a5c12
authored
Jan 15, 2020
by
Andrey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added PosGenerator class
parent
4d83fd23
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
84 additions
and
6 deletions
+84
-6
main.dart
example/blue/lib/main.dart
+14
-4
esc_pos_printer.dart
lib/esc_pos_printer.dart
+1
-0
pos_generator.dart
lib/src/pos_generator.dart
+43
-0
printer_bluetooth_manager.dart
lib/src/printer_bluetooth_manager.dart
+26
-2
No files found.
example/blue/lib/main.dart
View file @
6d0a5c12
import
'dart:async'
;
import
'dart:convert'
;
import
'package:esc_pos_printer/esc_pos_printer.dart'
;
import
'package:flutter/material.dart'
;
// import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
import
'package:oktoast/oktoast.dart'
;
void
main
(
)
=>
runApp
(
MyApp
());
...
...
@@ -59,7 +56,20 @@ class _MyHomePageState extends State<MyHomePage> {
void
_testPrint
(
PrinterBluetooth
printer
)
async
{
printerManager
.
selectPrinter
(
printer
);
printerManager
.
printLine
(
'====== @@@ hello
\n\n\n
'
).
then
((
val
)
{
// printerManager.addText('left', styles: PosStyles(align: PosTextAlign.left));
// printerManager.addText('center',
// styles: PosStyles(align: PosTextAlign.center));
// printerManager.addText('right',
// styles: PosStyles(align: PosTextAlign.right), linesAfter: 3);
printerManager
.
addText
(
'Regular: aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ'
);
printerManager
.
addText
(
'Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ'
,
styles:
PosStyles
(
codeTable:
PosCodeTable
.
westEur
));
printerManager
.
addEmptyLines
(
3
);
printerManager
.
printTicket
().
then
((
val
)
{
showToast
(
'Success!'
);
}).
catchError
((
dynamic
e
)
{
print
(
'catched:
${e.toString()}
'
);
...
...
lib/esc_pos_printer.dart
View file @
6d0a5c12
...
...
@@ -9,6 +9,7 @@ library esc_pos_printer;
export
'./src/barcode.dart'
;
export
'./src/enums.dart'
;
export
'./src/pos_column.dart'
;
export
'./src/pos_generator.dart'
;
export
'./src/pos_styles.dart'
;
export
'./src/printer.dart'
;
export
'./src/printer_bluetooth_manager.dart'
;
lib/src/pos_generator.dart
0 → 100644
View file @
6d0a5c12
import
'dart:convert'
;
// import 'dart:io';
import
'dart:typed_data'
;
// import 'package:gbk_codec/gbk_codec.dart';
// import 'package:hex/hex.dart';
// import 'package:image/image.dart';
// import 'barcode.dart';
import
'commands.dart'
;
import
'enums.dart'
;
// import 'pos_column.dart';
import
'pos_styles.dart'
;
class
PosGenerator
{
static
List
<
int
>
emptyLines
(
int
n
)
{
if
(
n
>
0
)
{
return
latin1
.
encode
(
List
.
filled
(
n
,
'
\n
'
).
join
());
}
return
[];
}
static
List
<
int
>
text
(
String
text
,
{
PosStyles
styles
=
const
PosStyles
(),
int
linesAfter
=
0
,
})
{
List
<
int
>
bytes
=
[];
bytes
+=
latin1
.
encode
(
styles
.
align
==
PosTextAlign
.
left
?
cAlignLeft
:
(
styles
.
align
==
PosTextAlign
.
center
?
cAlignCenter
:
cAlignRight
));
// Set local code table
if
(
styles
.
codeTable
!=
null
)
{
bytes
+=
Uint8List
.
fromList
(
List
.
from
(
cCodeTable
.
codeUnits
)..
add
(
styles
.
codeTable
.
value
),
);
}
bytes
+=
latin1
.
encode
(
text
+
'
\n
'
);
bytes
+=
emptyLines
(
linesAfter
);
return
bytes
;
}
}
lib/src/printer_bluetooth_manager.dart
View file @
6d0a5c12
...
...
@@ -8,6 +8,7 @@
import
'dart:async'
;
import
'dart:convert'
;
import
'package:esc_pos_printer/esc_pos_printer.dart'
;
import
'package:rxdart/rxdart.dart'
;
// import 'dart:convert';
// import 'dart:io';
...
...
@@ -40,6 +41,7 @@ class PrinterBluetoothManager {
StreamSubscription
_scanResultsSubscription
;
StreamSubscription
_isScanningSubscription
;
PrinterBluetooth
_selectedPrinter
;
List
<
int
>
_printBuffer
=
[];
final
BehaviorSubject
<
bool
>
_isScanning
=
BehaviorSubject
.
seeded
(
false
);
Stream
<
bool
>
get
isScanningStream
=>
_isScanning
.
stream
;
...
...
@@ -80,7 +82,7 @@ class PrinterBluetoothManager {
_selectedPrinter
=
printer
;
}
Future
<
void
>
printLine
(
String
text
)
async
{
Future
<
void
>
writeBytes
(
List
<
int
>
bytes
)
async
{
const
int
timeout
=
5
;
if
(
_selectedPrinter
==
null
)
{
throw
Exception
(
'Print failed (Select a printer first)'
);
...
...
@@ -108,7 +110,6 @@ class PrinterBluetoothManager {
print
(
'********************* CONNECTED'
);
// To avoid double call
if
(!
_isConnected
)
{
final
List
<
int
>
bytes
=
latin1
.
encode
(
text
).
toList
();
await
_bluetoothManager
.
writeData
(
bytes
);
// TODO Notify data sent
}
...
...
@@ -137,4 +138,27 @@ class PrinterBluetoothManager {
}
});
}
void
addText
(
String
text
,
{
PosStyles
styles
=
const
PosStyles
(),
int
linesAfter
=
0
,
})
{
_printBuffer
+=
PosGenerator
.
text
(
text
,
styles:
styles
,
linesAfter:
linesAfter
);
}
void
addEmptyLines
(
int
n
)
{
_printBuffer
+=
PosGenerator
.
emptyLines
(
n
);
}
Future
<
void
>
printTicket
()
async
{
if
(
_printBuffer
.
isNotEmpty
)
{
final
Future
<
void
>
res
=
writeBytes
(
_printBuffer
);
_printBuffer
=
[];
return
res
;
}
else
{
throw
Exception
(
'Print failed (print buffer is empty)'
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment