Flutter - 排除不需要的特定于平台的依赖项

问题描述 投票:0回答:1

我是 Flutter 新手。 我正在尝试该框架,我想探索多平台应用程序。 我的具体目标是:

  • Windows
  • Mac系统
  • Linux

对于每个平台,我都使用自己的主题和图标集(例如,Windows 上的 Fluent 和 Fluent_icons、Linux 上的 adwaita 和 adwaita_icons、Mac 上的 cupertino 和 macos_ui,...)。然而,尽管不同平台的设计有所不同,我还是通过利用 MVVM 和与平台无关的组件来共享组件的行为。

我一直在阅读 packages vs plugins 的所有 flutter 文档 ,但是我认为要么我没有得到一些东西,要么我缺少一些东西。

当我执行特定于平台的构建时,我想摆脱所有不必要的依赖项(例如 Windows 上的 cupertino)。我怎样才能实现这个目标? 我正在考虑创作:

  • 业务逻辑的“共享”包(ViewModel、共享组件……)
  • 每个单独平台的包将依赖于“共享”

通过这种方法,我可以为每个单独的平台提供单独的

pubspec.yaml
,因此,我可以仅包含所需的依赖项。

my_app
  - my_app_linux // Only support for Linux with Adwaita widgets
    - linux 
    - lib
    - pubspec.yaml
  - my_app_windows // Only support for Windows with Fluent widgets
    - windows 
    - lib
    - pubspec.yaml
  - my_app_macos // Only support for Macos with MacOs widgets
    - macos 
    - lib
    - pubspec.yaml
  - my_app_shared (here I can just actually use the root project's "/lib" folder)
    - lib
    - pubspec.yaml

您有什么建议/经验吗?我是否错过了包与插件的官方文档中的某些内容?

flutter configuration multiplatform
1个回答
0
投票

这是演示您所描述的结构的代码:

my_app
  - my_app_linux
    - linux 
    - lib
    - pubspec.yaml
  - my_app_windows
    - windows 
    - lib
    - pubspec.yaml
  - my_app_macos
    - macos 
    - lib
    - pubspec.yaml
  - my_app_shared
    - lib
    - pubspec.yaml

为每个平台使用单独的包并为业务逻辑使用共享包的方法是实现目标的好方法。通过以这种方式构建项目,您可以在每个平台包的

pubspec.yaml
中拥有特定于平台的依赖项,并且仅包含每个平台所需的依赖项。

只需确保正确管理不同包之间的依赖关系和导入即可。您可以将共享包用于与平台无关的组件和逻辑,然后通过在各个平台包中实现所需的 UI 组件和依赖项来自定义特定于平台的行为。

© www.soinside.com 2019 - 2024. All rights reserved.