Flutter电子发票处理插件zatca_fatoora_flutter的使用

发布于 1周前 作者 caililin 来自 Flutter

Flutter电子发票处理插件zatca_fatoora_flutter的使用

标题

Flutter电子发票处理插件zatca_fatoora_flutter的使用

内容

  • Zatca e-invoice (fatoora) QR code generator in Flutter

    • 这个插件严格遵循Zatca当局提供的指南,生成简化版的e-invoice(fatoora)QR码,用于打印在您的企业的账单/发票上。
    • 更多信息请访问官方Zatca Docs
  • Features

    • 简单的QR码
    • 带有logo和自定义样式的QR码
    • 自定义QR码构建器,您可以展示您的魔法✨
  • Usage

    • 要查看完整的代码示例,请参阅example文件夹。

示例代码

import 'package:flutter/material.dart';
import 'package:zatca_fatoora_flutter/zatca_fatoora_flutter.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fatoora',
      theme: ThemeData(useMaterial3: true),
      home: const FatooraSimpleQRCodeView(),
      // home: const FatooraStyledQRCodeView(),
      // home: const FatooraCustomQRCodeView(),
    );
  }
}

///* Simple QR code view --------------------------------------------------------------
class FatooraSimpleQRCodeView extends StatelessWidget {
  const FatooraSimpleQRCodeView({super.key});
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Fatoora Simple QR Code")),
      body: Center(
        child: ZatcaFatoora.simpleQRCode(
          fatooraData: ZatcaFatooraDataModel(
            businessName: "Business name",
            vatRegistrationNumber: "323456789123453",
            date: DateTime.now(),
            totalAmountIncludingVat: 50.75,
          ),
        ),
      ),
    );
  }
}

///* QR code view with your own style --------------------------------------------------------------
class FatooraStyledQRCodeView extends StatelessWidget {
  const FatooraStyledQRCodeView({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Fatoora Styled QR Code")),
      body: Center(
        child: ZatcaFatoora.styledQRCode(
          fatooraData: ZatcaFatooraDataModel(
            businessName: "Business name",
            vatRegistrationNumber: "323456789113453",
            date: DateTime.now(),
            totalAmountIncludingVat: 50.85,
          ),

          // Provide your own style to the QR code data
          qrStyleData: QRStyleData(
            image: const AssetImage("assets/images/logo.jpeg"),
            size: 200,
            backgroundColor: Colors.green.withOpacity(.1),
            qrColor: Colors.green,
            isBorderShapeRounded: true,
          ),
        ),
      ),
    );
  }
}

///* Custom QR code view --------------------------------------------------------------
class FatooraCustomQRCodeView extends StatelessWidget {
  const FatooraCustomQRCodeView({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Fatoora Custom QR Code")),
      body: Center(
        child: ZatcaFatoora.customQRCode(
          fatooraData: ZatcaFatooraDataModel(
            businessName: "Business name",
            vatRegistrationNumber: "323456789113453",
            date: DateTime.now(),
            totalAmountIncludingVat: 50.75,
          ),
          builder: (BuildContext context, String qrData, Widget qrCode) {
            // debugPrint("QR Code data: $qrData");
            return Container(
              padding: const EdgeInsets.all(30),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15),
                color: Colors.grey.shade200,
                border: Border.all(color: Colors.grey.shade300),
              ),
              child: qrCode,
            );
          },
        ),
      ),
    );
  }
}

更多关于Flutter电子发票处理插件zatca_fatoora_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter电子发票处理插件zatca_fatoora_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何使用Flutter电子发票处理插件zatca_fatoora_flutter的示例代码。这个插件用于在Flutter应用中生成和处理符合沙特增值税局(ZATCA)规范的电子发票。

首先,确保你已经在pubspec.yaml文件中添加了zatca_fatoora_flutter插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  zatca_fatoora_flutter: ^最新版本号

然后,运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter应用中按如下方式使用这个插件。以下是一个简单的示例,展示如何生成一个电子发票:

