admin管理员组

文章数量:1410717

I am working on my first Android Compose app and need to validate whether this way of accessing resources like dimens, colors, and spacing is correct or not.

I created some classes like this:

@Immutable
class Spacing(
    val none: Dp = 0.dp,
    val small: Dp = 5.dp,
    val medium: Dp = 10.dp,
    val large: Dp = 20.dp,
    val extraLarge: Dp = 30.dp,
    val extraLarger: Dp = 40.dp,
    val big: Dp = 100.dp,
)

One for Spacing, one for Sizing, one for Colors, etc.

Is it OK to create a Singleton class and access from it? like this:

/**
 * Singleton object that contains all the theme related values to be accessed from anywhere.
 */
object AppTheme {
    val fontSizing = FontSizing()
    val spacing = Spacing()
    val color = Colors()
    val sizing = Sizing()
}

So in my composable UI, the idea is to access it like this:

BottomSheetScaffold(
    scaffoldState = scaffoldState,
    sheetShape = RoundedCornerShape(topStart = AppTheme.spacing.large, topEnd = AppTheme.spacing.large),
    sheetPeekHeight = AppTheme.sizing.bottomSheetPeekHeight,
    sheetContent = {
        DrawSheetContent(modifier, viewState, viewModel)
    }
...

本文标签: Android Compose access Themeresource values like DimensColorsStack Overflow