Skip to content

TopAppBar

TopAppBar is a top application bar component in Miuix, used to provide navigation, title, and action buttons at the top of the interface. It supports both large title and regular modes, as well as dynamic effects during scrolling.

This component is typically used in conjunction with the Scaffold component to maintain consistent layout and behavior across different pages in the application.

Import

kotlin
import top.yukonga.miuix.kmp.basic.TopAppBar
import top.yukonga.miuix.kmp.basic.SmallTopAppBar
import top.yukonga.miuix.kmp.basic.MiuixScrollBehavior
import top.yukonga.miuix.kmp.basic.rememberTopAppBarState

Basic Usage

Small TopAppBar

kotlin
Scaffold(
    topBar = {
        SmallTopAppBar(
            title = "Title",
            navigationIcon = {
                IconButton(onClick = { /* Handle click event */ }) {
                    Icon(MiuixIcons.Back, contentDescription = "Back")
                }
            },
            actions = {
                IconButton(onClick = { /* Handle click event */ }) {
                    Icon(MiuixIcons.More, contentDescription = "More")
                }
            }
        )
    }
)

Large TopAppBar

kotlin
Scaffold(
    topBar = {
        TopAppBar(
            title = "Title",
            largeTitle = "Large Title", // If not specified, title value will be used
            navigationIcon = {
                IconButton(onClick = { /* Handle click event */ }) {
                    Icon(MiuixIcons.Back, contentDescription = "Back")
                }
            },
            actions = {
                IconButton(onClick = { /* Handle click event */ }) {
                    Icon(MiuixIcons.More, contentDescription = "More")
                }
            }
        )
    }
)

Large TopAppBar Scroll Behavior (Using Scaffold)

TopAppBar supports changing its display state when content scrolls:

kotlin
val scrollBehavior = MiuixScrollBehavior()

Scaffold(
    topBar = {
        TopAppBar(
            title = "Title",
            largeTitle = "Large Title", // If not specified, title value will be used
            scrollBehavior = scrollBehavior
        )
    }
) { paddingValues ->
    // Content area needs to consider padding
    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            // If you want to add the overscroll effect, please add it before the scroll behavior
            .overScrollVertical()
            // Bind TopAppBar scroll behavior
            .nestedScroll(scrollBehavior.nestedScrollConnection),
        contentPadding = PaddingValues(top = paddingValues.calculateTopPadding())
    ) {
        // List content
    }
}

Custom Styles

Custom Colors

kotlin
TopAppBar(
    title = "Title",
    color = MiuixTheme.colorScheme.primary,
    titleColor = MiuixTheme.colorScheme.onPrimary,
    largeTitleColor = MiuixTheme.colorScheme.onPrimary
)

Custom Content Padding

kotlin
TopAppBar(
    title = "Title",
    titlePadding = 32.dp
)

Custom Icon Padding

kotlin
TopAppBar(
    title = "Title",
    navigationIconPadding = 12.dp,
    actionIconPadding = 12.dp
)

Properties

TopAppBar Properties

Property NameTypeDescriptionDefault ValueRequired
titleStringTop bar title-Yes
modifierModifierModifier applied to the top barModifierNo
colorColorTop bar background colorMiuixTheme.colorScheme.surfaceNo
titleColorColorColor of the collapsed small title textMiuixTheme.colorScheme.onSurfaceNo
largeTitleStringLarge title texttitleNo
largeTitleColorColorColor of the expanded large title textMiuixTheme.colorScheme.onSurfaceNo
subtitleStringSubtitle text displayed below the title bar""No
subtitleColorColorColor of the subtitle textMiuixTheme.colorScheme.onSurfaceVariantSummaryNo
navigationIcon@Composable () -> UnitComposable function for navigation icon area{}No
actions@Composable RowScope.() -> UnitComposable function for action buttons area{}No
scrollBehaviorScrollBehavior?Controls top bar scroll behaviornullNo
defaultWindowInsetsPaddingBooleanWhether to apply default window insets paddingtrueNo
titlePaddingDpHorizontal content paddingTopAppBarDefaults.TitlePaddingNo
navigationIconPaddingDpStart padding of the navigation iconTopAppBarDefaults.NavigationIconPaddingNo
actionIconPaddingDpEnd padding of the action iconsTopAppBarDefaults.ActionIconPaddingNo
bottomContent@Composable () -> UnitComposable content displayed below the title bar area{}No

SmallTopAppBar Properties

