【问题标题】:Using Compose themes in KMM project在 KMM 项目中使用 Compose 主题
【发布时间】:2021-09-09 21:54:24
【问题描述】:

我正在关注this 撰写文档,但是由于我正在使用由 IDE 生成的 KMM 项目,因此我没有文档中描述的生成的 ui.theme 包。我应该在哪里寻找或编写自己的主题?我应该制作自己的主题包还是放在 KMM 目录的其他位置?

【问题讨论】:

    标签: android-jetpack-compose kotlin-multiplatform-mobile


    【解决方案1】:

    它不会在您的 KMM 目录中,因为它使用无法从共享代码访问的 compose API。

    您可以创建一个空的撰写项目并从那里复制您的主题。这些文件将如下所示:

    ui/theme/Color.kt

    val Purple200 = Color(0xFFBB86FC)
    val Purple500 = Color(0xFF6200EE)
    val Purple700 = Color(0xFF3700B3)
    val Teal200 = Color(0xFF03DAC5)
    

    ui/theme/Shape.kt

    val Shapes = Shapes(
        small = RoundedCornerShape(4.dp),
        medium = RoundedCornerShape(4.dp),
        large = RoundedCornerShape(0.dp)
    )
    

    ui/theme/Typography.kt

    // Set of Material typography styles to start with
    val Typography = Typography(
        body1 = TextStyle(
            fontFamily = FontFamily.Default,
            fontWeight = FontWeight.Normal,
            fontSize = 16.sp
        )
        /* Other default text styles to override
        button = TextStyle(
            fontFamily = FontFamily.Default,
            fontWeight = FontWeight.W500,
            fontSize = 14.sp
        ),
        caption = TextStyle(
            fontFamily = FontFamily.Default,
            fontWeight = FontWeight.Normal,
            fontSize = 12.sp
        )
        */
    )
    

    ui/theme/Theme.kt

    private val DarkColorPalette = darkColors(
        primary = Purple200,
        primaryVariant = Purple700,
        secondary = Teal200
    )
    
    private val LightColorPalette = lightColors(
        primary = Purple500,
        primaryVariant = Purple700,
        secondary = Teal200
    
        /* Other default colors to override
        background = Color.White,
        surface = Color.White,
        onPrimary = Color.White,
        onSecondary = Color.Black,
        onBackground = Color.Black,
        onSurface = Color.Black,
        */
    )
    
    @Composable
    fun MyApplicationTheme(
        darkTheme: Boolean = isSystemInDarkTheme(),
        content: @Composable() () -> Unit
    ) {
        val colors = if (darkTheme) {
            DarkColorPalette
        } else {
            LightColorPalette
        }
    
        MaterialTheme(
            colors = colors,
            typography = Typography,
            shapes = Shapes,
            content = content
        )
    }
    

    【讨论】:

    • 是的,这正是我需要做的,谢谢
    猜你喜欢
    • 2021-05-13
    • 2021-11-13
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 2021-12-16
    相关资源
    最近更新 更多