showMaterialColorPicker function

Future<Color?> showMaterialColorPicker({
  1. required BuildContext context,
  2. String title = "Pick a color",
  3. required Color selectedColor,
  4. Color? headerColor,
  5. Color? headerTextColor,
  6. Color? backgroundColor,
  7. Color? buttonTextColor,
  8. String? confirmText,
  9. String? cancelText,
  10. double? maxLongSide,
  11. double? maxShortSide,
  12. ValueChanged<Color>? onChanged,
  13. VoidCallback? onConfirmed,
  14. bool cancellable = true,
  15. VoidCallback? onCancelled,
})

Allows RGB selection of a color.

Implementation

Future<Color?> showMaterialColorPicker({
  required BuildContext context,

  /// The title for the dialog box
  String title = "Pick a color",

  /// The color that is initially selected
  required Color selectedColor,

  /// The dialog header color (overrides theme)
  Color? headerColor,

  /// The dialog header text color (overrides theme)
  Color? headerTextColor,

  /// The dialog background color (overrides theme)
  Color? backgroundColor,

  /// The button text color (overrides theme)
  Color? buttonTextColor,

  /// Text to display in the confirm button
  String? confirmText,

  /// Text to display in the cancel button
  String? cancelText,

  /// Used to restrict how tall the dialog can be.
  double? maxLongSide,

  /// Used to restrict how wide the dialog can be.
  double? maxShortSide,

  /// Function that gets called when the value is changed
  ValueChanged<Color>? onChanged,

  /// Function that gets called when the confirm button is pressed
  VoidCallback? onConfirmed,

  /// Whether to display a cancel button
  bool cancellable = true,

  /// Function that gets called when the cancel button is pressed
  VoidCallback? onCancelled,
}) {
  return showDialog<Color>(
    context: context,
    barrierDismissible: cancellable,
    builder: (BuildContext context) {
      return OrientationBuilder(
        builder: (context, orientation) {
          return ResponsiveDialog(
            context: context,
            title: title,
            headerColor: headerColor,
            headerTextColor: headerTextColor,
            backgroundColor: backgroundColor,
            buttonTextColor: buttonTextColor,
            confirmText: confirmText,
            cancelText: cancelText,
            maxLongSide: maxLongSide,
            maxShortSide: maxLongSide,
            forcePortrait: true,
            cancelButtonVisible: cancellable,
            child: SingleChildScrollView(
              child: ColorPicker(
                pickerColor: selectedColor,
                onColorChanged: (color) => selectedColor = color,
                colorPickerWidth: 1000.0,
                pickerAreaHeightPercent: 0.3,
                enableAlpha: true,
                displayThumbColor: true,
                paletteType: PaletteType.hsv,
              ),
            ),
            okPressed: () => Navigator.of(context).pop(selectedColor),
          );
        },
      );
    },
  ).then((selection) {
    if (selection != null) {
      onChanged?.call(selection);
      onConfirmed?.call();
    } else {
      onCancelled?.call();
    }
    return selection;
  });
}