Skip to content

Commit

Permalink
Add constraints and modifier order snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
JolandaVerhoef committed Oct 16, 2023
1 parent 4685568 commit b0e7cb3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.compose.snippets.layouts

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.compose.snippets.R

val containerModifier = Modifier.requiredSize(300.dp, 200.dp).background(Color(0xFFD1F1FE))

@Preview
@Composable
private fun Demo1() {
Box(containerModifier, contentAlignment = Alignment.Center) {
// [START android_compose_layout_constraints_modifiers_1]
Image(
painterResource(R.drawable.frag),
contentDescription = null,
Modifier
.fillMaxSize()
.size(50.dp)
)
// [END android_compose_layout_constraints_modifiers_1]
}
}

@Preview(widthDp = 300, heightDp = 200, backgroundColor = 0xFFD1F1FE)
@Composable
private fun Demo2() {
Box(containerModifier, contentAlignment = Alignment.Center) {
// [START android_compose_layout_constraints_modifiers_2]
Image(
painterResource(R.drawable.frag),
contentDescription = null,
Modifier
.fillMaxSize()
.wrapContentSize()
.size(50.dp)
)
// [END android_compose_layout_constraints_modifiers_2]
}
}

@Preview(widthDp = 300, heightDp = 200, backgroundColor = 0xFFD1F1FE)
@Composable
private fun Demo3() {
Box(containerModifier, contentAlignment = Alignment.Center) {
// [START android_compose_layout_constraints_modifiers_3]
Image(
painterResource(R.drawable.frag),
contentDescription = null,
Modifier
.clip(CircleShape)
.padding(10.dp)
.size(100.dp)
)
// [END android_compose_layout_constraints_modifiers_3]
}
}
Binary file added compose/snippets/src/main/res/drawable/frag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b0e7cb3

Please sign in to comment.