InfoApp Jetpack Compose | #1

MainActivity.kt

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.example.infoapp.ui.theme.InfoAppTheme
import com.example.infoapp.ui_components.DrawerMenu
import com.example.infoapp.ui_components.MainTopBar

class MainActivity : ComponentActivity() {
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val scaffoldState = rememberScaffoldState()
val topBarTitle = remember {
mutableStateOf("Грибы")
}
InfoAppTheme {
Scaffold(
scaffoldState = scaffoldState,
topBar = {
MainTopBar(
title = topBarTitle.value,
scaffoldState
)
},
drawerContent = {
DrawerMenu()
}
) {

}
}
}
}
}

DrawerMenu.kt

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.example.infoapp.ui.theme.MainRed
import com.example.infoapp.R
import com.example.infoapp.ui.theme.BgTransparent


@Composable
fun DrawerMenu() {
Box(modifier = Modifier.fillMaxSize()){
Image(painter = painterResource(
id = R.drawable.drawer_list_bg),
contentDescription = "Main Bg image",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
)
Column(modifier = Modifier.fillMaxSize()) {
Header()
Body()
}
}
}

@Composable
fun Header(){
Card(
modifier = Modifier
.fillMaxWidth()
.height(170.dp)
.padding(5.dp),
shape = RoundedCornerShape(10.dp),
border = BorderStroke(1.dp, MainRed)
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.BottomCenter
) {
Image(
painter = painterResource(id = R.drawable.header_bg),
contentDescription = "Header image",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
)
Text(
text = "-Справочник ботаника-",
modifier = Modifier
.fillMaxWidth()
.background(MainRed)
.padding(10.dp),
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold,
color = Color.White
)
}
}
}

@Composable
fun Body(){
val list = stringArrayResource(id = R.array.drawer_list)
LazyColumn(modifier = Modifier.fillMaxSize()){
itemsIndexed(list){ idex, title ->
Card(
modifier = Modifier
.fillMaxWidth()
.padding(3.dp),
backgroundColor = BgTransparent
) {
Text(
text = title,
modifier = Modifier
.fillMaxWidth()
.clickable {

}
.padding(10.dp)
.wrapContentWidth(),
fontWeight = FontWeight.Bold
)
}
}
}
}

MainTopBar.kt

import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Menu
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.graphics.Color
import kotlinx.coroutines.launch

@Composable
fun MainTopBar(title: String, scaffoldState: ScaffoldState) {
val coroutine = rememberCoroutineScope()
TopAppBar(
title = {
Text(text = title)
},
backgroundColor = Color.White,
navigationIcon = {
IconButton(
onClick = {
coroutine.launch {
scaffoldState.drawerState.open()
}
}
) {
Icon(
imageVector = Icons.Default.Menu,
contentDescription = "Menu"
)
}
},
actions = {
IconButton(
onClick = {

}
) {
Icon(
imageVector = Icons.Default.Favorite,
contentDescription = "Favorite"
)
}
}
)
}

main_array,xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="drawer_list">
<item>Грибы</item>
<item>Лечебные травы</item>
<item>Деревья</item>
<item>Фрукты</item>
<item>Овощи</item>
</string-array>
</resources>

Color.kt

import androidx.compose.ui.graphics.Color

val Purple200 = Color(0xFFBB86FC)
val Purple500 = Color(0xFF6200EE)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
val MainRed = Color(0xE6BD0909)
val BgTransparent = Color(0xB3FFFFFF)

Картинки из проекта

2 комментария для “InfoApp Jetpack Compose | #1”

Добавить комментарий

Ваш адрес email не будет опубликован.