t70_printer_lib 1.0.2 copy "t70_printer_lib: ^1.0.2" to clipboard
t70_printer_lib: ^1.0.2 copied to clipboard

Printer library for T70.

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:permission_handler/permission_handler.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:t70_printer_lib/const/barcode_format.dart';
import 'package:t70_printer_lib/const/error_correction_level.dart';

import 'package:t70_printer_lib_example/printer.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _printer = Printer();

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('PrinterDemo'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () async {
                    await _printer.testPrint();
                  },
                  child: const Text('PRINT'),
                ),
                ElevatedButton(
                  onPressed: () {
                    _printer.testCutPaper();
                  },
                  child: const Text('CUT PAPER'),
                ),
                ElevatedButton(
                  onPressed: () async {
                    String? version = await _printer.testGetVersionInfo();
                    showToast(version!);
                  },
                  child: const Text('GET VERSION INFO'),
                ),
                ElevatedButton(
                  onPressed: () async {
                    _printer.setPrintConcentration();
                  },
                  child: const Text('SET CONCENTRATION'),
                ),
                ElevatedButton(
                  onPressed: () async {
                    _printer.stopPrint();
					showToast("stop");
                  },
                  child: const Text('STOP PRINT'),
                ),
                ElevatedButton(
                  onPressed: () async {
                    _printer.close();
                    showToast("close");
                  },
                  child: const Text('CLOSE'),
                )
              ],
            ),
          )),
    );
  }

  void showToast(String msg) {
    Fluttertoast.showToast(
      msg: msg,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
    );
  }

  void requestPermission(Permission permission) async {
    PermissionStatus status = await permission.request();
    print('权限状态$status');
  }

  void checkPermission() async {
    Permission permission = Permission.storage;
    PermissionStatus status = await permission.status;
    if (status.isGranted) {
      //权限通过
    } else if (status.isDenied) {
      //权限拒绝, 需要区分IOS和Android,二者不一样
      requestPermission(permission);
    } else if (status.isPermanentlyDenied) {
      //权限永久拒绝,且不在提示,需要进入设置界面
      openAppSettings();
    } else if (status.isRestricted) {
      //活动限制(例如,设置了家长///控件,仅在iOS以上受支持。
      openAppSettings();
    } else {
      //第一次申请
      requestPermission(permission);
    }
  }
}