Skip to content

Commit 1116b5a

Browse files
committed
Fixed bottomsheet and bottom sheet Modal demo
1 parent 930ef14 commit 1116b5a

File tree

1 file changed

+105
-93
lines changed

1 file changed

+105
-93
lines changed
Lines changed: 105 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
package com.guru.composecookbook.ui.home.dialogs
22

33

4+
import androidx.compose.foundation.Image
45
import androidx.compose.foundation.background
56
import androidx.compose.foundation.clickable
7+
import androidx.compose.foundation.isSystemInDarkTheme
68
import androidx.compose.foundation.layout.*
79
import androidx.compose.foundation.shape.RoundedCornerShape
810
import androidx.compose.material.*
911
import androidx.compose.material.icons.Icons
12+
import androidx.compose.material.icons.filled.FavoriteBorder
1013
import androidx.compose.material.icons.filled.List
14+
import androidx.compose.material.icons.filled.PlayArrow
1115
import androidx.compose.runtime.*
16+
import androidx.compose.ui.Alignment
1217
import androidx.compose.ui.Modifier
1318
import androidx.compose.ui.graphics.Color
19+
import androidx.compose.ui.layout.ContentScale
20+
import androidx.compose.ui.res.painterResource
1421
import androidx.compose.ui.semantics.semantics
1522
import androidx.compose.ui.semantics.testTag
1623
import androidx.compose.ui.text.style.TextAlign
1724
import androidx.compose.ui.tooling.preview.Preview
1825
import androidx.compose.ui.unit.dp
26+
import androidx.compose.ui.unit.sp
27+
import com.guru.composecookbook.spotify.R
28+
import com.guru.composecookbook.theme.graySurface
29+
import com.guru.composecookbook.theme.typography
1930
import com.guru.composecookbook.ui.home.lists.VerticalListView
2031
import kotlinx.coroutines.CoroutineScope
2132
import kotlinx.coroutines.launch
@@ -32,26 +43,22 @@ fun BottomSheetLayouts() {
3243
fun BottomSheetDrawer() {
3344
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState()
3445
val coroutineScope = rememberCoroutineScope()
35-
val showCustomScrim = remember { mutableStateOf(false) }
46+
val sheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
3647
BottomSheetScaffold(
3748
content = {
3849
Box {
39-
ScafoldContent(coroutineScope, bottomSheetScaffoldState, showCustomScrim)
40-
if (showCustomScrim.value) {
41-
CustomBottomSheetBackgroundScrim(scaffoldState = bottomSheetScaffoldState)
42-
}
50+
ScafoldContent(coroutineScope, bottomSheetScaffoldState, sheetState)
4351
}
4452
},
4553
sheetContent = {
46-
BottomSheetContent()
54+
PlayerBottomSheet()
4755
},
4856
drawerContent = {
4957
DrawerContent()
5058
},
5159
scaffoldState = bottomSheetScaffoldState,
52-
sheetPeekHeight = 0.dp,
53-
sheetElevation = 16.dp,
54-
sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp)
60+
sheetPeekHeight = if (sheetState.isAnimationRunning || sheetState.isVisible) 0.dp else 65
61+
.dp,
5562
)
5663
}
5764

@@ -60,83 +67,60 @@ fun BottomSheetDrawer() {
6067
private fun ScafoldContent(
6168
coroutineScope: CoroutineScope,
6269
bottomSheetScaffoldState: BottomSheetScaffoldState,
63-
showCustomScrim: MutableState<Boolean>
70+
sheetState: ModalBottomSheetState
6471
) {
65-
Column(modifier = Modifier.fillMaxSize()) {
66-
Button(
67-
modifier = Modifier
68-
.fillMaxWidth()
69-
.padding(16.dp)
70-
.height(55.dp),
71-
onClick = {
72-
showCustomScrim.value = false
73-
coroutineScope.launch {
74-
bottomSheetScaffoldState.bottomSheetState.expand()
75-
}
76-
}) {
77-
Text(text = "Bottom Sheet")
78-
}
79-
Button(
80-
modifier = Modifier
81-
.fillMaxWidth()
82-
.padding(16.dp)
83-
.height(55.dp),
84-
onClick = {
85-
coroutineScope.launch {
86-
bottomSheetScaffoldState.drawerState.open()
87-
}
88-
}) {
89-
Text(text = "Navigation Drawer")
72+
ModalBottomSheetLayout(
73+
modifier = Modifier.fillMaxSize(),
74+
sheetState = sheetState,
75+
sheetContent = {
76+
BottomSheetContent()
9077
}
91-
Button(
92-
modifier = Modifier
93-
.fillMaxWidth()
94-
.padding(16.dp)
95-
.height(55.dp),
96-
onClick = {
97-
showCustomScrim.value = true
98-
coroutineScope.launch {
99-
bottomSheetScaffoldState.bottomSheetState.expand()
100-
}
101-
}) {
102-
Text(text = "BottomSheet + Custom Scrim")
78+
) {
79+
Column(modifier = Modifier.fillMaxSize()) {
80+
Button(
81+
modifier = Modifier
82+
.fillMaxWidth()
83+
.padding(16.dp)
84+
.height(55.dp),
85+
onClick = {
86+
coroutineScope.launch {
87+
bottomSheetScaffoldState.drawerState.open()
88+
}
89+
}) {
90+
Text(text = "Navigation Drawer")
91+
}
92+
Button(
93+
modifier = Modifier
94+
.fillMaxWidth()
95+
.padding(16.dp)
96+
.height(55.dp),
97+
onClick = {
98+
coroutineScope.launch {
99+
bottomSheetScaffoldState.bottomSheetState.expand()
100+
}
101+
}) {
102+
Text(text = "Bottom Sheet")
103+
}
104+
Button(
105+
modifier = Modifier
106+
.fillMaxWidth()
107+
.padding(16.dp)
108+
.height(55.dp),
109+
onClick = {
110+
coroutineScope.launch {
111+
sheetState.show()
112+
}
113+
}) {
114+
Text(text = "Modal Bottom sheet")
115+
}
103116
}
104117
}
105118
}
106119

107120

108121
@Composable
109122
fun BottomSheetContent() {
110-
Text(text = "Bottom Sheet", textAlign = TextAlign.Center, modifier = Modifier
111-
.fillMaxWidth()
112-
.padding(16.dp))
113-
Button(
114-
modifier = Modifier
115-
.fillMaxWidth()
116-
.padding(16.dp)
117-
.height(55.dp),
118-
onClick = {
119-
}) {
120-
Text(text = "Action 1")
121-
}
122-
Button(
123-
modifier = Modifier
124-
.fillMaxWidth()
125-
.padding(16.dp)
126-
.height(55.dp),
127-
onClick = {
128-
}) {
129-
Text(text = "Action 2")
130-
}
131-
Button(
132-
modifier = Modifier
133-
.fillMaxWidth()
134-
.padding(16.dp)
135-
.height(55.dp),
136-
onClick = {
137-
}) {
138-
Text(text = "Action 3")
139-
}
123+
DrawerContent()
140124
}
141125

142126
@Composable
@@ -166,22 +150,50 @@ fun DrawerContent() {
166150
}
167151
}
168152

169-
@ExperimentalMaterialApi
170153
@Composable
171-
fun CustomBottomSheetBackgroundScrim(scaffoldState: BottomSheetScaffoldState) {
172-
val coroutineScope = rememberCoroutineScope()
173-
val bottomSheetProgress = scaffoldState.bottomSheetState.progress
174-
val bottomSheetFraction = bottomSheetProgress.fraction
175-
if ((bottomSheetProgress.from == BottomSheetValue.Collapsed && bottomSheetFraction < 1.0)
176-
|| bottomSheetProgress.from == BottomSheetValue.Expanded && scaffoldState.bottomSheetState.progress.fraction.toInt() == 1) {
177-
Spacer(modifier = Modifier
178-
.fillMaxSize()
179-
.background(MaterialTheme.colors.onSurface.copy(alpha = DrawerDefaults.ScrimOpacity))
180-
.clickable {
181-
coroutineScope.launch {
182-
scaffoldState.bottomSheetState.collapse()
183-
}
184-
}
154+
fun PlayerBottomSheet() {
155+
val backgroundColor = MaterialTheme.colors.background.copy(alpha = 0.7f)
156+
Row(
157+
modifier = Modifier
158+
.fillMaxWidth()
159+
.background(color = backgroundColor),
160+
verticalAlignment = Alignment.CenterVertically,
161+
) {
162+
Image(
163+
painter = painterResource(id = R.drawable.adele21),
164+
modifier = Modifier.size(65.dp),
165+
contentDescription = null,
166+
contentScale = ContentScale.Crop
167+
)
168+
Text(
169+
text = "Someone Like you by Adele",
170+
style = typography.h6.copy(fontSize = 14.sp),
171+
modifier = Modifier
172+
.padding(8.dp)
173+
.weight(1f),
174+
)
175+
Icon(
176+
imageVector = Icons.Default.FavoriteBorder, modifier = Modifier.padding(8.dp),
177+
contentDescription = null
178+
)
179+
Icon(
180+
imageVector = Icons.Default.PlayArrow, modifier = Modifier.padding(8.dp),
181+
contentDescription = null
185182
)
186183
}
187-
}
184+
Text(text = "Lyrics", style = typography.h6, modifier = Modifier.padding(16.dp))
185+
Text(
186+
text = "I heard that you're settled down\n" +
187+
"That you found a girl and you're married now\n" +
188+
"I heard that your dreams came true\n" +
189+
"Guess she gave you things, I didn't give to you\n" +
190+
"Old friend, why are you so shy?\n" +
191+
"Ain't like you to hold back or hide from the light\n" +
192+
"I hate to turn up out of the blue, uninvited\n" +
193+
"But I couldn't stay away, I couldn't fight it\n" +
194+
"I had hoped you'd see my face\n" +
195+
"And that you'd be reminded that for me, it isn't over",
196+
modifier = Modifier.padding(16.dp)
197+
)
198+
199+
}

0 commit comments

Comments
 (0)