flutter_colorpicker

repository·master·Indexed 18 days ago

https://github.com/mchome/flutter_colorpicker

A Flutter package providing various color picking widgets, including ColorPicker (supporting HSV, HSL, RGB, and Material modes), MaterialPicker, BlockPicker, MultipleChoiceBlockPicker, SlidePicker, and HueRingPicker. It features support for alpha channels, hex input synchronization via TextEditingController, and customizable layouts through PickerLayoutBuilder and PickerItemBuilder.

Tokens
3.7K
Snippets
9
Records
13
Agent score
63%

What's inside flutter_colorpicker

  1. Available color picker widgets

    master

    The flutter_colorpicker package provides several different widget types depending on the desired user experience:

    • ColorPicker: A versatile picker supporting HSV(HSB), HSL, RGB, and Material color modes.
    • MaterialPicker: A picker styled with Material Design principles. It includes a showLabel option (primarily for portrait mode).
    • BlockPicker: A picker that presents colors as discrete blocks.
    • MultipleChoiceBlockPicker: A picker designed for selecting multiple colors from a predefined list using pickerColors and onColorsChanged.
    // Use ColorPicker:
    ColorPicker(
      pickerColor: pickerColor,
      onColorChanged: changeColor,
    )
    
    // Use MaterialPicker:
    MaterialPicker(
      pickerColor: pickerColor,
      onColorChanged: changeColor,
      showLabel: true,
    )
    
    // Use BlockPicker:
    BlockPicker(
      pickerColor: currentColor,
      onColorChanged: changeColor,
    )
    
    // Use MultipleChoiceBlockPicker:
    MultipleChoiceBlockPicker(
      pickerColors: currentColors,
      onColorsChanged: changeColors,
    )
  2. Use flutter_colorpicker in a showDialog

    master

    To implement a color picker in your Flutter application, you can wrap one of the provided picker widgets inside a showDialog using an AlertDialog. You must manage the state of the selected color (e.g., using setState) to update the picker as the user interacts with it. Once the user confirms their selection (e.g., by clicking a 'Got it' button), you can update your application's primary color state and close the dialog.

    // create some values
    Color pickerColor = Color(0xff443a49);
    Color currentColor = Color(0xff443a49);
    
    // ValueChanged<Color> callback
    void changeColor(Color color) {
      setState(() => pickerColor = color);
    }
    
    // raise the [showDialog] widget
    showDialog(
      context: context,
      child: AlertDialog(
        title: const Text('Pick a color!'),
        content: SingleChildScrollView(
          child: ColorPicker(
            pickerColor: pickerColor,
            onColorChanged: changeColor,
          ),
        ),
        actions: <Widget>[
          ElevatedButton(
            child: const Text('Got it'),
            onPressed: () {
              setState(() => currentColor = pickerColor);
              Navigator.of(context).pop();
            },
          ),
        ],
      ),
    )
  3. Customize BlockPicker layout and item appearance

    master

    Both BlockPicker and MultipleChoiceBlockPicker provide hooks to completely redefine their UI via layoutBuilder and itemBuilder.

    PickerLayoutBuilder

    Used to define the container and grid structure. It provides:

    • BuildContext context
    • List<Color> colors: The list of available colors.
    • PickerItem child: A function that returns a widget for a specific color.

    PickerItemBuilder

    Used to define the look of an individual color block. It provides:

    • Color color: The color of the block.
    • bool isCurrentColor: Whether this color is currently selected.
    • void Function() changeColor: The callback to trigger when the block is tapped.
  4. Sync color selection with a TextEditingController

    master

    You can synchronize the ColorPicker with an external text field (like TextField or CupertinoTextField) by providing a TextEditingController to the hexInputController parameter.

    When the user types a valid hex string, the picker updates automatically. The supported formats are:

    • RGB (e.g., FF0000)
    • #RGB (e.g., #F00)
    • RRGGBB (e.g., FF0000)
    • #RRGGBB (e.g., #FF0000)
    • AARRGGBB (e.g., FFFF0000)
    • #AARRGGBB (e.g., #FFFF0000)

    Note: The picker respects the enableAlpha flag; if alpha is disabled, hex inputs with transparency will be converted to non-transparent values.

    final textController = TextEditingController(text: '#2F19DB');
    
    // In your widget tree:
    Column(
      children: [
        ColorPicker(
          pickerColor: currentColor,
          onColorChanged: changeColor,
          hexInputController: textController,
          enableAlpha: true,
        ),
        CupertinoTextField(
          controller: textController,
          // ... other text field config
        ),
      ],
    )
  5. Use the ColorPicker widget

    master

    The ColorPicker is a comprehensive widget for selecting colors using various models (HSV, HSL, RGB) and layouts. It supports a color area, sliders, color history, and hex input via a TextEditingController.

    ColorPicker(
      pickerColor: currentColor,
      onColorChanged: changeColor,
      colorPickerWidth: 300.0,
      pickerAreaHeightPercent: 0.7,
      enableAlpha: true,
      displayThumbColor: true,
      showLabel: true,
      paletteType: PaletteType.hsv,
      pickerAreaBorderRadius: const BorderRadius.only(
        topLeft: Radius.circular(2.0),
        topRight: Radius.circular(2.0),
      ),
      hexInputController: textController, // Allows manual hex entry
      portraitOnly: true,
    )
  6. Use the BlockPicker widget for single color selection

    master

    The BlockPicker widget allows users to select a single color from a grid of blocks. You can customize the available colors, the layout of the grid, and the visual shape of the color items.

    Parameters

    • pickerColor: The currently selected Color.
    • onColorChanged: A callback function ValueChanged<Color> triggered when a new color is selected.
    • availableColors: A List<Color> of colors to display in the picker. Defaults to a predefined set of 20 colors.
    • useInShowDialog: A boolean that affects how the 'current color' state is calculated (useful when the picker is inside a dialog).
    • layoutBuilder: A PickerLayoutBuilder function to customize the grid layout.
    • itemBuilder: A PickerItemBuilder function to customize the appearance of each color block.
    BlockPicker(
      pickerColor: Colors.blue,
      onColorChanged: (color) {
        print('Selected color: $color');
      },
    )
  7. Use the MultipleChoiceBlockPicker widget for multiple color selection

    master

    The MultipleChoiceBlockPicker widget allows users to select or deselect multiple colors from a grid. Tapping a color block toggles its selection state.

    Parameters

    • pickerColors: A List<Color>? representing the currently selected colors.
    • onColorsChanged: A callback function ValueChanged<List<Color>> triggered when the selection changes.
    • availableColors: A List<Color> of colors to display in the picker.
    • useInShowDialog: A boolean that affects how the 'current color' state is calculated.
    • layoutBuilder: A PickerLayoutBuilder function to customize the grid layout.
    • itemBuilder: A PickerItemBuilder function to customize the appearance of each color block.
    MultipleChoiceBlockPicker(
      pickerColors: [Colors.red, Colors.blue],
      onColorsChanged: (colors) {
        print('Selected colors: $colors');
      },
    )
  8. Use the HueRingPicker widget

    master

    The HueRingPicker provides a circular hue ring combined with a color area (for saturation/value) and an optional alpha slider. It is ideal for a compact, visual selection experience.

    HueRingPicker(
      pickerColor: currentColor,
      onColorChanged: changeColor,
      portraitOnly: false,
      colorPickerHeight: 250.0,
      hueRingStrokeWidth: 20.0,
      enableAlpha: true,
      displayThumbColor: true,
      disableTextInput: false,
      pickerAreaBorderRadius: const BorderRadius.all(Radius.zero),
    )
  9. Use the MaterialPicker widget

    master

    The MaterialPicker widget provides a user interface for selecting colors from Material Design palettes. It displays a list of primary color types (e.g., Red, Blue, Green) and, upon selection, shows a list of available shades for that specific color.

    Parameters

    • pickerColor: The currently selected Color. This is used to initialize the picker's state.
    • onColorChanged: A callback function triggered whenever a specific shade is selected. It receives the selected Color.
    • onPrimaryChanged: (Optional) A callback function triggered when a new primary color type is selected. It receives the Color representing the primary color.
    • enableLabel: (Optional) A boolean that, if true, displays text labels (color names and hex codes) next to the shades. Defaults to false.
    • portraitOnly: (Optional) A boolean that forces the picker to use the portrait layout regardless of device orientation. Defaults to false.
    MaterialPicker(
      pickerColor: Colors.blue,
      onColorChanged: (Color color) {
        print('Selected color: $color');
      },
      onPrimaryChanged: (Color primaryColor) {
        print('Selected primary color: $primaryColor');
      },
      enableLabel: true,
      portraitOnly: false,
    )
  10. Use the SlidePicker widget

    master

    The SlidePicker is a specialized widget that provides color selection exclusively through sliders. It supports ColorModel.rgb, ColorModel.hsv, and ColorModel.hsl.

    SlidePicker(
      pickerColor: currentColor,
      onColorChanged: changeColor,
      colorModel: ColorModel.rgb,
      enableAlpha: true,
      sliderSize: const Size(260, 40),
      showSliderText: true,
      showParams: true,
      showLabel: true,
      labelTypes: [ColorLabelType.rgb],
      showIndicator: true,
      indicatorSize: const Size(280, 50),
      displayThumbColor: true,
    )
  11. Configure ColorPicker options

    master

    The ColorPicker widget accepts several configuration parameters to customize its behavior and appearance:

    • pickerColor: The initial color.
    • onColorChanged: Callback triggered when the color changes.
    • pickerHsvColor: Optional initial HSVColor.
    • onHsvColorChanged: Optional callback for HSVColor changes.
    • paletteType: Sets the color model (e.g., PaletteType.hsv, PaletteType.hsl, PaletteType.rgbWithBlue).
    • enableAlpha: Enables/disables transparency support.
    • showLabel: Whether to show color value labels (use empty labelTypes to disable).
    • labelTypes: A list of ColorLabelType to display (e.g., ColorLabelType.rgb, ColorLabelType.hsv).
    • displayThumbColor: Whether to show the current color on the slider thumb.
    • portraitOnly: Forces a portrait layout regardless of device orientation.
    • colorPickerWidth: The width of the picker.
    • pickerAreaHeightPercent: The height ratio of the color area.
    • pickerAreaBorderRadius: Border radius for the color area.
    • hexInputBar: Enables/disables the hex input bar.
    • hexInputController: A TextEditingController to sync hex text input with the picker.
    • colorHistory: A list of previously selected colors.
    • onHistoryChanged: Callback when the color history is updated.
  12. Configure HueRingPicker options

    master

    The HueRingPicker widget accepts the following configuration:

    • pickerColor: The initial color.
    • onColorChanged: Callback triggered when the color changes.
    • portraitOnly: Forces a portrait layout.
    • colorPickerHeight: The height of the picker.
    • hueRingStrokeWidth: The thickness of the hue ring.
    • enableAlpha: Enables/disables the alpha slider.
    • displayThumbColor: Whether to show the current color on the thumb.
    • disableTextInput: Disables manual hex text editing.
    • pickerAreaBorderRadius: Border radius for the color area.