Material Components for Flutter

repository·develop·Indexed 21 days ago

https://github.com/material-components/material-components-flutter

A set of widgets and tools to implement Material Design principles in Flutter applications. Included by default in the Flutter SDK via the material.dart library, it provides guidance and implementation details for components such as Card, MaterialBanner, BottomAppBar, and strategies for building advanced components like Backdrops.

Tokens
48.2K
Snippets
107
Records
155
Agent score
72%

What's inside MDC-Flutter

  1. Access Material Component documentation

    develop
    Engineering guidance for components within the material.dart library in Flutter can be found in this directory. For a more visual and interactive experience, it is recommended to view component documentation on the official material.io website. This includes documentation for components included in the material.dart library as well as those not included (such as backdrop).
  2. Understand the different types of Material chips

    develop

    Material chips are compact elements used to represent inputs, attributes, or actions. They should appear dynamically as a group of multiple interactive elements rather than as persistent buttons. There are four main types of chips, all of which are subclasses of the Chip class:

    1. Input chips: Represent complex information like an entity (person, place, or thing). They convert text into chips to verify input.
    2. Choice chips: Allow users to select a single option from a set. They serve as alternatives to radio buttons or single-select menus.
    3. Filter chips: Use tags or descriptive words to filter content. They are alternatives to checkboxes or toggle buttons.
    4. Action chips: Offer actions related to primary content and should appear contextually. They are alternatives to persistent buttons.
  3. Customize Menu Anatomy and Attributes

    develop

    Menus consist of several optional elements that can be configured via the PopupMenuButton's itemBuilder:

    • Leading Icon: Add an icon to a menu item by using a PopupMenuItem containing a ListTile with the leading parameter set to an Icon.
    • Text Label: Use a ListTile within a PopupMenuItem and set the title parameter to a Text widget. You can customize color and typography via the Text widget's style parameter.
    • Divider: Insert a PopupMenuDivider into the itemBuilder list to separate groups of items.
    • Command (Trailing Icon): Add an icon to the end of a menu item by using a ListTile within a PopupMenuItem and setting the trailing parameter to an Icon.
    • Selection State: To show a selection state (like a checkmark), use CheckedPopupMenuItem instead of PopupMenuItem within the itemBuilder list.
    • Container Color/Height: Use the Colors property or padding property within the PopupMenuButton widget context.
  4. Understand Slider types and anatomy

    develop

    Sliders allow users to select values from a range. They are categorized by two main dimensions:

    1. Selection Type

    • Single point slider: Uses one thumb to select a single value.
    • Range slider: Uses two thumbs to select a range of values.

    2. Value Continuity

    • Continuous sliders: Allow for any value within the range (no specific increments required).
    • Discrete sliders: Use divisions to snap to specific values and display numeric labels.

    Anatomy

    A slider consists of:

    • Track: The bar representing the range.
    • Thumb: The draggable handle(s).
    • Value label (optional): A popup showing the current value.
    • Tick mark (discrete sliders only): Visual indicators for specific values.
  5. Customize Data Table appearance via ThemeData

    develop

    Data table cells contain arbitrary widgets, so most styling (typography, colors) must be applied directly to the widgets inside DataCell or DataColumn (e.g., using TextStyle within a Text widget).

    However, certain structural elements are controlled globally via ThemeData:

    • Stroke/Divider Color: Set dividerColor in ThemeData to change the color of the lines separating rows/columns.
    • Row Checkbox Color: Set accentColor in ThemeData to change the color of active checkboxes.
    • Header Background: Use secondaryHeaderColor in ThemeData to style the column header area.
    • General Dividers: The dividerColor property affects the container's stroke color.
  6. Create a Bottom Navigation Drawer

    develop

    Flutter does not have a built-in BottomDrawer widget. To implement a bottom navigation drawer, you must compose a custom Widget using ListTile, Divider, and Text widgets.

    Implementation Pattern:

    1. Animation: Use a PositionedTransition to animate the visibility of the drawer.
    2. Trigger: The opening and closing of the drawer should be triggered by a menu icon within a BottomAppBar located in the bottomNavigationBar slot of a Scaffold.
    3. Usage: Bottom drawers are typically used in conjunction with bottom app bars.
    bottomNavigationBar: BottomAppBar(
      child: Row(
        children: [
          IconButton(
            icon: Icon(Icons.menu),
            onPressed: () {
              // Animate a bottom drawer
            },
          ),
          Spacer(),
          IconButton(icon: Icon(Icons.search), onPressed: () {}),
          IconButton(icon: Icon(Icons.more_vert), onPressed: () {}),
        ],
      ),
    ),
  7. MaterialBanner Anatomy and Properties

    develop

    The MaterialBanner widget is composed of several key parts, each controlled by specific properties:

    ComponentPropertyDescription
    Supporting illustrationleadingAn optional widget (e.g., an icon or avatar) displayed at the start of the banner.
    ContainerbackgroundColorThe background color of the banner container.
    Text labelcontentThe main message text. You can use the style property on the Text widget within content to control typography and color.
    ButtonsactionsA list of widgets (typically buttons) provided for user interaction.
  8. How standard bottom sheets work

    develop

    Standard bottom sheets are surfaces that co-exist with the screen's main UI. They allow users to view and interact with both the sheet and the main content simultaneously. They are ideal for secondary content or features that need to remain visible while the user scrolls or pans the main UI.

    To implement a standard bottom sheet, use the showBottomSheet function. Note that you often need to use a Builder widget to provide a BuildContext that is a descendant of the Scaffold.

    For more advanced dragging behavior and snap points, consider using a DraggableScrollableSheet.

    showBottomSheet(
      context: context,
      builder: (context) {
        return Wrap(
          children: [
            ListTile(title: Text('Item 1')),
            ListTile(title: Text('Item 2')),
          ],
        );
      },
    );
  9. How modal bottom sheets work

    develop

    Modal bottom sheets present a set of choices while blocking interaction with the rest of the screen using a scrim. They are an alternative to inline menus and dialogs on mobile, providing more space for content, icons, and actions. Modal bottom sheets are intended for mobile use only.

    showModalBottomSheet(
      context: context,
      builder: (context) {
        return Wrap(
          children: [
            ListTile(leading: Icon(Icons.share), title: Text('Share')),
            ListTile(leading: Icon(Icons.link), title: Text('Get link')),
          ],
        );
      },
    );