IntentHandler 不适用于 widgetFamily

ser*_*lar 3 widget swift widgetkit swiftui ios14

我向我的项目添加了一个意图扩展目标,我试图区分在编辑模式下显示的小部件类型,IntentHandler并根据此信息,我想用小、中或大小部件类型填充“编辑小部件”列表。

当我使用@Environment(\.widgetFamily) var family它时,它给了我.systemMedium所有的时间,但当时我正在编辑一个大尺寸的小部件。

对于前;当我长按一个小部件来编辑并从列表中选择另一个部件类型时,我看到一个包含小型、中型和大型部件类型的项目列表,IntentHandler但我只想看到小型类型。

问题是,是否可以根据我当前正在编辑的小部件类型来填充列表?

Ada*_*dam 7

假设我明白你\xe2\x80\x99想要做什么,我认为答案是否定的,\xe2\x80\x99是不可能的。要复制 Widgetsmith 等应用程序的功能,您\xe2\x80\x99 需要定义 3 个独立的 Widget,分别对应小、中和大supportedFamily尺寸。然后定义 3 个单独的 Intent,每个小部件一个,并为每个 Intent 实现一个单独的 Intent Handler。

\n

像这样的东西:

\n
@main\nstruct MyWidgetBundle: WidgetBundle {\n    @WidgetBundleBuilder\n    var body: some Widget {\n        SmallWidget()\n        MediumWidget()\n        LargeWidget()\n    }\n}\n\nstruct SmallWidget: Widget {\n    let kind: String = "SmallWidget"\n    \n    var body: some WidgetConfiguration {\n        IntentConfiguration(kind: kind, intent: SmallWidgetIntent.self, provider: TimelineProvider()) { entry in\n            WidgetView(entry: entry)\n        }\n        .configurationDisplayName("Small Widget")\n        .supportedFamilies([.systemSmall])\n    }\n}\n\nstruct MediumWidget: Widget {\n    let kind: String = "MediumWidget"\n    \n    var body: some WidgetConfiguration {\n        IntentConfiguration(kind: kind, intent: MediumWidgetIntent.self, provider: TimelineProvider()) { entry in\n            WidgetView(entry: entry)\n        }\n        .configurationDisplayName("Medium Widget")\n        .supportedFamilies([.systemMedium])\n    }\n}\n\nstruct LargeWidget: Widget {\n    let kind: String = "LargeWidget"\n    \n    var body: some WidgetConfiguration {\n        IntentConfiguration(kind: kind, intent: LargeWidgetIntent.self, provider: TimelineProvider()) { entry in\n            WidgetView(entry: entry)\n        }\n        .configurationDisplayName("Large Widget")\n        .supportedFamilies([.systemLarge])\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在您的意图定义文件中,定义SmallWidgetIntentMediumWidgetIntentLargeWidgetIntent。然后在您的意图处理程序中,您\xe2\x80\x99d 实现SmallWidgetIntentHandlingMediumWidgetIntentHandlingLargeWidgetIntentHandling,这将返回其关联大小的正确选项。

\n