showExpressiveNavigationDrawer function

Future<int?> showExpressiveNavigationDrawer(
  1. BuildContext context, {
  2. required List<ExpressiveNavItem> destinations,
  3. required int selectedIndex,
  4. Widget? header,
  5. ExpressiveComponentThemeData? style,
  6. bool useRootNavigator = true,
})

Raises the expanded rail modally over a scrim, from the leading edge (Compose's ModalWideNavigationRail, expanded and hidden when collapsed): the navigation drawer of a phone, where a bar has no room for every destination. It slides in on the fast spatial spring, the modal container colour, its end corners large, its indicators spanning it as ExpressiveNavigationDrawer's do; a tap on the scrim or the system back closes it. Resolves to the index chosen, or null.

final index = await showExpressiveNavigationDrawer(context, destinations: destinations, selectedIndex: page);

Implementation

Future<int?> showExpressiveNavigationDrawer(
  BuildContext context, {
  required List<ExpressiveNavItem> destinations,
  required int selectedIndex,
  Widget? header,
  ExpressiveComponentThemeData? style,
  bool useRootNavigator = true,
}) {
  final navigator = Navigator.of(context, rootNavigator: useRootNavigator);
  final colors = context.expressiveTheme.colors;
  return navigator.push<int>(
    ExpressivePopupRoute<int>(
      spring: ExpressiveMotion.of(context)
          .spring(ExpressiveMotionKey.fastSpatial),
      capturedThemes: InheritedTheme.capture(
        from: context,
        to: navigator.context,
      ),
      barrierColor: colors
          .role(ScrimTokens.containerColor)
          .withValues(alpha: ScrimTokens.containerOpacity),
      barrierLabel: Localizations.of<MaterialLocalizations>(
        context,
        MaterialLocalizations,
      )?.modalBarrierDismissLabel,
      // In from the leading edge; a spring's overshoot never lifts it off
      // the edge.
      transitionBuilder: (context, animation, child) => AnimatedBuilder(
        animation: animation,
        child: child,
        builder: (context, child) => FractionalTranslation(
          translation: Offset(
            (Directionality.of(context) == TextDirection.ltr ? -1 : 1) *
                (1 - animation.value.clamp(0.0, 1.0)),
            0,
          ),
          child: child,
        ),
      ),
      builder: (context) => Align(
        alignment: AlignmentDirectional.centerStart,
        child: Semantics(
          scopesRoute: true,
          namesRoute: true,
          explicitChildNodes: true,
          label: Localizations.of<MaterialLocalizations>(
            context,
            MaterialLocalizations,
          )?.drawerLabel,
          child: ExpressiveNavigationRail._modal(
            destinations: destinations,
            selectedIndex: selectedIndex,
            header: header,
            style: style,
            onDestinationSelected: (index) => Navigator.of(context).pop(index),
          ),
        ),
      ),
    ),
  );
}