简体   繁体   English

是否可以在flutter中构建android小部件?

[英]Is it possible to build android widgets in flutter?

The fact that Flutter heavily uses the word widget makes it difficult to find documentation on this topic.由于 Flutter 大量使用小部件这个词,因此很难找到有关该主题的文档。 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.在 Android 上(我相信这在 iOS 上是不可能的),我们可以在我们的主页仪表板上添加小部件,使我们能够查看与应用程序相关的信息或触发一键操作,而无需打开相关应用程序。

Is it possible to build such "widgets" in Dart & Flutter?是否可以在 Dart & Flutter 中构建这样的“小部件”? Or should I do that in java and somehow plug it with my flutter app?或者我应该在 Java 中执行此操作并以某种方式将其插入我的 flutter 应用程序中?

Can you share an example of a resource containing one?你能分享一个包含一个资源的例子吗?

EDIT: I have no android development experience, but it sounds like using a drawable canvas might do the trick.编辑:我没有 android 开发经验,但听起来使用可绘制画布可能会成功。 I could find some canvas flutter code , but I can't connect the dots yet.我可以找到一些画布颤动代码,但我还无法连接这些点。

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.编辑 2:从这个 Github 问题来看,似乎在 Flutter 中编写 android home 小部件是行不通的,因为 Flutter 有自己的渲染引擎。 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.我热衷于学习kotlin来完成这项工作,但如果有人知道很好的教程来帮助我解决这个问题,那将非常有帮助。

As the OP mentioned in an edit, this isn't currently possible because Flutter uses a custom rendering engine.正如编辑中提到的 OP,这目前是不可能的,因为 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!从理论上讲,您可以使用 Flutter 的软件渲染器在与主实例分离的实例中渲染图像并显示该图像,但这将非常技术性,可能性能不高,而且一点也不简单!

Here is a quite detailed tutorial for widgets that guides you through creating a few examples although in Java.是一个非常详细的小部件教程,可指导您创建一些示例,尽管使用的是 Java。 The same logic applies with just a few syntactical changes for Kotlin as the classes are pretty much interchangeable.相同的逻辑适用于 Kotlin 的一些语法更改,因为这些类几乎可以互换。 However, realistically, most of the work is in the layout with some wiring in android;但是,实际上,大部分工作都在布局中,在 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).如果您已经熟悉 Android 和 Java,请记住 Kotlin 确实增加了您的应用程序大小( The Kotlin runtime adds about 7,000 methods and ~1MB to your debug APKandroid 上kotlin 常见问题解答The Kotlin runtime adds about 7,000 methods and ~1MB to your debug APK )并且由于大多数你的逻辑应该混乱,如果这就是你使用它的全部内容,你可能不会拥有那么多 Kotlin/Java 代码(尽管如果你是 Java 和 Kotlin 的新手,Kotlin 可以说在某些方面更友好)。

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 应用程序和 java/kotlin 后端之间进行通信,您需要使用flutter 文档中所述的平台通道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM