|
| 1 | + |
| 2 | +A Flutter widget that displays the native web color picker for browsers for use in Flutter Web apps. |
| 3 | + |
| 4 | +> [!NOTE] |
| 5 | +> This package supports only Flutter web. |
| 6 | +
|
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- **Default Browser Color Picker**: Uses the built-in color picker of web browsers. |
| 11 | +- **Callbacks for Color Change Events**: Notifies you when a color is selected or confirmed. |
| 12 | +- **Customizable Selector Appearance**: Allows you to display a custom widget as the color picker selector. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +To use the `web_color_picker` package in your Flutter Web application, follow these steps: |
| 17 | + |
| 18 | +### 1. Import the Package |
| 19 | + |
| 20 | +First, make sure to add `web_color_picker` to your `pubspec.yaml` dependencies. Then, import it in your Dart code: |
| 21 | + |
| 22 | +```dart |
| 23 | +import 'package:web_color_picker/web_color_picker.dart'; |
| 24 | +``` |
| 25 | + |
| 26 | +### 2. Using The `WebColorPicker` Widget |
| 27 | + |
| 28 | +#### To Display A Custom Color Picker Selector |
| 29 | + |
| 30 | +Use the `WebColorPicker.builder` constructor to display a custom color picker selector provided by the `builder` parameter. |
| 31 | + |
| 32 | +Tapping on the custom color picker will open the browser's color picker. |
| 33 | + |
| 34 | + |
| 35 | +An example showing how to use an `ElevatedButton` as the custom color picker selector: |
| 36 | + |
| 37 | +<img src="screenshots/web_color_picker_dot_builder.gif" alt="A gif showing how to use an `ElevatedButton` as the custom color picker selector" width="50%"> |
| 38 | + |
| 39 | +```dart |
| 40 | +WebColorPicker.builder( |
| 41 | + initialColor: textColor, |
| 42 | + builder: (context, selectedColor) { |
| 43 | + return ElevatedButton( |
| 44 | + onPressed: () { |
| 45 | + print('ElevatedButton pressed'); |
| 46 | + }, |
| 47 | + style: ElevatedButton.styleFrom( |
| 48 | + padding: const EdgeInsets.symmetric( |
| 49 | + horizontal: 8, |
| 50 | + vertical: 12, |
| 51 | + ), |
| 52 | + ), |
| 53 | + child: Row( |
| 54 | + mainAxisSize: MainAxisSize.min, |
| 55 | + children: [ |
| 56 | + Container( |
| 57 | + width: 20, |
| 58 | + height: 20, |
| 59 | + decoration: BoxDecoration( |
| 60 | + borderRadius: BorderRadius.circular(8), |
| 61 | + color: selectedColor, |
| 62 | + ), |
| 63 | + ), |
| 64 | + const SizedBox( |
| 65 | + width: 8, |
| 66 | + ), |
| 67 | + Text( |
| 68 | + 'Select color', |
| 69 | + ), |
| 70 | + ], |
| 71 | + ), |
| 72 | + ); |
| 73 | + }, |
| 74 | +) |
| 75 | +``` |
| 76 | + |
| 77 | +#### To Display The Default Color Picker Selector |
| 78 | + |
| 79 | +Use the `WebColorPicker` constructor to display the browser's default color picker selector. |
| 80 | + |
| 81 | +An example showing how to use the default color picker selector: |
| 82 | + |
| 83 | +<img src="screenshots/web_color_picker.gif" alt="A gif showing how to use the default color picker selector" width="50%"> |
| 84 | + |
| 85 | +```dart |
| 86 | +WebColorPicker( |
| 87 | + initialColor: Colors.red, |
| 88 | + width: 60.0, |
| 89 | + height: 30.0, |
| 90 | +) |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +### 3. Handle Color Change Events |
| 95 | + |
| 96 | +There are two main callbacks you can handle: |
| 97 | + |
| 98 | +- **onInput**: Triggered whenever a color is selected in the picker. |
| 99 | +- **onChange**: Triggered whenever the picker is dismissed and a color is confirmed. |
| 100 | + |
| 101 | +Each of these callbacks provides the selected `Color` and the corresponding HTML event: |
| 102 | + |
| 103 | +An example demonstrating the color picker events using the default color picker selector and |
| 104 | +a custom color picker selector: |
| 105 | + |
| 106 | +<img src="screenshots/web_color_picker_events_hq.gif" alt="A gif demonstrating the color picker events using the default color picker selector and a custom color picker selector" width="50%"> |
| 107 | + |
| 108 | +```dart |
| 109 | +WebColorPicker( |
| 110 | + initialColor: textColor, |
| 111 | + onInput: (color, event) { |
| 112 | + setState(() { |
| 113 | + previewTextColor = color; |
| 114 | + }); |
| 115 | + }, |
| 116 | + onChange: (color, event) { |
| 117 | + setState(() { |
| 118 | + textColor = color; |
| 119 | + }); |
| 120 | + }, |
| 121 | + // ... other properties |
| 122 | +), |
| 123 | +``` |
| 124 | + |
| 125 | +See the [example](example) directory for a complete example. |
| 126 | + |
| 127 | +## Resources |
| 128 | + |
| 129 | +For more understanding on how the HTML color input (which this package uses) works, check out the following: |
| 130 | + |
| 131 | +- [MDN Documentation: `<input type="color">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color): An in-depth overview of the HTML color input element. |
| 132 | + |
| 133 | +- [Using Color Inputs on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color#using_color_inputs): A guide that explains the user interactions and behavior of the color input. |
| 134 | + |
| 135 | +- [Color Inputs: A Deep Dive into Cross-Browser Differences | CSS-Tricks](https://css-tricks.com/color-inputs-a-deep-dive-into-cross-browser-differences/): An article that delves into the cross-browser differences of color inputs. |
| 136 | + |
| 137 | +### Apps Using This Library |
| 138 | + |
| 139 | +- [Flutter Gradient Generator](https://fluttergradientgenerator.com) - An online tool for creating and customizing gradients for use in Flutter applications. |
| 140 | + |
| 141 | + Check it out on [GitHub](https://github.com/victoreronmosele/flutter_gradient_generator). |
0 commit comments