bluetooth_print 0.1.2 copy "bluetooth_print: ^0.1.2" to clipboard
bluetooth_print: ^0.1.2 copied to clipboard

outdated

A new flutter plugin project connect to bluetooth thermal printer (Gprinter).

example/lib/main.dart

import 'dart:async';
import 'dart:convert';

import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  BluetoothPrint bluetoothPrint = BluetoothPrint.instance;

  List<BluetoothDevice> _list = [];
  bool _connected = false;
  BluetoothDevice _device;
  String tips = 'no device connect';

  @override
  void initState() {
    super.initState();

    initBluetooth();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initBluetooth() async {
    bool isConnected=await bluetoothPrint.isConnected;
    List<BluetoothDevice> list = await bluetoothPrint.getBondedDevices();
    list.forEach((e){
      print('${e.name} ${e.address}');
    });

    bluetoothPrint.state.listen((state) {
      print('cur device status: $state');

      switch (state) {
        case BluetoothPrint.CONNECTED:
          setState(() {
            _connected = true;
            tips = 'connect success';
          });
          break;
        case BluetoothPrint.DISCONNECTED:
          setState(() {
            _connected = false;
            tips = 'disconnect success';
          });
          break;
        default:
          break;
      }
    });

    if (!mounted) return;

    setState(() {
      _list = list;
    });

    if(isConnected) {
      setState(() {
        _connected=true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Container(
            child: ListView(
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                      child: Text(tips),
                    ),
                  ],
                ),
                Divider(),
                Container(
                  height: 400.0,
                  child: ListView.builder(
                      itemCount: _list.length,
                      itemBuilder: (context, index){
                        return ListTile(
                          title: Text('${_list[index].name}'),
                          subtitle: Text('${_list[index].address}'),
                          onTap: () async {
                            setState(() {
                              _device = _list[index];
                            });
                          },
                          trailing: _device!=null && _device.address == _list[index].address?Icon(
                            Icons.check,
                            color: Colors.green,
                          ):null,
                        );
                      }
                  ),
                ),
                Divider(),
                Container(
                  padding: EdgeInsets.fromLTRB(20, 20, 20, 10),
                  child: Column(
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          OutlineButton(
                            child: Text('connect'),
                            onPressed:  _connected?null:() async {
                              if(_device!=null && _device.address !=null){
                                await bluetoothPrint.connect(_device);
                              }else{
                                setState(() {
                                  tips = 'please select device';
                                });
                                print('please select device');
                              }
                            },
                          ),
                          SizedBox(width: 10.0),
                          OutlineButton(
                            child: Text('disconnect'),
                            onPressed:  _connected?() async {
                              await bluetoothPrint.disconnect();
                            }:null,
                          ),
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          OutlineButton(
                            child: Text('print receipt(esc)'),
                            onPressed:  _connected?() async {
                              Map<String, dynamic> config = Map();
                              List<LineText> list = List();
                              list.add(LineText(type: LineText.TYPE_TEXT, content: 'It is a test', align: LineText.ALIGN_LEFT,linefeed: 0));
                              list.add(LineText(type: LineText.TYPE_TEXT, content: '在右边', align: LineText.ALIGN_RIGHT,linefeed: 0));
                              list.add(LineText(linefeed: 1));
                              list.add(LineText(type: LineText.TYPE_QRCODE, content: 'qrcode i\n', size:10, align: LineText.ALIGN_CENTER, linefeed: 1));
                              list.add(LineText(linefeed: 1));

                              ByteData data = await rootBundle.load("assets/images/guide3.png");
                              List<int> imageBytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
                              String base64Image = base64Encode(imageBytes);
                              list.add(LineText(type: LineText.TYPE_IMAGE, content: base64Image, align: LineText.ALIGN_CENTER, linefeed: 1));
                              await bluetoothPrint.print(config, list);
                            }:null,
                          ),
                          SizedBox(width: 10.0),
                          OutlineButton(
                            child: Text('print label(tsc)'),
                            onPressed:  _connected?() async {
                              Map<String, dynamic> config = Map();
                              config['width'] = 40; // 标签宽度,单位mm
                              config['height'] = 70; // 标签高度,单位mm
                              config['gap'] = 2; // 标签间隔,单位mm

                              // x、y坐标位置,单位dpi,1mm=8dpi
                              List<LineText> list = List();
                              list.add(LineText(type: LineText.TYPE_TEXT, x:10, y:10, content: 'It is a test'));
                              list.add(LineText(type: LineText.TYPE_TEXT, x:10, y:40, content: '在右边'));
                              list.add(LineText(type: LineText.TYPE_QRCODE, x:10, y:70, content: 'qrcode i\n'));
                              list.add(LineText(type: LineText.TYPE_BARCODE, x:10, y:190, content: 'qrcode i\n'));

                              List<LineText> list1 = List();
                              ByteData data = await rootBundle.load("assets/images/guide3.png");
                              List<int> imageBytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
                              String base64Image = base64Encode(imageBytes);
                              list1.add(LineText(type: LineText.TYPE_IMAGE, x:10, y:10, content: base64Image,));

                              await bluetoothPrint.print(config, list);
                              await bluetoothPrint.print(config, list1);
                            }:null,
                          ),
                          SizedBox(width: 10.0),
                          OutlineButton(
                            child: Text('print selftest'),
                            onPressed:  _connected?() async {
                              await bluetoothPrint.printTest();
                            }:null,
                          )
                        ],
                      )
                    ],
                  ),
                )
              ],
            ),
          )
      ),
    );
  }
}
244
likes
35
pub points
95%
popularity

Publisher

unverified uploader

A new flutter plugin project connect to bluetooth thermal printer (Gprinter).

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

flutter, json_annotation

More

Packages that depend on bluetooth_print