-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from robiness/add-samples-to-color-picker
Add samples to color picker
- Loading branch information
Showing
7 changed files
with
227 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:stage_craft/stage_craft.dart'; | ||
|
||
class ExampleData { | ||
static const colorSamples = [ | ||
ColorSample( | ||
color: Colors.teal, | ||
name: 'StgColors.teal', | ||
), | ||
ColorSample( | ||
color: Colors.amber, | ||
name: 'StgColors.amber', | ||
), | ||
ColorSample( | ||
color: Colors.indigo, | ||
name: 'StgColors.indigo', | ||
), | ||
ColorSample( | ||
color: Colors.blueAccent, | ||
name: 'StgColors.blueAccent', | ||
), | ||
ColorSample( | ||
color: Colors.lime, | ||
name: 'StgColors.lime', | ||
), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,9 @@ dependencies: | |
|
||
|
||
dev_dependencies: | ||
lint: ^2.1.2 | ||
flutter_test: | ||
sdk: flutter | ||
lint: ^2.1.2 | ||
|
||
|
||
flutter: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_colorpicker/flutter_colorpicker.dart' as picker; | ||
|
||
/// A color picking interface with two modes of color selection. | ||
/// | ||
/// The widget provides a complete color picker that allows free selection of any color. In addition, | ||
/// it also presents a list of predefined color samples to quickly select from if [colorSamples] are provided. | ||
/// | ||
/// ``` | ||
/// Creating a ColorPicker widget | ||
/// ColorPicker( | ||
/// colorSamples: [ColorSample(color: Colors.blue, name: 'Blue')], | ||
/// color: Colors.red, | ||
/// onColorChanged: (color) { | ||
/// print('Color changed to $color'); | ||
/// }, | ||
/// ); | ||
/// ``` | ||
class ColorPicker extends StatelessWidget { | ||
/// Creates a `ColorPicker`. | ||
/// | ||
/// The [colorSamples] parameter is a list of `ColorSample` objects, each representing a predefined color. | ||
/// This parameter is optional, if it's null or empty, no color samples will be displayed. | ||
/// | ||
/// The [color] parameter represents the currently selected color in the [ColorPicker]. If not provided, | ||
/// the color picker will default to white. | ||
/// | ||
/// The required [onColorChanged] parameter is a callback that will be called when a new color is selected. | ||
const ColorPicker({ | ||
this.colorSamples, | ||
this.color, | ||
required this.onColorChanged, | ||
}); | ||
|
||
/// List of color samples. Can be null. | ||
final List<ColorSample>? colorSamples; | ||
|
||
/// Currently selected color. Can be null, in which case the color picker defaults to white. | ||
final Color? color; | ||
|
||
/// Callback that is called when a new color is selected. | ||
final void Function(Color color) onColorChanged; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
picker.ColorPicker( | ||
pickerColor: color ?? Colors.white, | ||
onColorChanged: onColorChanged, | ||
), | ||
if (colorSamples?.isNotEmpty == true) ...[ | ||
const SizedBox(height: 32.0), | ||
const Text('Color Samples'), | ||
const SizedBox(height: 16.0), | ||
Wrap( | ||
spacing: 24, | ||
runSpacing: 10, | ||
children: colorSamples!.map( | ||
(sample) { | ||
final isSelected = sample.color == color; | ||
return GestureDetector( | ||
onTap: () => onColorChanged.call(sample.color), | ||
child: MouseRegion( | ||
cursor: SystemMouseCursors.click, | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
Container( | ||
width: 38, | ||
height: 38, | ||
decoration: BoxDecoration( | ||
color: isSelected ? Colors.blue : Colors.transparent, | ||
shape: BoxShape.circle, | ||
), | ||
child: Padding( | ||
padding: const EdgeInsets.all(2), | ||
child: Container( | ||
width: 34, | ||
height: 34, | ||
decoration: BoxDecoration( | ||
color: isSelected ? Colors.white : Colors.transparent, | ||
shape: BoxShape.circle, | ||
), | ||
child: Padding( | ||
padding: const EdgeInsets.all(2), | ||
child: Container( | ||
width: 30, | ||
height: 30, | ||
decoration: BoxDecoration( | ||
color: sample.color, | ||
shape: BoxShape.circle, | ||
), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
if (sample.name != null) ...[ | ||
const SizedBox(height: 6.0), | ||
Text( | ||
sample.name!, | ||
style: const TextStyle( | ||
fontSize: 10, | ||
), | ||
), | ||
] | ||
], | ||
), | ||
), | ||
); | ||
}, | ||
).toList(), | ||
), | ||
] | ||
], | ||
); | ||
} | ||
} | ||
|
||
/// A ColorSample class represents a color sample with an optional name. | ||
/// | ||
/// It contains a `Color` and an optional `String` to represent its according name. | ||
/// | ||
/// Used as optional parameters in [ColorFieldConfigurator] and [ColorFieldConfiguratorNullable]. | ||
/// ``` | ||
/// Creating a ColorSample | ||
/// const ColorSample sample = ColorSample(color: Colors.blue, name: "MyColors.blue"); | ||
/// ``` | ||
class ColorSample { | ||
/// Creates a `ColorSample`. | ||
/// | ||
/// The [color] parameter is required, and it represents the actual color of the sample. | ||
/// | ||
/// The [name] parameter is optional. It can be used to assign a specific name to the sample. | ||
const ColorSample({ | ||
required this.color, | ||
this.name, | ||
}); | ||
|
||
/// The name of the color sample, can be null. | ||
final String? name; | ||
|
||
/// The color of the color sample. This property is required and cannot be null. | ||
final Color color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export 'src/field_configurators/field_configurator.dart'; | ||
export 'src/stage_controller.dart'; | ||
export 'src/widget_stage_data.dart'; | ||
export 'src/widgets/color_picker.dart'; | ||
export 'src/widgets/stage_craft.dart'; |