Home > OS >  How can I put the bottom bar inside the surface in kotlin compose?
How can I put the bottom bar inside the surface in kotlin compose?

Time:11-04

I want the bottom bar to be where I show it at the bottom, but it will overlap with the surface, so I want the bottom bar at the bottom and the bottom bar on the surface. I tried to put it in the box, but I couldn't do that and I don't want to create your own dictionary text corrupted, I want to adjust accordingly. I couldn't adapt this to my own code. Please help me how can I do this.

my screen

@Composable
fun CreateDicScreen() {



        var text = remember { mutableStateOf("") }


        Card()




        Surface(color = Color.White, modifier = Modifier
                .requiredHeight(600.dp)
                .fillMaxWidth(),
                shape = RoundedCornerShape(60.dp).copy(topStart = ZeroCornerSize, topEnd = ZeroCornerSize)) {


                Column(modifier = Modifier
                        .padding(5.dp),
                        horizontalAlignment = Alignment.CenterHorizontally,
                        ) {
                        Image(
                                painter = painterResource(id = R.drawable.resim ),
                                contentDescription = "image",
                                modifier = Modifier
                                        .padding(5.dp)
                                        .requiredHeight(300.dp)
                                        .requiredWidth(300.dp)

                                )

                        Spacer(modifier = Modifier.padding(16.dp))

                        OutlinedTextField(
                                value = text.value,
                                onValueChange = { text.value = it },
                                modifier= Modifier
                                        .fillMaxWidth()
                                        .padding(5.dp)
                                ,
                                textStyle = TextStyle(color = Color.Black),
                                label = { Text(text = "dictionary name") },
                                colors = TextFieldDefaults.outlinedTextFieldColors(
                                        //focusedBorderColor = Color.Blue,
                                        unfocusedBorderColor = Color.Blue
                                ),

                                )
                        Spacer(modifier=Modifier.padding(5.dp))

                        Button(onClick = {},
                                modifier= Modifier
                                        .padding(5.dp)
                                        .fillMaxWidth(),
                                colors = ButtonDefaults.buttonColors(
                                        backgroundColor = orangish,
                                        contentColor = Color.White),
                                shape = shapes.medium,
                                contentPadding = PaddingValues(8.dp),

                        ) {
                                Text(text = "Save")
                        }
                }
        }


}

@Composable
fun Card(){

        Surface(color = purplish, modifier = Modifier.fillMaxSize()) {

                        Column(verticalArrangement = Arrangement.Bottom,
                                horizontalAlignment = Alignment.CenterHorizontally,
                                modifier = Modifier.offset(y = (-30).dp)
                        ) {

                                Spacer(modifier = Modifier.padding(vertical = 8.dp))

                                Text(text = "create your own dictionary", color = Color.White)

                        }



        }
}

@Composable
fun bottomBar(){


        var bottomState = remember {
                mutableStateOf("Home")
        }

        Scaffold(

                bottomBar = {
                        BottomNavigation(
                                modifier = Modifier
                                        .clip(shape = RoundedCornerShape(topStart = 30.dp, topEnd = 30.dp)),
                                backgroundColor = Color(0xFFFEDBD0),
                                contentColor = Color(0xFF442c2E)
                        ) {
                                BottomNavigationItem(
                                        selected =  bottomState.equals("Home") ,
                                        onClick = { bottomState.equals("Home")},
                                        label = { Text(text = "Home")},
                                        icon = { Icon(imageVector = Icons.Default.Home, contentDescription = null)}
                                )

                                BottomNavigationItem(
                                        selected = bottomState.equals("Account") ,
                                        onClick = { bottomState.equals("Account")},
                                        label = { Text(text = "Account")},
                                        icon = { Icon(imageVector = Icons.Default.AccountCircle, contentDescription = null)}
                                )
                                BottomNavigationItem(
                                        selected = bottomState.equals("Search") ,
                                        onClick = { bottomState.equals("Search")},
                                        label = { Text(text = "Search")},
                                        icon = { Icon(imageVector = Icons.Default.Search, contentDescription = null)}
                                )
                                BottomNavigationItem(
                                        selected = bottomState.equals("Setting") ,
                                        onClick = { bottomState.equals("Setting") },
                                        label = { Text(text = "Setting")},
                                        icon = { Icon(imageVector = Icons.Default.Settings, contentDescription = null)}
                                )

                        }
                }
        ){

        }

}

