为什么 Jetpack Compose 提供单独的按钮类型,例如 OutlinedButton、TextButton 和 Button?

You*_*oui 7 android kotlin android-jetpack-compose

为什么 Jetpack Compose 提供单独的按钮类型(例如OutlinedButton和 )TextButton,而不是将按钮样式作为参数传递给单个 Button 类型?

我知道这将有助于我们更好的代码组织和可维护性,并且还可以更容易地直接从名称来理解组件的用途,而不是通过带有一堆参数的通用名称!

这个问题不仅仅是关于按钮,而是关于为什么 Jetpack Compose 为不同的视觉样式提供多个可组合函数,而不是提供可以使用样式参数进行自定义的单个基本可组合函数的设计决策。

因为最后,如果我们看到这些组件的代码,您会看到它正在使用不同的参数调用基本组件:

@Composable
fun OutlinedButton(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    shape: Shape = ButtonDefaults.outlinedShape,
    colors: ButtonColors = ButtonDefaults.outlinedButtonColors(),
    elevation: ButtonElevation? = null,
    border: BorderStroke? = ButtonDefaults.outlinedButtonBorder,
    contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    content: @Composable RowScope.() -> Unit
) =
    Button(
        onClick = onClick,
        modifier = modifier,
        enabled = enabled,
        shape = shape,
        colors = colors,
        elevation = elevation,
        border = border,
        contentPadding = contentPadding,
        interactionSource = interactionSource,
        content = content
    )
Run Code Online (Sandbox Code Playgroud)

Thr*_*ian 1

Material Design 组件不仅适用于 Compose,也适用于 Views、Web、Flutter,并作为一种设计原则。

因此,它们不仅仅是样式,而且是具有各自指导方针、基本原理和目的的组件,以创建具有 M2 和 M3 的设计风格。

在此输入图像描述

在此输入图像描述

在此输入图像描述

https://m3.material.io/components/all-buttons