|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package androidx.compose.mpp.demo |
| 18 | + |
| 19 | +import androidx.compose.foundation.layout.Column |
| 20 | +import androidx.compose.foundation.layout.Row |
| 21 | +import androidx.compose.foundation.layout.Spacer |
| 22 | +import androidx.compose.foundation.layout.fillMaxSize |
| 23 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 24 | +import androidx.compose.foundation.layout.padding |
| 25 | +import androidx.compose.foundation.layout.size |
| 26 | +import androidx.compose.material.icons.Icons |
| 27 | +import androidx.compose.material.icons.filled.Home |
| 28 | +import androidx.compose.material.icons.filled.Search |
| 29 | +import androidx.compose.material.icons.filled.Settings |
| 30 | +import androidx.compose.material.icons.outlined.Home |
| 31 | +import androidx.compose.material.icons.outlined.Search |
| 32 | +import androidx.compose.material.icons.outlined.Settings |
| 33 | +import androidx.compose.material3.Button |
| 34 | +import androidx.compose.material3.Icon |
| 35 | +import androidx.compose.material3.ModalWideNavigationRail |
| 36 | +import androidx.compose.material3.Text |
| 37 | +import androidx.compose.material3.WideNavigationRailItem |
| 38 | +import androidx.compose.material3.rememberWideNavigationRailState |
| 39 | +import androidx.compose.runtime.Composable |
| 40 | +import androidx.compose.runtime.getValue |
| 41 | +import androidx.compose.runtime.mutableIntStateOf |
| 42 | +import androidx.compose.runtime.remember |
| 43 | +import androidx.compose.runtime.rememberCoroutineScope |
| 44 | +import androidx.compose.runtime.setValue |
| 45 | +import androidx.compose.ui.Alignment |
| 46 | +import androidx.compose.ui.Modifier |
| 47 | +import androidx.compose.ui.text.style.TextAlign |
| 48 | +import androidx.compose.ui.unit.dp |
| 49 | +import kotlinx.coroutines.launch |
| 50 | + |
| 51 | +@Composable |
| 52 | +fun ModalNavigationRail() { |
| 53 | + var selectedItem by remember { mutableIntStateOf(0) } |
| 54 | + val items = listOf("Home", "Search", "Settings") |
| 55 | + val selectedIcons = listOf(Icons.Filled.Home, Icons.Filled.Search, Icons.Filled.Settings) |
| 56 | + val unselectedIcons = |
| 57 | + listOf(Icons.Outlined.Home, Icons.Outlined.Search, Icons.Outlined.Settings) |
| 58 | + |
| 59 | + val state = rememberWideNavigationRailState() |
| 60 | + val scope = rememberCoroutineScope() |
| 61 | + |
| 62 | + Row(Modifier.fillMaxSize()) { |
| 63 | + ModalWideNavigationRail(state = state, hideOnCollapse = true) { |
| 64 | + items.forEachIndexed { index, item -> |
| 65 | + WideNavigationRailItem( |
| 66 | + railExpanded = true, |
| 67 | + icon = { |
| 68 | + Icon( |
| 69 | + if (selectedItem == index) selectedIcons[index] |
| 70 | + else unselectedIcons[index], |
| 71 | + contentDescription = null |
| 72 | + ) |
| 73 | + }, |
| 74 | + label = { Text(item) }, |
| 75 | + selected = selectedItem == index, |
| 76 | + onClick = { |
| 77 | + selectedItem = index |
| 78 | + scope.launch { state.collapse() } |
| 79 | + } |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { |
| 85 | + val currentPage = items[selectedItem] |
| 86 | + Spacer(modifier = Modifier.size(54.dp)) |
| 87 | + Text(text = "$currentPage Page", textAlign = TextAlign.Center) |
| 88 | + Button(onClick = { scope.launch { state.expand() } }, Modifier.padding(32.dp)) { |
| 89 | + Text(text = "Open modal rail", textAlign = TextAlign.Center) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments