Created
July 20, 2024 21:02
-
-
Save alexstyl/c9e60ff1949194ca29bc1bf17d568d42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.composables.composetheme.samples | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.gestures.Orientation | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.foundation.layout.widthIn | |
import androidx.compose.foundation.rememberScrollState | |
import androidx.compose.foundation.shape.RoundedCornerShape | |
import androidx.compose.foundation.text.BasicText | |
import androidx.compose.foundation.verticalScroll | |
import androidx.compose.material.Icon | |
import androidx.compose.material.icons.Icons | |
import androidx.compose.material.icons.outlined.Star | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.LaunchedEffect | |
import androidx.compose.runtime.movableContentOf | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.clip | |
import androidx.compose.ui.graphics.Brush | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.layout.ContentScale | |
import androidx.compose.ui.unit.dp | |
import coil3.compose.AsyncImage | |
import com.composables.composetheme.ComposeTheme | |
import com.composables.composetheme.base | |
import com.composables.composetheme.buildComposeTheme | |
import com.composables.composetheme.colors | |
import com.composables.composetheme.currentHorizontalSizeClass | |
import com.composables.composetheme.gray400 | |
import com.composables.composetheme.gray900 | |
import com.composables.composetheme.large | |
import com.composables.composetheme.medium | |
import com.composables.composetheme.onHorizontalSize | |
import com.composables.composetheme.purple500 | |
import com.composables.composetheme.screenSizes | |
import com.composables.composetheme.sm | |
import com.composables.composetheme.small | |
import com.composables.composetheme.textStyles | |
private val ScreenTheme = buildComposeTheme {} | |
@Composable | |
fun ScreenSizeDemo() { | |
ScreenTheme { | |
val horizontal = currentHorizontalSizeClass | |
LaunchedEffect(horizontal) { | |
println("Horizontal is now $horizontal") | |
} | |
@Composable | |
fun Flex( | |
orientation: Orientation = Orientation.Horizontal, | |
modifier: Modifier = Modifier, | |
arrangement: Arrangement.HorizontalOrVertical = Arrangement.Center, | |
content: @Composable () -> Unit | |
) { | |
val movableContent = movableContentOf(content) | |
if (orientation == Orientation.Horizontal) { | |
Row(modifier = modifier, horizontalArrangement = arrangement) { | |
movableContent() | |
} | |
} else { | |
Column(modifier = modifier, verticalArrangement = arrangement) { | |
movableContent() | |
} | |
} | |
} | |
Box( | |
modifier = Modifier.background(Color.White).fillMaxSize().verticalScroll(rememberScrollState()), | |
contentAlignment = Alignment.TopCenter | |
) { | |
Flex( | |
modifier = Modifier | |
.widthIn(max = 1200.dp) | |
.padding(vertical = 24.dp, horizontal = 16.dp) | |
.fillMaxSize(), | |
arrangement = Arrangement.spacedBy(12.dp), | |
orientation = if (currentHorizontalSizeClass.minWidth >= ComposeTheme.screenSizes.large.minWidth) Orientation.Horizontal else Orientation.Vertical, | |
) { | |
@Composable | |
fun Header(modifier: Modifier = Modifier, useScrim: Boolean = false) { | |
Column( | |
modifier = Modifier | |
.let { | |
if (useScrim) it.background( | |
Brush.verticalGradient(listOf(Color.Transparent, Color.Black.copy(0.7f))), | |
RoundedCornerShape(8.dp) | |
) else it | |
}.then(modifier), | |
verticalArrangement = Arrangement.spacedBy(8.dp) | |
) { | |
BasicText( | |
text = "Entire House", | |
style = ComposeTheme.textStyles.sm.copy(color = if (useScrim) Color.White else Color.Unspecified) | |
) | |
BasicText( | |
text = "Beach House in Collingwood", | |
style = ComposeTheme.textStyles.base.copy(color = if (useScrim) Color.White else Color.Unspecified) | |
) | |
} | |
} | |
// images | |
Box(Modifier.onHorizontalSize(ComposeTheme.screenSizes.large) { it.width(560.dp) }) { | |
Flex( | |
modifier = Modifier.fillMaxWidth(), | |
arrangement = Arrangement.spacedBy(4.dp), | |
orientation = if (currentHorizontalSizeClass.minWidth >= ComposeTheme.screenSizes.large.minWidth) Orientation.Vertical else Orientation.Horizontal, | |
) { | |
AsyncImage( | |
model = "https://plus.unsplash.com/premium_photo-1661962841993-99a07c27c9f4?q=80&w=3431&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", | |
contentScale = ContentScale.Crop, | |
contentDescription = null, | |
modifier = Modifier.clip(RoundedCornerShape(8.dp)) | |
.onHorizontalSize(ComposeTheme.screenSizes.large) { it.fillMaxWidth(1f) } | |
.onHorizontalSize(ComposeTheme.screenSizes.small) { it.fillMaxWidth(0.5f) } | |
.fillMaxWidth() | |
.height(240.dp) | |
) | |
if (currentHorizontalSizeClass.minWidth >= ComposeTheme.screenSizes.small.minWidth) { | |
Row( | |
modifier = Modifier.fillMaxWidth(1f), | |
horizontalArrangement = Arrangement.spacedBy(4.dp) | |
) { | |
AsyncImage( | |
model = "https://images.unsplash.com/photo-1511840831832-3efd661c1d0f?q=80&w=2048&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", | |
contentScale = ContentScale.Crop, | |
contentDescription = null, | |
modifier = Modifier.clip(RoundedCornerShape(8.dp)) | |
.weight(1f) | |
.height(240.dp) | |
) | |
if (currentHorizontalSizeClass.minWidth >= ComposeTheme.screenSizes.medium.minWidth) { | |
AsyncImage( | |
model = "https://images.unsplash.com/photo-1506079478915-3f458c5077a7?q=80&w=3387&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", | |
contentScale = ContentScale.Crop, | |
contentDescription = null, | |
modifier = Modifier.clip(RoundedCornerShape(8.dp)) | |
.weight(1f) | |
.height(240.dp) | |
) | |
} | |
} | |
} | |
} | |
if (currentHorizontalSizeClass.minWidth < ComposeTheme.screenSizes.small.minWidth) { | |
Header(Modifier.align(Alignment.BottomCenter).fillMaxWidth().padding(12.dp), useScrim = true) | |
} | |
} | |
Box( | |
Modifier | |
.fillMaxWidth() | |
.align(Alignment.TopCenter), | |
contentAlignment = Alignment.TopCenter | |
) { | |
Column( | |
verticalArrangement = Arrangement.spacedBy(12.dp), | |
modifier = Modifier | |
.onHorizontalSize(ComposeTheme.screenSizes.large) { | |
it.widthIn(max = 540.dp) | |
} | |
) { | |
if (currentHorizontalSizeClass.minWidth >= ComposeTheme.screenSizes.small.minWidth) { | |
Header(Modifier.fillMaxWidth()) | |
} | |
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(top = 4.dp)) { | |
Row( | |
horizontalArrangement = Arrangement.spacedBy(4.dp), | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
Icon( | |
Icons.Outlined.Star, | |
contentDescription = null, | |
modifier = Modifier.padding(end = 4.dp), | |
tint = ComposeTheme.colors.purple500 | |
) | |
BasicText( | |
text = "4.89", | |
style = ComposeTheme.textStyles.base.copy(color = ComposeTheme.colors.gray900) | |
) | |
BasicText( | |
text = "(128)", | |
style = ComposeTheme.textStyles.base.copy(color = ComposeTheme.colors.gray400) | |
) | |
} | |
} | |
Box( | |
Modifier.clip(RoundedCornerShape(8.dp)) | |
.clickable { /* TODO */ } | |
.background(ComposeTheme.colors.purple500) | |
.padding(vertical = 12.dp, horizontal = 16.dp) | |
) { | |
BasicText( | |
"Check availability", | |
style = ComposeTheme.textStyles.base.copy(color = Color.White) | |
) | |
} | |
BasicText( | |
text = "This sunny and spacious room is for those traveling light and looking for a comfy and cosy place to lay their head for a night or two. This beach house sits in a vibrant neighborhood littered with cafes, pubs, restaurants and supermarkets and is close to all the major attractions such as Edinburgh Castle and Arthur's Seat.", | |
style = ComposeTheme.textStyles.base | |
) | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment