最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How can I implement a FAB cradle (cutout) with a BottomAppBar in Material 3 using Jetpack Compose? - Stack Overflow

programmeradmin5浏览0评论

I'm trying to recreate the Material Design pattern where a Floating Action Button (FAB) is partially embedded (or “cradled”) by a BottomAppBar. In Material Design 2, I used the cutoutShape property on the BottomAppBar to achieve this effect. However, with the Material 3 theme in Jetpack Compose, there doesn't appear to be any built-in support for a FAB cradle anchor or cutout in the BottomAppBar.

Does anyone know of an idiomatic or recommended workaround in Material 3 to achieve this FAB cradle effect? For example, should I create a custom composable that draws a BottomAppBar with a cutout (perhaps using a custom shape and clipping)? Or is there a library or pattern that the community has adopted as a substitute for the missing API?

Are there any potential pitfalls or design considerations when implementing a custom FAB cradle in Material 3? I’d love to understand any challenges related to different screen configurations or animations.

I'm trying to recreate the Material Design pattern where a Floating Action Button (FAB) is partially embedded (or “cradled”) by a BottomAppBar. In Material Design 2, I used the cutoutShape property on the BottomAppBar to achieve this effect. However, with the Material 3 theme in Jetpack Compose, there doesn't appear to be any built-in support for a FAB cradle anchor or cutout in the BottomAppBar.

Does anyone know of an idiomatic or recommended workaround in Material 3 to achieve this FAB cradle effect? For example, should I create a custom composable that draws a BottomAppBar with a cutout (perhaps using a custom shape and clipping)? Or is there a library or pattern that the community has adopted as a substitute for the missing API?

Are there any potential pitfalls or design considerations when implementing a custom FAB cradle in Material 3? I’d love to understand any challenges related to different screen configurations or animations.

Share Improve this question edited Mar 9 at 16:41 Peter Haddad 81k26 gold badges145 silver badges148 bronze badges Recognized by Mobile Development Collective asked Mar 9 at 15:51 SinaSina 11 silver badge 1
  • I would not consider this a "missing API" - it rather seems to me that they don't support this case according to their new guidelines. – BenjyTec Commented Mar 10 at 10:37
Add a comment  | 

1 Answer 1

Reset to default 0

Are there any potential pitfalls or design considerations when implementing a custom FAB cradle in Material 3?

This is probably not the answer you are hoping for, but this kind of cradle is not supported in the Material3 libraries likely because the Material3 Guidelines indicate you should place the FAB exactly as shown in your first mockup.

If you still want to display a cutout, please have a look at this answer, it gives you an idea how to create a custom BottomAppBar with a specific shape.

However, as you are explicitly asking about the Material3 Guidelines, I would suggest you to follow them like in the code example below:

@Composable
fun BottomAppBarExample() {
    Scaffold(
        bottomBar = {
            BottomAppBar(
                actions = {
                    IconButton(onClick = { /* do something */ }) {
                        Icon(Icons.Filled.Check, contentDescription = "Localized description")
                    }
                    IconButton(onClick = { /* do something */ }) {
                        Icon(
                            Icons.Filled.Edit,
                            contentDescription = "Localized description",
                        )
                    }
                    IconButton(onClick = { /* do something */ }) {
                        Icon(
                            Icons.Filled.Mic,
                            contentDescription = "Localized description",
                        )
                    }
                    IconButton(onClick = { /* do something */ }) {
                        Icon(
                            Icons.Filled.Image,
                            contentDescription = "Localized description",
                        )
                    }
                },
                floatingActionButton = {
                    FloatingActionButton(
                        onClick = { /* do something */ },
                        containerColor = BottomAppBarDefaults.bottomAppBarFabColor,
                        elevation = FloatingActionButtonDefaults.bottomAppBarFabElevation()
                    ) {
                        Icon(Icons.Filled.Add, "Localized description")
                    }
                }
            )
        },
    ) { innerPadding ->
        Text(
            modifier = Modifier.padding(innerPadding),
            text = "Example of a scaffold with a bottom app bar."
        )
    }
}

Output:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论