How to connect wifi printer in flutter

Introduction

WiFi printing is kind of printing that enables users to print wirelessly from their mobile devices, including computers, smartphones, and tablets. 
wireless network should be established between the printer and the WiFi device.
This is the plug-in. We’ll use this plugin:
dependencies:
  printing: ^5.9.3

A plugin that enables Flutter apps to create and publish documents to printers that are compatible with iOS or Android. For the purpose of creating PDF files, this plugin utilizes the pdf package found at https://pub.dev/packages/pdf. The documentation is available at https://pub.dev/documentation/pdf/latest.

Include the pdf dependency in the pubspec.yaml file, then execute pub get to install it.

dependencies: 
  pdf: ^3.8.4

First of all, we’ve created a PDF. Please refer below code to create a PDF.

void _createPdf() async {
  final doc = pw.Document();

  doc.addPage(
    pw.Page(
      pageFormat: PdfPageFormat.a4,
      build: (pw.Context context) {
        return pw.Center(
          child: pw.Text(
            _textEditingController.text,
            style: const pw.TextStyle(fontSize: 50),
          ),
        ); // Center
      },
    ),
  );
}

Now, we need to pass the pdf to the printer with the method below.

await Printing.layoutPdf(
        onLayout: (PdfPageFormat format) async => doc.save());

Code:

import 'package:demo_printer/test_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(),
      debugShowCheckedModeBanner: false,
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController _textEditingController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Printing Demo"),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Padding(
              padding: const EdgeInsets.all(20.0),
              child: TextField(
                decoration: const InputDecoration(hintText: 'Enter Any Text'),
                controller: _textEditingController,
              ),
            ),
            ElevatedButton(
              onPressed: _createPdf,
              child: const Text(
                'Genrate PDF',
              ),
            ),
            const SizedBox(
              height: 20,
            ),
            const SizedBox(
              height: 20,
            ),
          ],
        ),
      ),
    );
  }

  void _createPdf() async {
    final doc = pw.Document();

    doc.addPage(
      pw.Page(
        pageFormat: PdfPageFormat.a4,
        build: (pw.Context context) {
          return pw.Center(
            child: pw.Text(
              _textEditingController.text,
              style: const pw.TextStyle(fontSize: 50),
            ),
          ); // Center
        },
      ),
    ); // Page
    await printer(doc);
  }

  printer(doc) async {
    await Printing.layoutPdf(
        onLayout: (PdfPageFormat format) async => doc.save());
  }
}

Conclusion

I hope this blog has given you enough information to try out the printer package in your flutter projects.

Thank you for taking the time to read this article.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories