directory

  • Hello,Jetpack Compose!
  • Jetpack Compose(2) : Layout
  • Jetpack Compose Compose for the first time

Modifier

Modifier Elements Decorates or adds actions to an ordered, immutable collection of Compose UI elements. For example, background, fill, and click event listeners decorate lines, text, or buttons or add actions to them.

The Modifier, as its name says, provides Compose component modifiers, including but not limited to style modifications, event listeners, and other modifiers.

An orderly

Note the official description of the word “ordered”, because the Modifier is used in a chained manner, so the order in which the attributes are defined affects the UI display. Such as:

@Composable
fun RoundButton(a) {
    Box(
        modifier = Modifier
            .width(300.dp)
            .height(90.dp)
            .background(Color(0xFF3ADF00), shape = RoundedCornerShape(50))
            .padding(20.dp),
        contentAlignment = Alignment.Center){
        Text(text = "RoundButton",color = Color.White)
    }
}
Copy the code

Due to thebackgroundinpaddingBefore, soModifierWill first giveBoxSet the background and then set the margins, as shown below:By the same token,backgroundwithpaddingOrder substitutions, the effect is as follows:

By looking at the Modifier source code can be found, in the process of our chain bit by bit overlay attributes,Modifier will create a CombinedModifier will old and new attributes together, synthesis of a separate Modifier, It is equivalent to the set of parts to be modified layer after layer of Modifier, which is the reason why the Modifier is orderly.

The scalable

Take the Modifier.padding() source code as an example:

@Stable
fun Modifier.padding(all: Dp) =
    this.then(
        PaddingModifier(
            start = all,
            top = all,
            end = all,
            bottom = all,
            rtlAware = true,
            inspectorInfo = debugInspectorInfo {
                name = "padding"
                value = all
            }
        )
    )
Copy the code

As you can see, the padding() is an extension function of Modifier that calls the Modifier’s THEN () function, and that then() needs to receive a Modifier object, which is PaddingModifier.

So of course we can also add extension functions for the Modifier, passing in our custom Modifier to achieve certain effects, such as the following properties we will often configure for different components:

modifier = Modifier
    .width(300.dp)
    .height(90.dp)
    .padding(20.dp)
    .background(Color(0xFF3ADF00), RoundedCornerShape(50))
Copy the code

To make it easier to call, we can add an extension method to the Modifier:

@Stable
fun Modifier.buttonDefault(a) = this.then(
    width(300.dp)
        .height(90.dp)
        .padding(20.dp)
        .background(Color(0xFF3ADF00), RoundedCornerShape(50)))Copy the code

So in the component, we just need to use it like this.

modifier = Modifier.buttonDefault()
Copy the code

Modifier properties

The Modifier configurable properties are numerous and miscellaneous, and are not listed here. For details, go to Andrid Developer and check out some common properties listed by functional category

Width & height

The property name meaning
Modifier.width(width: Dp) Sets its own width, in unitsdp
Modifier.fillMaxWidth(fraction: Float = 1f) The default is to fill the width of the parent container horizontally. The parameter can control the proportion of the width. For example, 0.5 means that the current element is half the width of the parent element
Modifier.wrapContentWidth(align: Alignment.Horizontal = Alignment.CenterHorizontally, unbounded: Boolean = false) According to theThe childThe width of the element determines its own width, and is ignored if it sets a minimum width. whenunboundedParameters fortrueIf the maximum width is set, it will also be ignored
———————————— —————————————–
Modifier.height(height: Dp) Set its own height, in unitsdp
Modifier.fillMaxHeight(fraction: Float = 1f) The default is to fill the width of the parent container vertically. The parameter controls the proportion of the width. For example, 0.5 means the current element is half the height of the parent element
Modifier.wrapContentHeight(align: Alignment.Vertical = Alignment.CenterVertically, unbounded: Boolean = false) According to theThe childThe height of the element determines its own height, and is ignored if it sets a minimum height. whenunboundedParameters fortrueIf you set the maximum height, it will also be ignored
———————————— —————————————–
Modifier.size(size: Dp) Set the width and height from the unitdp
Modifier.size(width: Dp, height: Dp) Set the width and height from the unitdp
Modifier.fillMaxSize(fraction: Float = 1f) The default fills the parent container, and the parameter can control the scale. For example,0.5The current element is half of the parent element
Modifier.wrapContentSize(align: Alignment = Alignment.Center, unbounded: Boolean = false) According to theThe childThe width of the element determines its own width and height, and is ignored if it sets a minimum width and height. whenunboundedParameters fortrueIf the maximum width and height is set, it will also be ignored

spacing

The property name meaning
Modifier.padding(start: Dp = 0.dp, top: Dp = 0.dp, end: Dp = 0.dp, bottom: Dp = 0.dp) Set the fill in each of the four directions
Modifier.padding(horizontal: Dp = 0.dp, vertical: Dp = 0.dp) Set the fill on landscape and portrait respectively
Modifier.padding(all: Dp) Uniformly set the fill in all directions
Modifier.padding(padding: PaddingValues) According to the parametersPaddingValuesTo set the fill,PaddingValuesParameters can be understood as the encapsulation of the above three methods

draw

The property name meaning
Modifier.alpha(alpha: Float) Opacity, ranging from 0 to 1
Modifier.clip(shape: Shape) Cut to the corresponding shape, for exampleshape = RoundedCornerShape(20)Represents clipping to20%Rectangle with rounded corners.
Modifier.shadow(elevation: Dp, shape: Shape = RectangleShape, clip: Boolean = elevation > 0.dp) Draw shadow effect
Modifier.rotate(degrees: Float) Sets the Angle at which the view is rotated around its center
Modifier.scale(scale: Float) Sets the zoom of the view
Modifier.scale(scaleX: Float, scaleY: Float) Sets the zoom of the view

Background & Border

The property name meaning
Modifier.background(color: Color, shape: Shape = RectangleShape) Set the background color
Modifier.background(brush: Brush, shape: Shape = RectangleShape, alpha: Float = 1.0f) Use Brush to set the background color, such as gradient effect
Modifier.border(border: BorderStroke, shape: Shape = RectangleShape) Draws a border for the specified shape
Modifier.border(width: Dp, color: Color, shape: Shape = RectangleShape) Draws a border of the specified width, color, and shape
Modifier.border(width: Dp, brush: Brush, shape: Shape) Draws a border of the specified width, Brush, and shape

behavior

The property name meaning
Modifier.clickable(  enabled: Boolean = true, onClickLabel: String? = null, role: Role? =null,  onClick: () -> Unit) Click on the event
Modifier.combinedClickable( enabled: Boolean = true,onClickLabel: String? = null,role: Role? = null,onLongClickLabel: String? = null,onLongClick: () -> Unit = null,onDoubleClick: () -> Unit = null,onClick: () -> Unit) Combination of click events, including click, long press, and double click
Modifier.horizontalScroll(state: ScrollState, enabled: Boolean = true, reverseScrolling: Boolean = false) Enable components to support horizontal scrolling mode
Modifier.verticalScroll(state: ScrollState, enabled: Boolean = true, reverseScrolling: Boolean = false) Enable components to support vertical scrolling mode

Second, the last

A good memory is better than a bad pen. Jetpack Compose series is my own study notes for the first time, which can not only deepen the knowledge consolidation, but also exercise my writing skills. The content in this article is for reference only, if you have any questions, please leave a comment.

Reference 1.

  • Android developers
  • Jetpack Compose – Modifier entry