How would I create an optional expect/actual funct...
# multiplatform
s
How would I create an optional expect/actual function with a default fallback in commonMain if a platform doesn’t overload it? I want to use material 3 dynamic colors on android, but default colors on all other platforms, so I have an
AppTheme
composable, but only want to expect it on android, and otherwise use the preset colors implementation.
w
c
What’s the reason you cannot implement that fallback at the platform level ? Let’s remember the point of the shared/common code is do/write/architect it once for all platforms and the whole point of expect/actual is for you to implement the part the code that’s platform-dependent while providing your own fallback mechanism (which can be as simple as having a default value) . Based on what you’re saying “_I want to use material 3 dynamic colors on android, but default colors on all other platforms_”… the compose-multiplatform way of thinking about this is for you to build your ui how you would want it to show on all platforms, and when it comes to the AppTheme part, instead of using the AppTheme from your Common Code, you’ll write your own android specific app-theme (w/ M3). To guide you with an example: in commonCode :
@Composable
fun AppTheme(content: @Composable -> Unit )
// no material 3 here
you will call the below function as ..
AppTheme { ..content/composables.. }
@Composable
fun AppThemeM3(content: @Composable -> Unit )
in your androidCode: you will call the below function as ..
AppThemeM3 { ..content/composables.. }
s
@Cheick Kante Except my top-level
App
composable is the entry point to the app. If I didn’t include
AppTheme
in
App
, I would have to wrap the invocation in
AppTheme
on every platform except Android, and
AppThemeM3
on Android. This is the same problem appearing differently. Maybe I could pass a function reference to
App
with a composable theme and provide
AppTheme
as a default value, though I don’t know if Compose supports that.