Skip to content

Commit 888ba53

Browse files
add type navigation nsample
1 parent 7cdae30 commit 888ba53

File tree

5 files changed

+191
-23
lines changed

5 files changed

+191
-23
lines changed

Tutorial3-1Navigation/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ dependencies {
5050
implementation(libs.androidx.compose.ui)
5151
implementation(libs.androidx.compose.ui.graphics)
5252
implementation(libs.androidx.compose.ui.tooling.preview)
53+
implementation(libs.androidx.compose.material)
5354
implementation(libs.androidx.compose.material3)
5455
implementation(libs.androidx.navigation.compose)
5556
implementation(libs.kotlinx.serialization.json)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
@file:OptIn(ExperimentalMaterialApi::class)
2+
3+
package com.smarttoolfactory.tutorial3_1navigation
4+
5+
import androidx.compose.foundation.layout.fillMaxWidth
6+
import androidx.compose.foundation.layout.padding
7+
import androidx.compose.material.DropdownMenuItem
8+
import androidx.compose.material.ExperimentalMaterialApi
9+
import androidx.compose.material.ExposedDropdownMenuBox
10+
import androidx.compose.material.ExposedDropdownMenuDefaults
11+
import androidx.compose.material.Text
12+
import androidx.compose.material.TextField
13+
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.getValue
15+
import androidx.compose.runtime.mutableStateOf
16+
import androidx.compose.runtime.remember
17+
import androidx.compose.runtime.setValue
18+
import androidx.compose.ui.Modifier
19+
import androidx.compose.ui.unit.dp
20+
21+
@Composable
22+
fun ExposedSelectionMenu(
23+
title: String,
24+
index: Int,
25+
options: List<String>,
26+
onSelected: (Int) -> Unit,
27+
) {
28+
29+
var expanded by remember { mutableStateOf(false) }
30+
var selectedOptionText by remember { mutableStateOf(options[index]) }
31+
32+
ExposedDropdownMenuBox(
33+
modifier = Modifier
34+
.fillMaxWidth()
35+
.padding(vertical = 4.dp),
36+
expanded = expanded,
37+
onExpandedChange = {
38+
expanded = it
39+
}
40+
) {
41+
42+
TextField(
43+
modifier = Modifier.fillMaxWidth(),
44+
readOnly = true,
45+
value = selectedOptionText,
46+
onValueChange = { },
47+
label = { Text(title) },
48+
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
49+
colors = ExposedDropdownMenuDefaults.textFieldColors()
50+
)
51+
ExposedDropdownMenu(
52+
modifier = Modifier.fillMaxWidth(),
53+
expanded = expanded,
54+
onDismissRequest = {
55+
expanded = false
56+
57+
}
58+
) {
59+
options.forEachIndexed { index: Int, selectionOption: String ->
60+
DropdownMenuItem(
61+
modifier = Modifier.fillMaxWidth(),
62+
content = {
63+
Text(text = selectionOption)
64+
},
65+
onClick = {
66+
selectedOptionText = selectionOption
67+
expanded = false
68+
onSelected(index)
69+
}
70+
)
71+
}
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
@file:OptIn(ExperimentalMaterial3Api::class)
2+
13
package com.smarttoolfactory.tutorial3_1navigation
24

35
import android.os.Bundle
46
import androidx.activity.ComponentActivity
57
import androidx.activity.compose.setContent
68
import androidx.activity.enableEdgeToEdge
7-
import androidx.compose.foundation.layout.fillMaxSize
89
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.material3.ExperimentalMaterial3Api
911
import androidx.compose.material3.Scaffold
10-
import androidx.compose.material3.Text
11-
import androidx.compose.runtime.Composable
12+
import androidx.compose.material3.Surface
1213
import androidx.compose.ui.Modifier
13-
import androidx.compose.ui.tooling.preview.Preview
1414
import com.smarttoolfactory.tutorial3_1navigation.ui.theme.ComposeTutorialsTheme
1515

1616
class MainActivity : ComponentActivity() {
@@ -19,29 +19,15 @@ class MainActivity : ComponentActivity() {
1919
enableEdgeToEdge()
2020
setContent {
2121
ComposeTutorialsTheme {
22-
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
23-
Greeting(
24-
name = "Android",
22+
Scaffold { innerPadding ->
23+
Surface(
2524
modifier = Modifier.padding(innerPadding)
26-
)
25+
) {
26+
TypeSafeNavigationTutorial()
27+
}
2728
}
2829
}
2930
}
3031
}
3132
}
3233

33-
@Composable
34-
fun Greeting(name: String, modifier: Modifier = Modifier) {
35-
Text(
36-
text = "Hello $name!",
37-
modifier = modifier
38-
)
39-
}
40-
41-
@Preview(showBackground = true)
42-
@Composable
43-
fun GreetingPreview() {
44-
ComposeTutorialsTheme {
45-
Greeting("Android")
46-
}
47-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.smarttoolfactory.tutorial3_1navigation
2+
3+
import kotlinx.serialization.Serializable
4+
5+
6+
// Define a home route that doesn't take any arguments
7+
@Serializable
8+
object Home
9+
10+
// Define a profile route that takes an ID
11+
@Serializable
12+
data class Profile(val id: String)
13+
14+
@Serializable
15+
object RouteA
16+
17+
@Serializable
18+
object RouteB
19+
20+
@Serializable
21+
object RouteC
22+
23+
@Serializable
24+
object RouteD
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.smarttoolfactory.tutorial3_1navigation
2+
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.Arrangement
5+
import androidx.compose.foundation.layout.Column
6+
import androidx.compose.foundation.layout.Row
7+
import androidx.compose.foundation.layout.fillMaxSize
8+
import androidx.compose.foundation.layout.fillMaxWidth
9+
import androidx.compose.foundation.lazy.LazyColumn
10+
import androidx.compose.foundation.lazy.items
11+
import androidx.compose.material3.Text
12+
import androidx.compose.runtime.Composable
13+
import androidx.compose.runtime.remember
14+
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.unit.dp
16+
import androidx.navigation.NavBackStackEntry
17+
import androidx.navigation.compose.NavHost
18+
import androidx.navigation.compose.composable
19+
import androidx.navigation.compose.rememberNavController
20+
import androidx.navigation.toRoute
21+
22+
@Composable
23+
fun TypeSafeNavigationTutorial() {
24+
25+
val navController = rememberNavController()
26+
NavHost(
27+
navController = navController,
28+
startDestination = Home
29+
) {
30+
31+
composable<Home> {
32+
HomeScreen {
33+
navController.navigate(
34+
route = it
35+
)
36+
}
37+
}
38+
39+
composable<Profile> { navBackStackEntry: NavBackStackEntry ->
40+
val profile: Profile = navBackStackEntry.toRoute<Profile>()
41+
ProfileScreen(profile)
42+
}
43+
}
44+
}
45+
46+
@Composable
47+
private fun HomeScreen(
48+
onClick: (Profile) -> Unit,
49+
) {
50+
Column(
51+
modifier = Modifier.fillMaxSize()
52+
) {
53+
val list = remember {
54+
List(20) {
55+
Profile(it.toString())
56+
}
57+
}
58+
59+
LazyColumn(
60+
modifier = Modifier.fillMaxSize(),
61+
verticalArrangement = Arrangement.spacedBy(16.dp)
62+
) {
63+
items(list) {
64+
Row(
65+
modifier = Modifier.fillMaxWidth().clickable {
66+
onClick(it)
67+
}
68+
) {
69+
Text("Profile ${it.id}")
70+
}
71+
}
72+
}
73+
}
74+
}
75+
76+
@Composable
77+
private fun ProfileScreen(profile: Profile) {
78+
Column(
79+
modifier = Modifier.fillMaxSize()
80+
) {
81+
Text("Profile: ${profile.id}")
82+
}
83+
}

0 commit comments

Comments
 (0)