flutter 插件_编写Flutter插件包— 1

flutter 插件

介绍 (Introduction)

I ran into an issue some time ago. I wanted to build Flutter application that could convert PDF files to speech, to help me with school work. The idea was simple; read the text from PDF file and convert it to speech with the flutter_tts plugin. However, I ran into a problem, the PDF plugins for Flutter don’t extract the text from the file they just open native view and show the file as PDF. So to get around this issue I decided to build my own PDF text extractor plugin.

我前段时间遇到了一个问题。 我想构建可以将PDF文件转换为语音的Flutter应用程序,以帮助我完成学业。 这个想法很简单; 从PDF文件中读取文本,并使用 flutter_tts 插件 将其转换为语音 。 但是,我遇到了一个问题,Flutter的PDF插件没有从文件中提取文本,它们只是打开本机视图并将文件显示为PDF。 因此,为了解决这个问题,我决定构建自己的PDF文本提取器插件。

I will split this tutorial to two parts, first part will cover the Android side and second will cover iOS. I will also go over how package plugins work and how they are structured.

我将本教程分为两部分,第一部分将涵盖Android方面,第二部分将涵盖iOS。 我还将介绍软件包插件的工作原理和结构。

要求 (Requirements)

  • I expect you to have a basic understanding of Flutter.

    我希望您对Flutter有一个基本的了解。
  • You will need to have Flutter installed and configured.

    您将需要安装和配置Flutter。
  • I recommend writing native code on Android Studio so that you know it works before adding it to the plugin. This is what I did, but it is not showcased in this tutorial.

    我建议在Android Studio上编写本机代码,以便在将其添加到插件之前知道它可以工作。 这是我所做的,但本教程中未进行介绍。

Flutter软件包如何工作? (How do Flutter packages work?)

Flutter defines two types of packages; dart packages and plugin packages. Dart packages are plain dart code that you can add to your application, plugin packages are packages that run code on the native side; Android and iOS. We are going to be writing a plugin package.

Flutter定义了两种类型的程序包: Dart包和插件包。 Dart程序包是可以添加到应用程序中的普通dart代码,插件程序包是在本机端运行代码的程序包; Android和iOS。 我们将要编写一个插件包。

Flutter uses MethodChannels to talk to the native code. There is a host on the native side that listens for calls from the Flutter- side and responds to those calls. This guide is based on the official documentation by Flutter, I recommend checking it out.

Flutter使用MethodChannels与本机代码对话。 本机端有一个主机,可以侦听Flutter端的呼叫并响应这些呼叫。 本指南基于Flutter的官方文档 ,建议您查阅。

Image for post

创建一个插件包 (Creating a plugin package)

First run Flutter doctor to check that everything is working.

首先运行Flutter医生,检查一切是否正常。

flutter doctor

If needed upgrade Flutter to the latest version.

如果需要,将Flutter升级到最新版本。

flutter upgrade

I am using the stable build of Flutter for this project. The stable build should have less bugs in it. We can check the build channel, and change the channel to stable using these commands.

我正在为该项目使用Flutter的稳定版本。 稳定的版本应该包含较少的错误。 我们可以检查构建通道,并使用这些命令将通道更改为稳定通道。

flutter channelflutter channel stable

We can use the command below to create a new plugin package. The package includes an example project that shows the platform version of your device.

我们可以使用下面的命令来创建一个新的插件包。 该软件包包括一个示例项目,该项目显示了设备的平台版本。

flutter create --template=plugin -i swift -a java pdf_text_package

We can use the -i and -a to set the languages that we want to use for our package. I chose Swift for iOS and Java for Android because I’m more familiar with those languages. If you want you can also use Objective-C or Kotlin.

我们可以使用-i-a设置我们要用于程序包的语言。 我之所以选择iOS的 SwiftAndroid的 Java ,是因为我对这些语言更加熟悉。 如果需要,也可以使用Objective-CKotlin

档案结构 (File structure)

After creating the package we can open the package with our favorite code editor (for me VS Code). Yo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值