Compose笔记(六十五)--插槽模式

这一节主要了解一下Compose中的插槽模式,在Jetpack Compose开发中,插槽模式(Slot Pattern)是一种核心的UI组合设计模式,用于构建高度可复用、灵活且语义清晰的可组合函数。它借鉴Web开发中的"插槽(slot)"思想,允许父组件向子组件"注入"自定义内容,而子组件负责布局和行为逻辑。简单总结如下:

特点:

1 子组件(容器)定义布局结构和交互逻辑。

2 父组件提供具体显示的内容(文本、图标、按钮等)。

3 内容以lambda参数形式传入,通常命名为content、title、icon、actions等。

场景:

1 通用布局组件 当需要构建可复用的布局容器时,插槽模式允许外部灵活控制不同区域的内容。

2 动态内容组合 在需要根据状态或数据动态切换内容的场景中,插槽模式可以避免复杂的条件分支逻辑。

3 主题与样式定制 插槽模式可以结合Modifier或样式对象实现外观定制。

栗子:

Kotlin 复制代码
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
@Composable
fun CardContainer(
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit
) {
    Card(modifier = modifier) {
        content()
    }
}
@Composable
fun ShowCardDemo() {
    CardContainer {
        Text("Hello from parent!")
        Icon(Icons.Default.Favorite, contentDescription = null)
    }
}
Kotlin 复制代码
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowBack
import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun CustomTopAppBar(
    titleText: String,
    title: @Composable (String) -> Unit, 
    navigationIcon: @Composable (() -> Unit)? = null,
    actions: @Composable (() -> Unit)? = null,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .height(56.dp)
            .padding(horizontal = 4.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        
        navigationIcon?.let {
            IconButton(onClick = {  }) {
                it()
            }
        }
        
        Box(
            modifier = Modifier.weight(1f),
            contentAlignment = Alignment.Center
        ) {
            title(titleText)
        }
      
        actions?.let {
            it()
        }
    }
}


@Composable
fun CustomTopAppBarDemo() {
    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        
        CustomTopAppBar(
            titleText = "首页",
            title = { text -> 
                Text(
                    text = text,
                    style = MaterialTheme.typography.titleLarge,
                    color = MaterialTheme.colorScheme.primary
                )
            },
            navigationIcon = {
                Icon(
                    imageVector = Icons.Outlined.ArrowBack,
                    contentDescription = "返回",
                    modifier = Modifier.size(24.dp)
                )
            },
            actions = {
                IconButton(onClick = {  }) {
                    Icon(
                        imageVector = Icons.Outlined.MoreVert,
                        contentDescription = "更多",
                        modifier = Modifier.size(24.dp)
                    )
                }
            }
        )

        CustomTopAppBar(
            titleText = "我的",
            title = { text ->
                Text(
                    text = text,
                    style = MaterialTheme.typography.titleLarge
                )
            },
            modifier = Modifier.padding(top = 20.dp)
        )
    }
}

注意:

1 避免不必要的重组:使用 remember 缓存插槽内容,减少重复计算。

2 单个插槽:命名为content并作为尾随lambda。

3 默认参数:为可选插槽提供合理的默认值。
原理:

1 Compose的组合性,Compose的UI是通过函数调用树(Composition Tree)构建的,每个@Composable函数代表一个UI节点。插槽模式利用这一特性,将子组件的某些部分暴露为参数,允许父组件通过传递不同的@Composable lambda来填充这些插槽,从而动态改变子组件的内部结构。

2 重组与状态管理,当父组件的状态变化时,传递的@Composable lambda会重新执行,导致子组件中对应的插槽内容更新。这种机制确保了UI的响应式更新,同时保持了代码的模块化。

相关推荐
吃我一窝jio1 小时前
字节流与字符流深度解析
java·开发语言·笔记
你看我名字1 小时前
OpenAI 兼容接口能不能直接接入:数眼智能大模型 API 迁移实战与兼容性检查
android·java·数据库·大模型平台选择
用户69371750013841 小时前
用上 AI 之后,Android 的边界确实在消失,但我的活反而变多了
android·人工智能·ai编程
AI2中文网1 小时前
App Inventor 做的表单怎么导出 PDF?WebViewer + JavaScript 在本地生成并保存 PDF 的完整示例
android·开发语言·javascript·pdf·鸿蒙·app inventor
安教安全员通关宝2 小时前
学习笔记:安全员A证、B证、C证的区别与适用岗位
笔记·学习·安全·题库·安全员c·安全员考试
应用市场2 小时前
安卓底层拆解(二)· 改成 user 版为什么开不了机:合成密码与启动状态
android
星夜离尘562 小时前
MATLAB:标定表局部倒退,为什么不能排序?
android·开发语言·matlab
iThinkAi智能体2 小时前
WorkBuddy 写入飞书选题库实战:字段映射、笔记 ID 去重与批量校验
笔记·飞书
StarsGreen2 小时前
Android 12环境下检查v4l2_code的接入方法
android
小白的高手之路3 小时前
【LeetCode 热题 100】560. 和为 K 的子数组|前缀和、哈希表(Python)
数据结构·笔记·python·算法·leetcode·力扣·散列表