Jetpack compose expert

From findscripter/everything-skills · View interactive version

Jetpack compose expert is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 Jetpack Compose 新建/重构 Android UI、做状态管理与导航、调优重组性能时使用;产出 ViewModel+StateFlow 单向数据流的 Compose 屏幕、类型安全导航与 Material 3 结构;不适用于 XML View/Flutter/RN 或后端逻辑;触发词:Compose、Composable、重组、StateFlow、NavHost、Material3

How to use this skill

Skill file

何时使用

适用于:

不该用:

步骤

1. 依赖配置

在 libs.versions.toml 中引入 Compose BOM 与所需库,用 BOM 统一管理版本:

[versions]
composeBom = "2024.02.01"
activityCompose = "1.8.2"

[libraries]
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }

2. 状态管理(MVVM/MVI)

用 ViewModel + StateFlow 暴露 UI 状态;对外只暴露只读 StateFlow,绝不暴露 MutableStateFlow。

data class UserUiState(
    val isLoading: Boolean = false,
    val user: User? = null,
    val error: String? = null
)

class UserViewModel @Inject constructor(
    private val userRepository: UserRepository
) : ViewModel() {
    private val _uiState = MutableStateFlow(UserUiState())
    val uiState: StateFlow<UserUiState> = _uiState.asStateFlow()

    fun loadUser() {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true) }
            try {
                val user = userRepository.getUser()
                _uiState.update { it.copy(user = user, isLoading = false) }
            } catch (e: Exception) {
                _uiState.update { it.copy(error = e.message, isLoading = false) }
            }
        }
    }
}

3. 拆分有状态/无状态 Composable

「Screen」级组件负责消费状态,把纯数据与回调下传给「Content」级无状态组件。用 collectAsStateWithLifecycle() 按生命周期安全收集。

@Composable
fun UserScreen(viewModel: UserViewModel = hiltViewModel()) {
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    UserContent(uiState = uiState, onRetry = viewModel::loadUser)
}

@Composable
fun UserContent(uiState: UserUiState, onRetry: () -> Unit) {
    Scaffold { padding ->
        Box(modifier = Modifier.padding(padding)) {
            when {
                uiState.isLoading -> CircularProgressIndicator()
                uiState.error != null -> ErrorView(uiState.error, onRetry)
                uiState.user != null -> UserProfile(uiState.user)
            }
        }
    }
}

指令

示例

类型安全导航(Navigation Compose Type Safety,新版本可用):用 @Serializable 定义目的地,composable<T> 注册路由,backStackEntry.toRoute() 取参。

@Serializable object Home
@Serializable data class Profile(val userId: String)

@Composable
fun AppNavHost(navController: NavHostController) {
    NavHost(navController, startDestination = Home) {
        composable<Home> {
            HomeScreen(onNavigateToProfile = { id ->
                navController.navigate(Profile(userId = id))
            })
        }
        composable<Profile> { backStackEntry ->
            val profile: Profile = backStackEntry.toRoute()
            ProfileScreen(userId = profile.userId)
        }
    }
}

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.