Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

color picker

color picker

colorpicker is a highly configurable color picker for flutter. the colorpicker can show anywhere from 1 to 6 different color picking tools that you can use to choose colors from.

colorpickerallsize50-upper

color picker

the different color selection choices are:

  1. material primary colors and its shades. colorpickerswatch.material
  2. material accent colors and its shades. colorpickerswatch.accent
  3. material primary and accent colors and their shades in the same color picker. colorpickerswatch.both
  4. black and white colors, including near black and near white shades. colorpickerswatch.bw
  5. custom material like colors and their shades, that you define and name. colorpickerswatch.custom
  6. a hsv color wheel picker, that allows you to select any color. material primary color like shades for the selected color are computed automatically. colorpickerswatch.any

color picker

when you show more than one color swatch selection tool, a slider allows you to select which one to use. you can configure the color picker to include any of the above color swatches. showing options 1 and 2, together with option 3 is not very useful, they are available as optional ways of showing the material primary and accent colors.

you provide the picker with its heading and sub heading, usually a text widgets with appropriate style. you also define if the material shades can be selected or not and if the selected color names and values are shown in the picker. if the value is shown, the picker includes a button that allows you the copy the selected color code to the clipboard.

the shape, size and spacing of the color picker items can be modified. there is built in dialog that can be used to show the colorpicker in a pre-made dialog, but you can also make your own dialog and just use the colorpicker in your own custom dialog. the example below show some variations of the colorpicker.

colorpickerallsize50-lower

getting started

color picker

in the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  color_picker:

in your library file add the following import:

import 'package:color_picker/color_picker.dart';

example application

color picker

to try a basic example of the colorpicker on a device or simulator, download the colorpicker repository and run the example:

cd example/
flutter run --release

the result is a basic default picker and another picker that opens in a dialog, with a few more options.

colorpickersimpledemo

live web example

color picker

you can also try a live web example of the colorpicker here. with the web demo you can modify most of the colorpicker’s api values and use it as a tool to find settings and a style that you like and that fits your application.

the source code for the web demo, which is just a slightly more elaborate example than the basic sample bundled with the package, is available here.

colorpickerweb

introduction to the colorpicker

color picker

define a color variable and give it a start value in a normal statefulwidget.

class _colorpickerpagestate extends state<colorpickerpage> {
  color screenpickercolor;

@override
void initstate() {
  super.initstate();
  screenpickercolor = colors.blue[500];
}

color picker

add the colorpicker in your build method, could for example be in a listview or column. give it the starting color, a heading and subheading, plus the required oncolorchanged callback function. use the color from the callback in a setstate() to modify the screenpickercolor, to the selected color in the color picker.

colorpicker(
  color: screenpickercolor,
  oncolorchanged: (color color) =>
    setstate(() => screenpickercolor = color),
  heading: text(
    'select color',
    style: theme.of(context).texttheme.subtitle1,
  ),
  subheading: text(
    'select color shade',
    style: theme.of(context).texttheme.subtitle1,
  ),
),

color picker

the result will look like this:

colorpicker01

if you like, you can change the pick item style and size. let’s make it circular:

colorpicker(
  size: 44,
  borderradius: 22,

  color: screenpickercolor,
  oncolorchanged: (color color) =>
    setstate(() => screenpickercolor = color),
 :
),

you can use same the colorindicator widget that the colorpicker uses internally as a color indicator. here we use it in a listtile as its trailing property to show the selected color. the colorpicker also includes colortools that you can use to display names of the standard material colors and their shade index value. optionally it can also return a flutter style hex color code. we use it in the listtile subtitle property to describe the selected color.

listtile(
  title: const text('select color above to change this color'),
  subtitle: text(
    colortools.colornameandhexcode(screenpickercolor),
  ),
  trailing: colorindicator(
    height: 44,
    width: 44,
    borderradius: 22,
    color: screenpickercolor,
  ),
),

this setup results in round color pick items and a separate indicator for the selected color:

colorpicker02

colorpicker as a dialog

a common use case for a color picker is to show a color selection widget and allow the user to choose a new color in a dialog or cancel the color selection. the colorpicker comes with a built in dialog that can be used for this. alternatively you can just use the colorpicker widget to build your own dialog based on it.

for the dialog example we will show a few more of the built in picker options and also add some custom colors to the custom colors section of the colorpicker.

first we define custom colors and from the single color definitions we create primary and accent color swatches by using colortools.createprimarycolor or alternatively colortools.createaccentcolor for an accent color swatch of a defined color. we then add these color swatches to a colorswatch map, that we map to our own custom names for the color swatches.

  // define some custom colors.
  static const color googleprimary = color(0xff6200ee);
  static const color googleprimaryvariant = color(0xff3700b3);
  static const color googlesecondary = color(0xff03dac6);
  static const color googlesecondaryvariant = color(0xff018786);
  static const color googleerror = color(0xffb00020);
  static const color googleerrordark = color(0xffcf6679);
  static const color mrblue = color(0xff174378);