Property NameTypeDescriptionDefault ValueRequired
titleStringTop bar title-Yes
modifierModifierModifier applied to the top barModifierNo
colorColorTop bar background colorMiuixTheme.colorScheme.surfaceNo
titleColorColorColor of the title textMiuixTheme.colorScheme.onSurfaceNo
subtitleStringSubtitle text displayed below the title bar""No
subtitleColorColorColor of the subtitle textMiuixTheme.colorScheme.onSurfaceVariantSummaryNo
navigationIcon@Composable () -> UnitComposable function for navigation icon area{}No
actions@Composable RowScope.() -> UnitComposable function for action buttons area{}No
scrollBehaviorScrollBehavior?Controls top bar scroll behaviornullNo
defaultWindowInsetsPaddingBooleanWhether to apply default window insets paddingtrueNo
titlePaddingDpHorizontal content paddingTopAppBarDefaults.TitlePaddingNo
navigationIconPaddingDpStart padding of the navigation iconTopAppBarDefaults.NavigationIconPaddingNo
actionIconPaddingDpEnd padding of the action iconsTopAppBarDefaults.ActionIconPaddingNo
bottomContent@Composable () -> UnitComposable content displayed below the title bar area{}No

TopAppBarDefaults Object

The TopAppBarDefaults object provides default values for TopAppBar and SmallTopAppBar components.

Constants

Constant NameTypeDescriptionDefault Value
TitlePaddingDpHorizontal padding of the title and large title26.dp
NavigationIconPaddingDpStart padding of the navigation icon16.dp
ActionIconPaddingDpEnd padding of the action icons16.dp
CollapsedHeightDpCollapsed height of the TopAppBar52.dp
SmallTopAppBarCenterHeightDpVertical center height for SmallTopAppBar layout50.dp
LargeTitleBottomPaddingDpBottom padding below the large title when no subtitle is present4.dp
SubtitleBottomPaddingDpBottom padding below the subtitle (both large and small)8.dp

ScrollBehavior

MiuixScrollBehavior is a configuration object used to control the scroll behavior of the top bar.

rememberTopAppBarState

Used to create and remember TopAppBarState:

kotlin
val scrollBehavior = MiuixScrollBehavior(
    state = rememberTopAppBarState(),
    snapAnimationSpec = spring(stiffness = 2500f),
    canScroll = { true }
)
Parameter NameTypeDefault ValueDescription
stateTopAppBarStaterememberTopAppBarState()State object controlling scroll state
canScroll() -> BooleanCallback to control whether scrolling is allowed
snapAnimationSpecAnimationSpec<Float>?spring(stiffness = 2500f)Defines snap animation after scrolling
flingAnimationSpecDecayAnimationSpec<Float>?rememberSplineBasedDecay()Defines decay animation for fling

Advanced Usage

Handling Window Insets

kotlin
TopAppBar(
    title = "Title",
    largeTitle = "Large Title",
    defaultWindowInsetsPadding = false // Handle window insets manually
)

Custom Scroll Behavior Animation

kotlin
var isScrollingEnabled by remember { mutableStateOf(true) }
val scrollBehavior = MiuixScrollBehavior(
    snapAnimationSpec = tween(durationMillis = 100),
    flingAnimationSpec = rememberSplineBasedDecay(),
    canScroll = { isScrollingEnabled } // Can dynamically control whether scrolling is allowed
)

TopAppBar(
    title = "Title",
    largeTitle = "Large Title",
    scrollBehavior = scrollBehavior
)

Combining Large and Small Titles

kotlin
var useSmallTopBar by remember { mutableStateOf(false) }

Box(modifier = Modifier.fillMaxSize()) {
    if (useSmallTopBar) {
        SmallTopAppBar(
            title = "Compact Mode",
            navigationIcon = {
                IconButton(onClick = { useSmallTopBar = false }) {
                    Icon(
                        imageVector = MiuixIcons.Back,
                        contentDescription = "Switch to Large Title",
                        tint = MiuixTheme.colorScheme.onBackground
                    )
                }
            }
        )
    } else {
        TopAppBar(
            title = "Title",
            largeTitle = "Expanded Mode",
            navigationIcon = {
                IconButton(onClick = { useSmallTopBar = true }) {
                    Icon(
                        imageVector = MiuixIcons.Back,
                        contentDescription = "Switch to Small Title",
                        tint = MiuixTheme.colorScheme.onBackground
                    )
                }
            }
        )
    }
}

Changelog

Released under the Apache-2.0 License