|
1 | 1 | package com.example.memoboard
|
2 | 2 |
|
3 |
| -import androidx.compose.runtime.Composable |
| 3 | +import androidx.compose.animation.animateContentSize |
| 4 | +import androidx.compose.animation.core.Spring |
| 5 | +import androidx.compose.animation.core.spring |
| 6 | +import androidx.compose.foundation.clickable |
| 7 | +import androidx.compose.foundation.layout.Arrangement |
| 8 | +import androidx.compose.foundation.layout.Column |
| 9 | +import androidx.compose.foundation.layout.Row |
| 10 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 11 | +import androidx.compose.foundation.layout.padding |
| 12 | +import androidx.compose.foundation.layout.width |
| 13 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 14 | +import androidx.compose.material3.ButtonDefaults |
| 15 | +import androidx.compose.material3.Card |
| 16 | +import androidx.compose.material3.CardDefaults |
| 17 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 18 | +import androidx.compose.material3.MaterialTheme |
| 19 | +import androidx.compose.material3.OutlinedCard |
4 | 20 | import androidx.compose.material3.Text
|
5 |
| - |
| 21 | +import androidx.compose.material3.TextButton |
| 22 | +import androidx.compose.runtime.Composable |
| 23 | +import androidx.compose.runtime.getValue |
| 24 | +import androidx.compose.runtime.mutableStateOf |
| 25 | +import androidx.compose.runtime.remember |
| 26 | +import androidx.compose.runtime.setValue |
| 27 | +import androidx.compose.ui.Alignment |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.tooling.preview.Preview |
| 30 | +import androidx.compose.ui.unit.dp |
6 | 31 | import androidx.navigation.NavHostController
|
7 | 32 | import androidx.navigation.compose.rememberNavController
|
8 | 33 | import com.example.memoboard.ui.home.HomeScreen
|
| 34 | +import com.example.memoboard.ui.theme.MemoBoardTheme |
9 | 35 |
|
10 | 36 | @Composable
|
11 | 37 | fun MemoBoardScreen(navController: NavHostController = rememberNavController()) {
|
12 | 38 | HomeScreen()
|
13 | 39 | }
|
| 40 | + |
| 41 | +@OptIn(ExperimentalMaterial3Api::class) |
| 42 | +@Composable |
| 43 | +fun MemoBoardTopAppBar( |
| 44 | + title: String, |
| 45 | + canNavigateBack: Boolean, |
| 46 | + navigateUp: () -> Unit = {}, |
| 47 | + titleClickedActionName: String = "", |
| 48 | + titleClickedAction: () -> Unit = {}, |
| 49 | + modifier: Modifier = Modifier |
| 50 | +) { |
| 51 | + var titleClicked by remember { |
| 52 | + mutableStateOf(true) |
| 53 | + } |
| 54 | + |
| 55 | + Row( |
| 56 | + modifier = Modifier |
| 57 | + .fillMaxWidth() |
| 58 | + .animateContentSize( |
| 59 | + animationSpec = spring( |
| 60 | + dampingRatio = Spring.DampingRatioMediumBouncy, |
| 61 | + stiffness = Spring.StiffnessMediumLow |
| 62 | + ) |
| 63 | + ), |
| 64 | + horizontalArrangement = Arrangement.Center |
| 65 | + ) { |
| 66 | + OutlinedCard( |
| 67 | + modifier = modifier |
| 68 | + .width(240.dp), |
| 69 | + shape = RoundedCornerShape(16.dp), |
| 70 | + ) { |
| 71 | + Column( |
| 72 | + horizontalAlignment = Alignment.CenterHorizontally |
| 73 | + ) { |
| 74 | + Row( |
| 75 | + modifier = Modifier.fillMaxWidth(), |
| 76 | + horizontalArrangement = Arrangement.Center, |
| 77 | + verticalAlignment = Alignment.CenterVertically |
| 78 | + ) { |
| 79 | + TextButton( |
| 80 | + enabled = titleClickedActionName != "", |
| 81 | + onClick = { titleClicked = !titleClicked }, |
| 82 | + colors = ButtonDefaults.buttonColors( |
| 83 | + // Match with Card |
| 84 | + containerColor = CardDefaults.outlinedCardColors().containerColor, |
| 85 | + contentColor = CardDefaults.outlinedCardColors().contentColor, |
| 86 | + disabledContainerColor = CardDefaults.outlinedCardColors().containerColor, |
| 87 | + disabledContentColor = CardDefaults.outlinedCardColors().contentColor, |
| 88 | + ) |
| 89 | + ) { |
| 90 | + Text( |
| 91 | + text = title, |
| 92 | + style = MaterialTheme.typography.titleMedium, |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (titleClicked) { |
| 98 | + Card( |
| 99 | + shape = RoundedCornerShape(8.dp), |
| 100 | + modifier = Modifier |
| 101 | + .width(200.dp) |
| 102 | + .padding(bottom = 8.dp) |
| 103 | + .clickable( |
| 104 | + onClick = titleClickedAction |
| 105 | + ) |
| 106 | + ) { |
| 107 | + Row( |
| 108 | + modifier = Modifier |
| 109 | + .fillMaxWidth(), |
| 110 | + horizontalArrangement = Arrangement.Start |
| 111 | + ) { |
| 112 | + Text( |
| 113 | + modifier = Modifier.padding(start = 16.dp, top = 4.dp, bottom = 4.dp), |
| 114 | + text = titleClickedActionName, |
| 115 | + style = MaterialTheme.typography.bodyLarge, |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +@Composable |
| 126 | +@Preview |
| 127 | +fun MemoBoardTopAppBarPreview() { |
| 128 | + MemoBoardTheme { |
| 129 | + MemoBoardTopAppBar( |
| 130 | + title = "Hello World", |
| 131 | + canNavigateBack = false |
| 132 | + ) |
| 133 | + } |
| 134 | +} |
0 commit comments