@@ -33,7 +33,6 @@ import androidx.compose.foundation.verticalScroll
3333import androidx.compose.material.ripple.rememberRipple
3434import androidx.compose.material3.Card
3535import androidx.compose.material3.CardDefaults
36- import androidx.compose.material3.ExperimentalMaterial3Api
3736import androidx.compose.material3.MaterialTheme
3837import androidx.compose.material3.Text
3938import androidx.compose.material3.windowsizeclass.WindowSizeClass
@@ -46,8 +45,10 @@ import androidx.compose.runtime.rememberUpdatedState
4645import androidx.compose.runtime.saveable.rememberSaveable
4746import androidx.compose.runtime.setValue
4847import androidx.compose.ui.Modifier
48+ import androidx.compose.ui.res.stringResource
4949import androidx.compose.ui.unit.dp
5050import androidx.window.layout.DisplayFeature
51+ import com.example.listdetailcompose.R
5152import com.google.accompanist.adaptive.HorizontalTwoPaneStrategy
5253
5354// Create some simple sample data
@@ -81,9 +82,9 @@ fun ListDetailSample(
8182 val widthSizeClass by rememberUpdatedState(windowSizeClass.widthSizeClass)
8283
8384 /* *
84- * The index of the currently selected word.
85+ * The index of the currently selected word, or `null` if none is selected
8586 */
86- var selectedWordIndex by rememberSaveable { mutableStateOf(0 ) }
87+ var selectedWordIndex: Int? by rememberSaveable { mutableStateOf(null ) }
8788
8889 /* *
8990 * True if the detail is currently open. This is the primary control for "navigation".
@@ -101,10 +102,11 @@ fun ListDetailSample(
101102 },
102103 detailKey = selectedWordIndex,
103104 list = { isDetailVisible ->
105+ val currentSelectedWordIndex = selectedWordIndex
104106 ListContent (
105107 words = sampleWords.map(DefinedWord ::word),
106- selectionState = if (isDetailVisible) {
107- SelectionVisibilityState .ShowSelection (selectedWordIndex )
108+ selectionState = if (isDetailVisible && currentSelectedWordIndex != null ) {
109+ SelectionVisibilityState .ShowSelection (currentSelectedWordIndex )
108110 } else {
109111 SelectionVisibilityState .NoSelection
110112 },
@@ -123,7 +125,7 @@ fun ListDetailSample(
123125 )
124126 },
125127 detail = { isListVisible ->
126- val definedWord = sampleWords[ selectedWordIndex]
128+ val definedWord = selectedWordIndex?. let (sampleWords::get)
127129 DetailContent (
128130 definedWord = definedWord,
129131 modifier = if (isListVisible) {
@@ -165,7 +167,6 @@ sealed interface SelectionVisibilityState {
165167/* *
166168 * The content for the list pane.
167169 */
168- @OptIn(ExperimentalMaterial3Api ::class )
169170@Composable
170171private fun ListContent (
171172 words : List <String >,
@@ -254,20 +255,26 @@ private fun ListContent(
254255 */
255256@Composable
256257private fun DetailContent (
257- definedWord : DefinedWord ,
258+ definedWord : DefinedWord ? ,
258259 modifier : Modifier = Modifier ,
259260) {
260261 Column (
261262 modifier = modifier
262263 .verticalScroll(rememberScrollState())
263264 .padding(vertical = 16 .dp)
264265 ) {
265- Text (
266- text = definedWord.word,
267- style = MaterialTheme .typography.headlineMedium
268- )
269- Text (
270- text = definedWord.definition
271- )
266+ if (definedWord != null ) {
267+ Text (
268+ text = definedWord.word,
269+ style = MaterialTheme .typography.headlineMedium
270+ )
271+ Text (
272+ text = definedWord.definition
273+ )
274+ } else {
275+ Text (
276+ text = stringResource(R .string.placeholder)
277+ )
278+ }
272279 }
273280}
0 commit comments