Material Foundation Flutter Packages

repository·main·Indexed 21 days ago

https://github.com/material-foundation/flutter-packages

A collection of Flutter packages developed by the Material Design team to provide official Material Design implementations. This repository primarily hosts the dynamic_color package, which enables Material You dynamic color schemes in Flutter applications via the DynamicColorBuilder widget and color harmonization methods.

Tokens
2K
Snippets
9
Records
11
Agent score
75%

What's inside material-foundation-flutter-packages

  1. Overview of Material Design pub packages

    main
    This repository contains the source code for various Material Design packages originally developed by the Material Flutter team. The primary package hosted in this specific repository is dynamic_color, which facilitates implementing Material You dynamic color schemes in Flutter applications.
  2. Customize iOS launch screen assets

    main

    To change the launch screen image for the iOS version of your Flutter app, you can either replace the image files directly in the project directory or use Xcode.

    Method 1: Direct File Replacement

    Replace the existing image files located in the packages/dynamic_color/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.

    Method 2: Using Xcode

    1. Open your Flutter project's iOS workspace using the command: open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch images.
    open ios/Runner.xcworkspace
  3. Mock dynamic colors for testing

    main

    When writing widget tests, you can use DynamicColorTestingUtils to simulate specific dynamic color palettes. Use setMockDynamicColors() in your setUp block to ensure a consistent state for every test.

    import 'package:dynamic_color/test_utils.dart';
    import 'package:dynamic_color/samples.dart';
    
    void main() {
      // Reset for every test
      setUp(() => DynamicColorTestingUtils.setMockDynamicColors());
    
      testWidgets('Verify dynamic core palette is used ',
          (WidgetTester tester) async {
        DynamicColorTestingUtils.setMockDynamicColors(
          corePalette: SampleCorePalettes.green,
        );
    
        // ...
      });
    }
  4. Harmonize colors and color schemes

    main

    Harmonization shifts the hue and chroma of colors to make them feel more cohesive with the user's dynamic color scheme. The package provides two extension methods:

    1. Color.harmonizeWith(Color target): Shifts the hue of the calling Color towards the target color. This is useful for making custom brand colors blend with the dynamic theme.
    2. ColorScheme.harmonized(): Returns a new ColorScheme where the built-in semantic colors are harmonized with the current scheme.
    Color color = Colors.red;
    // Shift's [color]'s hue towards the (dynamic) color scheme's primary color.
    harmonizedColor = color.harmonizeWith(colorScheme.primary);
    
    // Harmonizes the entire ColorScheme's built-in semantic colors.
    harmonizedColorScheme = colorScheme.harmonized();
  5. Use DynamicColorBuilder to access device dynamic colors

    main

    The DynamicColorBuilder is a stateful widget that retrieves the device's dynamic colors via an underlying platform plugin. It provides two ColorScheme objects through its builder function: one for light mode and one for dark mode. If the platform does not support dynamic colors, these parameters will be null.

    import 'package:dynamic_color/dynamic_color.dart';
    
    DynamicColorBuilder(
      builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
        // Use lightDynamic and darkDynamic to build your app's theme
        return ...;
      },
    ),
  6. Use SampleColorSchemes for mocking ColorSchemes

    main

    The SampleColorSchemes class provides helper methods to generate ColorScheme objects based on the sample core palettes. You must provide a Brightness (either Brightness.light or Brightness.dark) to the method to get the appropriate scheme.

    import 'package:dynamic_color/dynamic_color.dart';
    import 'package:flutter/material.dart';
    
    // Generate a light green color scheme
    ColorScheme lightGreenScheme = SampleColorSchemes.green(Brightness.light);
    
    // Generate a dark orange color scheme
    ColorScheme darkOrangeScheme = SampleColorSchemes.orange(Brightness.dark);
  7. Use SampleCorePalettes for mocking dynamic colors

    main

    The SampleCorePalettes class provides pre-defined CorePalette instances. These are useful for mocking dynamic color behavior during testing or development without needing a device that supports actual dynamic color (like Android 12+).

    import 'package:dynamic_color/dynamic_color.dart';
    
    // Accessing sample palettes
    CorePalette myMockPalette = SampleCorePalettes.green;
  8. Use DynamicColorBuilder to apply dynamic colors

    main

    The DynamicColorBuilder is a stateful widget that automatically retrieves dynamic color information from the host platform and provides a light and dark ColorScheme via its builder function.

    Platform Behavior:

    • Android: Retrieves a CorePalette from the OS to construct the ColorSchemes.
    • macOS, Windows, and Linux: Retrieves an accent Color from the system to construct ColorSchemes using ColorScheme.fromSeed.
    • Fallback: If dynamic color is not supported or the OS has not yet responded, the provided ColorSchemes will be null.

    To use it, wrap your application (or a specific part of it) with DynamicColorBuilder and use the provided lightDynamic and darkDynamic color schemes to configure your ThemeData.

    DynamicColorBuilder(
      builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
        return MaterialApp(
          theme: ThemeData(
            colorScheme: lightDynamic ?? ColorScheme.fromSeed(seedColor: Colors.blue),
          ),
          darkTheme: ThemeData(
            colorScheme: darkDynamic ?? ColorScheme.fromSeed(seedColor: Colors.blue, brightness: Brightness.dark),
          ),
          home: const MyHomePage(),
        );
      },
    )