So far, I’ve followed everything explained in this video -> https://www.youtube.com/watch?v=rqo1w0d5i7w. After that, I created another widget in Xcode and added it to a new group. Unfortunately, that didn’t work.
Additionally, I made sure that the Runner is associated with both groups. However, when I try to add another widget on my iPhone’s home screen, the second widget I created in Xcode doesn’t appear.
Of course, I did get a successful build in Xcode.
This is my Flutter Code:
class HomePage extends StatefulWidget {
const HomePage ({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String appGroupId = 'group.bird_clock';
String iOSWidgetName = 'bird_clock_app_group';
String appGroupId_1 = 'group.bird_clock_app_minutes';
String iOSWidgetName_1 = 'bird_clock_app_minutes_group';
updateWidgetFun() {
HomeWidget.saveWidgetData<String>('title', 'Flutter');
HomeWidget.saveWidgetData<String>('description', 'App development');
HomeWidget.updateWidget(iOSName: iOSWidgetName);
HomeWidget.saveWidgetData<String>('title', 'Flutter_1');
HomeWidget.saveWidgetData<String>('description', 'App development_1');
HomeWidget.updateWidget(iOSName: iOSWidgetName_1);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Bird Clock Test'),
backgroundColor: Colors.deepPurple.withOpacity(0.4),
),
body: Center(
child: ElevatedButton(
onPressed: () => updateWidgetFun(),
child: const Text('Update'),
),
),
);
}
}
XCode
1 Widget:
import WidgetKit
import SwiftUI
private let widgetGroupId = "group.bird_clock_app_group"
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), emoji: "😀", title: "Placeholder Title", description: "Placeholder Message")
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let data = UserDefaults.init(suiteName: widgetGroupId)
let entry = SimpleEntry(date: Date(), emoji: "😀", title: "Placeholder Title", description: "Placeholder Message")
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
getSnapshot(in: context) { (entry) in
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
}
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let emoji: String
let title: String
let description: String
}
struct bird_clockEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack {
Text(entry.title)
Text(entry.description)
}
}
}
struct bird_clock: Widget {
let kind: String = "bird_clock"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
if #available(iOS 17.0, *) {
bird_clockEntryView(entry: entry)
.containerBackground(.fill.tertiary, for: .widget)
} else {
bird_clockEntryView(entry: entry)
.padding()
.background()
}
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
#Preview(as: .systemSmall) {
bird_clock()
} timeline: {
SimpleEntry(date: .now, emoji: "😀", title: "Placeholder Title", description: "Placeholder Message")
SimpleEntry(date: .now, emoji: "🤩", title: "Placeholder Title", description: "Placeholder Message")
}
2 Widget:
import WidgetKit
import SwiftUI
private let widgetGroupId = "bird_clock_app_minutes_group"
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), emoji: "😀", title: "Placeholder Title", description: "Placeholder Message")
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let data = UserDefaults.init(suiteName: widgetGroupId)
let entry = SimpleEntry(date: Date(), emoji: "😀", title: "Placeholder Title", description: "Placeholder Message")
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
getSnapshot(in: context) { (entry) in
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
}
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let emoji: String
let title: String
let description: String
}
struct bird_clock_app_minutesEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack {
Text(entry.title)
Text(entry.description)
}
}
}
struct bird_clock_app_minutes: Widget {
let kind: String = "bird_clock_app_minutes"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
if #available(iOS 17.0, *) {
bird_clock_app_minutesEntryView(entry: entry)
.containerBackground(.fill.tertiary, for: .widget)
} else {
bird_clock_app_minutesEntryView(entry: entry)
.padding()
.background()
}
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
#Preview(as: .systemSmall) {
bird_clock_app_minutes()
} timeline: {
SimpleEntry(date: .now, emoji: "😀", title: "Placeholder Title", description: "Placeholder Message")
SimpleEntry(date: .now, emoji: "🤩", title: "Placeholder Title", description: "Placeholder Message")
}
SonGoku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.