my screen right now enter image description here

CodePudding user response:

First, you can show bottomNavigation with Scaffold as you wrote in your code, but you don't need to use Scaffold if it's only purpose is to show bottomNavigation (inside your bottomBar() composable). You can use Box.

@Composable
fun bottomBar() {
val bottomState = remember {
    mutableStateOf("Home")
}

Box {
    BottomNavigation(
        modifier = Modifier
            .clip(shape = RoundedCornerShape(topStart = 30.dp, topEnd = 30.dp)),
        backgroundColor = Color(0xFFFEDBD0),
        contentColor = Color(0xFF442c2E)
    ) {
        BottomNavigationItem(
            selected = bottomState.equals("Home"),
            onClick = { bottomState.equals("Home") },
            label = { Text(text = "Home") },
            icon = { Icon(imageVector = Icons.Default.Home, contentDescription = null) }
        )

        BottomNavigationItem(
            selected = bottomState.equals("Account"),
            onClick = { bottomState.equals("Account") },
            label = { Text(text = "Account") },
            icon = {
                Icon(imageVector = Icons.Default.AccountCircle,
                    contentDescription = null)
            }
        )
        BottomNavigationItem(
            selected = bottomState.equals("Search"),
            onClick = { bottomState.equals("Search") },
            label = { Text(text = "Search") },
            icon = { Icon(imageVector = Icons.Default.Search, contentDescription = null) }
        )
        BottomNavigationItem(
            selected = bottomState.equals("Setting"),
            onClick = { bottomState.equals("Setting") },
            label = { Text(text = "Setting") },
            icon = { Icon(imageVector = Icons.Default.Settings, contentDescription = null) }
        )
    }
}
}

To show your bottomBar() on screen, you have several ways to do that. You can use Box again:

@Composable
fun CreateDicScreen() {
var text = remember { mutableStateOf("") }
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomCenter) {
    Card()

    bottomBar()

    Box(modifier = Modifier.fillMaxHeight()) {
        Surface(
            color = Color.White,
            modifier = Modifier
                .requiredHeight(600.dp)
                .fillMaxWidth(),
            shape = RoundedCornerShape(60.dp).copy(topStart = ZeroCornerSize,
                topEnd = ZeroCornerSize)
        ) {
            ...
        }
    }
}
}

With this approach we were forced to set contentAlignment to Bottom, and to wrap Surface with another Box layout and fill height of it.

Second way if you want bottomBar() to be in your Surface (inside composable "Card" (as you named it)), you can do that like this:

@Composable
fun CreateDicScreen() {
var text = remember { mutableStateOf("") }
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomCenter) {
    Card()

    Box(modifier = Modifier.fillMaxHeight()) {
        Surface(
            color = Color.White,
            modifier = Modifier
                .requiredHeight(600.dp)
                .fillMaxWidth(),
            shape = RoundedCornerShape(60.dp).copy(topStart = CornerSize(0.dp),
                topEnd = CornerSize(0.dp))
        ) {
            ...
        }
    }
}
}

@Composable
fun Card() {
Surface(color = Color.Magenta, modifier = Modifier.fillMaxSize()) {
    Column(
        verticalArrangement = Arrangement.Bottom,
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        Spacer(modifier = Modifier.padding(vertical = 8.dp))

        bottomBar()
    }
}
}

and the bottomBar() stays as I mentioned above.

Extra:

I recommend you to use Scaffold for you design (if you don't have specific reason to put your bottomBar in Surface) because it has all features you need here. Here is your design only with Scaffold:

@Composable
fun CreateDicScreen() {
var text = remember { mutableStateOf("") }

Scaffold(
    modifier = Modifier
        .fillMaxSize(),
    bottomBar = { bottomBar() }
) {
    Surface(
        color = Color.LightGray,
        modifier = Modifier
            .requiredHeight(600.dp)
            .fillMaxWidth(),
        shape = RoundedCornerShape(60.dp).copy(topStart = CornerSize(0.dp),
            topEnd = CornerSize(0.dp))
    ) {
        ...
    }
}
}

P.S. While trying to run your code on my Android I've received several times

java.lang.IllegalArgumentException: Corner size in Px can't be negative(topStart = 78.75, topEnd = -196350.0, bottomEnd = 0.0, bottomStart = 0.0)!

change your ZeroCornerSize for Surface to CornerSize(0.dp) to avoid this error.

Your Screen on phone

  • Related