In Jetpack Compose Modifiers are used for
- Change the composable's size, layout, behavior, and appearance
- Add information, like accessibility labels
- Process user input
- Add high-level interactions, like making an element clickable,
scrollable, draggable, or zoomable
You can use a Modifier for multiple items
by declaring them globally or inside a function such as
val modifier = Modifier
.shadow(1.dp, CircleShape)
.border(1.dp, Color.Yellow)
.size(100.dp)
.background(Color.Red)
.padding(10.dp)
or creating your own modifier
fun Modifier.myStyle() = this.then(
Modifier
.shadow(1.dp, CircleShape)
.border(1.dp, Color.Yellow)
.size(100.dp)
.background(Color.Red)
.padding(10.dp)
)
Multiple Composable can use declared or Modifier extension functions
Row(modifier){}
Column(modifier){}
Row(modifier){}
Row(Modifier.myStyle()){}
Row(Modifier.myStyle()){}
However font size is not a general attribute you can apply to any Composable, specific attributes like this can be stored in a class for Composable. Text Composable stores its attributes in a class named TextStyle and this can be applied to any Text when you create an instance of TextStyle.
You can create your own data classes to store Composable specific attributes either.
data class MyAttributes(val fontSize:TextUnit=10.sp, val elevation = CardElevation.elevation,...,val sliderColors)
You can also use CompositionLocal to pass specific styling to scope of Composables too
data class Elevations(val card: Dp = 0.dp, val default: Dp = 0.dp)
// Define a CompositionLocal global object with a default
// This instance can be accessed by all composables in the app
val LocalElevations = compositionLocalOf { Elevations() }
class MyActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Calculate elevations based on the system theme
val elevations = if (isSystemInDarkTheme()) {
Elevations(card = 1.dp, default = 1.dp)
} else {
Elevations(card = 0.dp, default = 0.dp)
}
// Bind elevation as the value for LocalElevations
CompositionLocalProvider(LocalElevations provides elevations) {
// ... Content goes here ...
// This part of Composition will see the `elevations` instance
// when accessing LocalElevations.current
}
}
}
}