You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Material3主题构建器导入KMP项目遇问题求解决方案

KMP项目Material3主题适配与Google字体替代方案

一、Material主题构建器导出代码的跨平台适配

导出的代码包含Android专属API(如Build.VERSION、LocalContext),需通过expect/actual封装动态颜色逻辑,实现跨平台兼容:

  1. 在commonMain定义expect函数
import androidx.compose.material3.ColorScheme

expect fun getDynamicColorScheme(darkTheme: Boolean): ColorScheme?
  1. 在androidMain实现actual函数
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.ui.platform.LocalContext
import android.os.Build

actual fun getDynamicColorScheme(darkTheme: Boolean): ColorScheme? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val context = LocalContext.current
        if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
    } else {
        null
    }
}
  1. 在iosMain实现actual函数
    iOS无系统级动态配色支持,直接返回null,后续使用静态配色:
import androidx.compose.material3.ColorScheme

actual fun getDynamicColorScheme(darkTheme: Boolean): ColorScheme? = null
  1. 在common主题代码中调用
val colorScheme = if (dynamicColor) {
    getDynamicColorScheme(darkTheme) ?: if (darkTheme) darkScheme else lightScheme
} else {
    if (darkTheme) darkScheme else lightScheme
}

二、Google字体的KMP替代方案

androidx.compose.ui:ui-text-google-fonts仅支持Android,推荐以下两种跨平台方案:

方案1:手动导入字体文件(无依赖,稳定可靠)

  • 从Google Fonts官网下载所需字体的ttf/otf文件
  • Android端:将字体文件放入androidMain/res/font目录(若无font目录需手动创建)
  • iOS端:将字体文件放入iosMain/Resources目录,并在Info.plist中添加Fonts provided by application字段,填入字体文件名
  • commonMain封装字体家族
    import androidx.compose.ui.text.font.Font
    import androidx.compose.ui.text.font.FontFamily
    import androidx.compose.ui.text.font.FontWeight
    
    expect val AppFontFamily: FontFamily
    
    // 示例:定义不同字重的字体
    expect fun getAppFont(weight: FontWeight): Font
    
  • androidMain实现
    import androidx.compose.ui.text.font.Font
    import androidx.compose.ui.text.font.FontFamily
    import androidx.compose.ui.text.font.FontWeight
    import com.yourprojectname.R
    
    actual val AppFontFamily = FontFamily(
        Font(R.font.your_font_regular, FontWeight.Normal),
        Font(R.font.your_font_bold, FontWeight.Bold)
    )
    
    actual fun getAppFont(weight: FontWeight): Font {
        return when(weight) {
            FontWeight.Bold -> Font(R.font.your_font_bold, weight)
            else -> Font(R.font.your_font_regular, weight)
        }
    }
    
  • iosMain实现
    import androidx.compose.ui.text.font.Font
    import androidx.compose.ui.text.font.FontFamily
    import androidx.compose.ui.text.font.FontWeight
    
    actual val AppFontFamily = FontFamily(
        Font("your_font_regular.ttf", FontWeight.Normal),
        Font("your_font_bold.ttf", FontWeight.Bold)
    )
    
    actual fun getAppFont(weight: FontWeight): Font {
        return when(weight) {
            FontWeight.Bold -> Font("your_font_bold.ttf", weight)
            else -> Font("your_font_regular.ttf", weight)
        }
    }
    

方案2:使用跨平台字体加载库

若不想手动管理字体文件,可使用专门的KMP字体库,这类库会自动处理各平台的字体下载与加载,但需注意版本兼容性。


内容的提问来源于stack exchange,提问作者A1m

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 08:22:15