  // make a custom color swatch to name map from the above custom colors.
  final map<colorswatch<object>, string> colorsnamemap =
      <colorswatch<object>, string>{
    colortools.createprimarycolor(googleprimary): 'g purple',
    colortools.createprimarycolor(googleprimaryvariant): 'g purple variant',
    colortools.createaccentcolor(googlesecondary): 'g teal',
    colortools.createaccentcolor(googlesecondaryvariant): 'g teal variant',
    colortools.createprimarycolor(googleerror): 'g error',
    colortools.createprimarycolor(googleerrordark): 'g error dark',
    colortools.createprimarycolor(mrblue): 'mrblue',
  };

we also create a map from all the colorpickerswatch enum values to a boolean value, that defines which swatches we want to show in the colorpicker. in the example below we includes material primary, accent and the above custom colors plus the hsv color wheel that allows us to select any color. we did not include the picker that combines the primary and accent colors in the same picker, nor do we show the black and white and its near black and near white shades picker.

  static final map<colorpickerswatch, bool> swatchesavailable =
      <colorpickerswatch, bool>{
    colorpickerswatch.both: false,
    colorpickerswatch.material: true,
    colorpickerswatch.accent: true,
    colorpickerswatch.bw: false,
    colorpickerswatch.custom: true,
    colorpickerswatch.any: true,
  };

we add a variable for a our dialog picked color dialogpickercolor and set a start value for it.

class _colorpickerpagestate extends state<colorpickerpage> {
  color screenpickercolor;
  color dialogpickercolor;

@override
void initstate() {
  super.initstate();
  screenpickercolor = colors.blue[500];
  dialogpickercolor = colors.red[700];
}

we use another listtile to display a colorindicator, that we style a bit differently in this case. we also use its onselect callback to open a dialog with another colorpicker. before we open the dialog we store the current dialogpickercolor color that we intend to change with the dialog. this way we can restore this color value if the user cancels the dialog.

  listtile(
    title: const text('click this color to change it in a dialog'),
    subtitle: text(
      colortools.colornameandhexcode(
        dialogpickercolor,
        colorswatchnamemap: colorsnamemap,
      ),
    ),
    trailing: colorindicator(
      height: 40,
      width: 40,
      borderradius: 3,
      color: dialogpickercolor,
      onselect: () async {
        final color _colorbeforedialog = dialogpickercolor;
        if (!(await colorpickerdialog())) {
          setstate(() {
            dialogpickercolor = _colorbeforedialog;
          });
        }
      },
    ),
  ),

the colorpickerdialog is an asynchronous boolean function, that returns true if the user closed the dialog picker with the select button. if cancel was selected or user dismissed the dialog by clicking outside of it, false is returned.

  future<bool> colorpickerdialog() async {
    return colorpicker(
      color: dialogpickercolor,
      oncolorchanged: (color color) =>
          setstate(() => dialogpickercolor = color),
      shownameselected: true,
      size: 40,
      borderradius: 3,
      padding: 8,
      spacing: 3,
      runspacing: 3,
      heading: text(
        'select color',
        style: theme.of(context).texttheme.subtitle1,
      ),
      subheading: text(
        'select color shade',
        style: theme.of(context).texttheme.subtitle1,
      ),
      subwheelheading: text(
        'selected color and its material like shades',
        style: theme.of(context).texttheme.subtitle1,
      ),
      swatchavailable: swatchesavailable,
      colorswatchnamemap: colorsnamemap,
    ).showpickerdialog(
      context,
      constraints:
          const boxconstraints(minheight: 475, minwidth: 480, maxwidth: 480),
    );
  }

the above example uses a few more styling properties, but most importantly it uses the showpickerdialog method to show the defined colorpicker in a dialog. the dialog needs a context, so we pass it along to it.

we also define size constraints for the dialog. if you do not define size constraints, it will auto size to fit the dialog content. using constraints allows the dialog to keep the same size when the content size changes slightly as you switch between the different picker types you enabled. it looks better if the dialog size does not change when you switch picker type with the selector. the color wheel picker in particular will often require a bit more space. the wheel size can be customized as well, it does however become more difficult to operate with touch and less accurate if it is made very small.

the end result of the above setup is a listtile where the trailing color indicator widget can be clicked to open a dialog to select a new color for the trailing colorindicator color.

as the dialogpickercolor color is changed in the dialog, the colorindicator‘s color also changes interactively. if the selection is cancelled the colorindicator original color is restored.

colorpickersimpledemo

as an extra exercise, try connecting the dialogpickercolor value the appbar‘s backgroundcolor property. did you manage to do it? what happened?

:
appbar: appbar(
  backgroundcolor: dialogpickercolor,
  elevation: 1,
  centertitle: true,
  title: const text('color picker simple demo'),
),

as can be seen below, now the selected color in the dialog changes the appbar‘s color as you select color in the dialog and cancelling it restores the app bar color as well, pretty cool!

you can connect the color to theme colors and modify your application’s theme color values interactively as well, but that goes beyond the scope of this example.

colorpickersimpledemoappbar

digging deeper

the above introduction was a walk through of the bundled example, please see the example section for its complete source code.

there are more configuration options available for the colorpicker. use the api reference guide for additional information. you can also study the more complete live web example and its .


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.