fasky-software / flutter_widgetkit

Flutter library for iOS Widgets Extensions. Integrate a Widget into your App 🍏📱
MIT License
259 stars 23 forks source link

Is it possible to have a background image? #14

Open desmeit opened 2 years ago

desmeit commented 2 years ago

How could I add a background image to the widget?

xiao-jay commented 2 years ago

hello @tomLadder i have the same need,please teach us how to add background image if you have time

Shatha-Naami commented 1 month ago

Hi @desmeit , @xiao-jay

Yes, you can add an image as a background in your WidgetKit extension, ensuring it's locally available. Here’s how you can do it:

1. Prepare Your Image:

2. Integrate the Image into Your Widget:

3. Customize to Fit Your Design:

Here’s a sample implementation:

import WidgetKit
import SwiftUI

struct YourWidgetEntryView: View {
    var entry: Provider.Entry

    var body: some View {
        GeometryReader { geometry in
            ZStack {
                // Background Image
                Image("backgroundImage")
                    .resizable()
                    .scaledToFill()
                    .edgesIgnoringSafeArea(.all)

                // Widget Content
                Text("Add Background Example!")
                    .font(.title3)
                    .fontWeight(.bold)
                    .foregroundColor(.white)
            }
            .widgetBackground(Color.black)
            .padding(.leading, -16)
            .padding(.trailing, -16)
            .padding(.top, -16)
            .padding(.bottom, -16)
        }
    }
}

struct YourWidget: Widget {
    let kind: String = "FlutterIOSWidget"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            YourWidgetEntryView(entry: entry)
        }
        .supportedFamilies([.systemMedium])
        .configurationDisplayName("Display Extension Name!")
        .description("Your Description🔥")
    }
}

ScreenShot fro result:

Screenshot 2024-07-19 at 8 16 02 PM