import 'package:flutter/material.dart';
import 'package:zatca_fatoora_flutter/zatca_fatoora_flutter.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter ZATCA Invoice Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: InvoicePage(),
    );
  }
}

class InvoicePage extends StatefulWidget {
  @override
  _InvoicePageState createState() => _InvoicePageState();
}

class _InvoicePageState extends State<InvoicePage> {
  final ZatcaFatoora _zatcaFatoora = ZatcaFatoora();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Generate ZATCA Invoice'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () async {
            try {
              // 创建发票数据
              final invoiceData = InvoiceData(
                sellerDetails: SellerDetails(
                  taxRegistrationNumber: '卖家税号',
                  name: '卖家名称',
                  address: '卖家地址',
                ),
                buyerDetails: BuyerDetails(
                  taxRegistrationNumber: '买家税号',
                  name: '买家名称',
                  address: '买家地址',
                ),
                invoiceDetails: InvoiceDetails(
                  invoiceType: InvoiceType.normal, // 发票类型
                  invoiceDate: DateTime.now(),
                  invoiceNumber: 'INV001',
                  totalAmount: 100.0, // 总金额
                  totalTaxAmount: 15.0, // 总税额
                  currencyCode: 'SAR', // 货币代码
                  items: [
                    InvoiceItem(
                      description: '商品描述',
                      quantity: 1,
                      unitPrice: 100.0,
                      taxRate: 15.0,
                      totalPrice: 100.0,
                    ),
                  ],
                ),
              );

              // 生成发票
              final invoice = await _zatcaFatoora.generateInvoice(invoiceData);
              print('Generated Invoice: $invoice');

              // 你可以在这里将invoice保存到服务器或显示给用户
            } catch (e) {
              print('Error generating invoice: $e');
            }
          },
          child: Text('Generate Invoice'),
        ),
      ),
    );
  }
}

// 定义发票数据模型(这些类通常根据插件提供的API自动生成或定义,这里仅为示例)
class InvoiceData {
  final SellerDetails sellerDetails;
  final BuyerDetails buyerDetails;
  final InvoiceDetails invoiceDetails;

  InvoiceData({
    required this.sellerDetails,
    required this.buyerDetails,
    required this.invoiceDetails,
  });
}

class SellerDetails {
  final String taxRegistrationNumber;
  final String name;
  final String address;

  SellerDetails({
    required this.taxRegistrationNumber,
    required this.name,
    required this.address,
  });
}

class BuyerDetails {
  final String taxRegistrationNumber;
  final String name;
  final String address;

  BuyerDetails({
    required this.taxRegistrationNumber,
    required this.name,
    required this.address,
  });
}

enum InvoiceType { normal, creditNote, debitNote }

class InvoiceDetails {
  final InvoiceType invoiceType;
  final DateTime invoiceDate;
  final String invoiceNumber;
  final double totalAmount;
  final double totalTaxAmount;
  final String currencyCode;
  final List<InvoiceItem> items;

  InvoiceDetails({
    required this.invoiceType,
    required this.invoiceDate,
    required this.invoiceNumber,
    required this.totalAmount,
    required this.totalTaxAmount,
    required this.currencyCode,
    required this.items,
  });
}

class InvoiceItem {
  final String description;
  final int quantity;
  final double unitPrice;
  final double taxRate;
  final double totalPrice;

  InvoiceItem({
    required this.description,
    required this.quantity,
    required this.unitPrice,
    required this.taxRate,
    required this.totalPrice,
  });
}

请注意,InvoiceData, SellerDetails, BuyerDetails, InvoiceDetails, 和 InvoiceItem 这些类只是示例,实际使用时你需要根据zatca_fatoora_flutter插件提供的API来定义这些类。

此外,ZatcaFatoora 类及其方法 generateInvoice 也是示例性的。你需要查阅插件的官方文档来获取准确的类和方法名。

这个示例展示了如何在Flutter应用中生成一个电子发票。实际使用时,你可能需要处理更多的细节,比如错误处理、用户输入验证、发票存储和检索等。

回到顶部