是否可以在flutter中构建android小部件? [英] Is it possible to build android widgets in flutter?

查看:282
本文介绍了是否可以在flutter中构建android小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flutter大量使用widget这个事实使得很难找到有关此主题的文档。
在Android上(我认为这在iOS上是不可能的),我们可以在家庭仪表板上添加小部件,从而使我们能够查看与应用程序有关的信息或触发一键式操作,而无需打开有问题的应用程序。

The fact that Flutter heavily uses the word widget makes it difficult to find documentation on this topic. On Android (I believe this isn't possible on iOS), we can add widgets on our home dashboard, allowing us to see app-related information or to trigger one-click actions without needing to open the app in question.

是否有可能在Dart&中建立这样的部件?扑?还是我应该在Java中执行此操作,然后以某种方式将其插入我的flutter应用程序中?

Is it possible to build such "widgets" in Dart & Flutter? Or should I do that in java and somehow plug it with my flutter app?

您可以共享一个包含一个资源的示例吗?

Can you share an example of a resource containing one?

编辑:我没有android开发经验,但听起来像是使用可绘制的画布可以解决问题。
我可以找到一些画布抖动代码,但我还无法连接点。

I have no android development experience, but it sounds like using a drawable canvas might do the trick. I could find some canvas flutter code, but I can't connect the dots yet.

编辑2:来自此Github问题,由于Flutter具有自己的渲染引擎,因此用flutter编写android home小部件似乎是不行的。我热衷于学习 kotlin 来完成这项工作,但是如果有人知道很好

EDIT 2: From this Github issue, it looks like writing android home widgets in flutter is a no go since Flutter has its own rendering engine. I'm keen on learning kotlin to get this done, but if someone knew of nice tutorials to help me with that, that would be immensely helpful.

推荐答案

正如编辑中提到的OP那样,这不是很有帮助。由于Flutter使用了自定义渲染引擎,因此目前可以使用。

As the OP mentioned in an edit, this isn't currently possible because Flutter uses a custom rendering engine.

小部件可呈现的内容非常有限; 文档解释说,只能使用某些布局。从理论上讲,您可以使用Flutter的软件渲染器在与主实例不同的实例中渲染图像并进行显示,但这将是非常技术性的,可能不是很高效,并且根本不简单!

Widgets are quite limited in what they can render; the documentation explains that only certain layouts may be used. You could theoretically use Flutter's software renderer to render to an image in a seperate instance from the main one and display that, but that would be very technical, likely not very performant, and not straightforward at all!

此处是有关小部件的非常详细的教程指导您创建一些示例(尽管使用Java)。相同的逻辑仅适用于Kotlin的一些语法更改,因为这些类几乎可以互换。但是,实际上,大多数工作是在android中进行一些布线的布局中。如果您已经熟悉Android& Java,请记住,Kotlin确实会增加您的应用程序大小( Kotlin运行时会从 android FAQ上的kotlin ),由于您的大多数逻辑都应该扑朔迷离,所以您可能一无所获很多Kotlin / Java代码(如果这就是您要使用的全部代码)(尽管您既是Java&Kotlin的新手,也可以说Kotlin在某些方面更友好)。

Here is a quite detailed tutorial for widgets that guides you through creating a few examples although in Java. The same logic applies with just a few syntactical changes for Kotlin as the classes are pretty much interchangeable. However, realistically, most of the work is in the layout with some wiring in android; if you're already familiar with Android & Java, keep in mind that Kotlin does add a bit to your app size (The Kotlin runtime adds about 7,000 methods and ~1MB to your debug APK from the kotlin on android FAQ) and since most of your logic should be in flutter you probably won't have all that much Kotlin/Java code if this is all you're using it for (although if you're new to both Java & Kotlin, Kotlin is arguably more friendly in some ways).

此外,为了能够在Flutter应用程序与java / kotlin后端之间进行通信,您需要使用 Flutter文档

Also, to be able to communicate between your flutter app and the java/kotlin backend, you'll need to use platform channels as described in the flutter documentation.

这篇关于是否可以在flutter中构建android小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