Bug: Device.isUsingDarkAppearance() in Widget always returns True

Just creating this to let the Dev team know that when Device.isUsingDarkAppearance() is used in a Widget script, it always returns True. The issue can be replicated with the below code.

let mainWidget = await createWidget()

if (!config.runsInWidget) {
	 await mainWidget.presentSmall()
}

Script.setWidget(mainWidget)
Script.complete()

async function createWidget() {
	
	let widget = new ListWidget()
	let isDarkMode = await Device.isUsingDarkAppearance()

	if (isDarkMode) {
		widget.addText("Dark mode is on.")
	} else {
		widget.addText("Dark mode is off.")
	}

	return widget
}

i tried the code and got a result of “Dark mode is off.”

btw, Device.isUsingDarkAppearance() simply returns a boolean value. there’s no need to use await with it.

Hmm, I tried removing the await keyword but I’m still getting the same issue. Were you testing the results from the widget? It works fine in the app for me but I just can’t get it to work for the widget.

Try this function isUsingDarkAppearance

Your code not working on my widget, always return true :confused:

try this example

Still always return true
Take a look here https://www.icloud.com/iclouddrive/0vAXgKMeHIL4GMRKz0oTjifzg#True

I recall a note somewhere saying that isUsingDarkApperance doesn’t work for widgets. I don’t see it in the dev notes, but I’m sure I’ve seen it somewhere.

Maybe on Twitter?

1 Like

Did somebody find a solution for this problem? I tried the function that supermamon shared but in the widget I always get true as answer. And thats wrong! In the app everything is working fine. Hope somebody could help!

I’m not sure why it doesn’t work for you but works fine for me. But some thing I found out on the latest version 1.6.1

Device.isUsingDarkAppearance() looks to be working already on widgets.

If you need that function just for color themes, there’s a new Color.dynamic() method where you can pass 2 colors, one for light and one for dark. The approriate color will be applied when you change modes.

Is there a way to make this work for DrawContext? While this works for the widget, unlike Device.isUsingDarkAppearance, I still haven’t found a way to get any color changes for DrawContext to work for switching color modes.