| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
ย | ย | |||
ย | ย | |||
First, examine the screen design with the KISS principle (except "K" is replaced with "L" โ i.e., Look, it's simple and stupid).
Break down the screen into individual components and write them cohesively with minimal dependencies and a single objective (Component's purpose section). Also, start with repeated ones in their dedicated Kotlin files.
// โ Don't do
// File name: MyScreen.kt
@Composable
fun MyButton(
onClick: () -> Unit,
iconInnerPadding: PaddingValue = PaddingValues(0.dp),
modifier: Modifier = Modifier,
icon: Icon? = null,
contentDescriptor: String? = null,
text: String? = null,
) {
Row(modifier.clickable(onClick), ...) {
if (icon != null) Icon(icon, contentDescriptor, modifier = Modifier.padding(iconInnerPadding))
if (text != null) Text(text, modifier = Modifier.padding(start = if (icon == null) 0.dp else 8.dp))
}
}
// โ
Do instead:
// File name: MyButton.kt
@Composable
fun TextButton(onClick: () -> Unit, text: String, modifier: Modifier = Modifier) {
Row(modifier.clickable(onClick), ...) {
Text(text)
}
}
@Composable
fun IconButton(
onClick: () -> Unit,
icon: Icon,
contentDescriptor: String,
modifier: Modifier = Modifier,
innerPadding: PaddingValue = PaddingValues(0.dp),
content: @Composable () -> Unit = { }
) {
Row(modifier.clickable(onClick), ...) {
Icon(icon, contentDescriptor, modifier = Modifier.padding(innerPadding))
content()
}
}First, create necessary data models and then a Composable function to avoid using hardcoded values inside the function.
Recognize component structure and write layout and element nodes with minimal configuration (i.e., only passing data to required arguments).
@Immutable
data class Question(val text: String, val possibleAnswers: List<String>, val correctAnswer: String)
// Build UI structure
@Composable
fun QuestionAnswerBox(question: Question) {
Column {
Text(introduceText)
Spacer(Modifier)
question.possibleAnswers.forEach {
Row {
RadioButton(
selected = question.correctAnswer == it,
onClick = { }
)
Text(it)
}
}
}
}@Immutable
data class Question(val text: String, val possibleAnswers: List<String>, val correctAnswer: String)
// First apply optional arguments
@Composable
fun QuestionAnswerBox(question: Question) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text(introduceText, color = Color.Red)
Spacer(Modifier)
question.possibleAnswers.forEach {
Row(horizontalArrangement = Arrangement.spaced(8.dp)) {
RadioButton(
selected = question.correctAnswer == it,
onClick = { }
)
Text(it)
}
}
}
}
// Finally, decorate with modifiers
@Composable
fun QuestionAnswerBox(question: Question) {
Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.background(Color.White)) {
Text(introduceText, color = Color.Red)
Spacer(Modifier.height(16.dp))
question.possibleAnswers.forEach {
Row(horizontalArrangement = Arrangement.spaced(8.dp)) {
RadioButton(
selected = question.correctAnswer == it,
onClick = { }
)
Text(it)
}
}
}
}| Back | FazBrowse Home | New Git URL |