Skip to content

Commit

Permalink
Merge pull request #25 from rostIvan/master
Browse files Browse the repository at this point in the history
Add MultipleChoiceBlockPicker
  • Loading branch information
mchome authored Jul 22, 2020
2 parents cd0d987 + 6dc413f commit b9c8bf0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## [0.3.5]

* [#25](https://github.com/mchome/flutter_colorpicker/pull/25) Add MultipleChoiceBlockPicker.
(Thanks [rostIvan](https://github.com/rostIvan))

## [0.3.4]

* [#20](https://github.com/mchome/flutter_colorpicker/pull/20) Added null control for availableColors parameter.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ showDialog(
// pickerColor: currentColor,
// onColorChanged: changeColor,
// ),
//
// child: MultipleChoiceBlockPicker(
// pickerColors: currentColors,
// onColorsChanged: changeColors,
// ),
),
actions: <Widget>[
FlatButton(
Expand Down
79 changes: 55 additions & 24 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
bool lightTheme = true;
Color currentColor = Colors.limeAccent;
List<Color> currentColors = [Colors.limeAccent, Colors.green];

void changeColor(Color color) => setState(() => currentColor = color);
void changeColors(List<Color> colors) => setState(() => currentColors = colors);

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -103,7 +105,7 @@ class _MyAppState extends State<MyApp> {
showLabel: false,
showIndicator: true,
indicatorBorderRadius:
const BorderRadius.vertical(
const BorderRadius.vertical(
top: const Radius.circular(25.0),
),
),
Expand Down Expand Up @@ -149,29 +151,58 @@ class _MyAppState extends State<MyApp> {
),
),
Center(
child: RaisedButton(
elevation: 3.0,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Select a color'),
content: SingleChildScrollView(
child: BlockPicker(
pickerColor: currentColor,
onColorChanged: changeColor,
),
),
);
},
);
},
child: const Text('Change me'),
color: currentColor,
textColor: useWhiteForeground(currentColor)
? const Color(0xffffffff)
: const Color(0xff000000),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
elevation: 3.0,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Select a color'),
content: SingleChildScrollView(
child: BlockPicker(
pickerColor: currentColor,
onColorChanged: changeColor,
),
),
);
},
);
},
child: const Text('Change me'),
color: currentColor,
textColor: useWhiteForeground(currentColor)
? const Color(0xffffffff)
: const Color(0xff000000),
),
RaisedButton(
elevation: 3.0,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Select colors'),
content: SingleChildScrollView(
child: MultipleChoiceBlockPicker(
pickerColors: currentColors,
onColorsChanged: changeColors,
),
),
);
},
);
},
child: const Text('Change me again'),
color: currentColor,
textColor: useWhiteForeground(currentColor)
? const Color(0xffffffff)
: const Color(0xff000000),
)
]
),
),
],
Expand Down
51 changes: 51 additions & 0 deletions lib/src/block_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,54 @@ class _BlockPickerState extends State<BlockPicker> {
);
}
}

class MultipleChoiceBlockPicker extends StatefulWidget {
const MultipleChoiceBlockPicker({
@required this.pickerColors,
@required this.onColorsChanged,
this.availableColors = _defaultColors,
this.layoutBuilder = BlockPicker.defaultLayoutBuilder,
this.itemBuilder = BlockPicker.defaultItemBuilder,
});

final List<Color> pickerColors;
final ValueChanged<List<Color>> onColorsChanged;
final List<Color> availableColors;
final PickerLayoutBuilder layoutBuilder;
final PickerItemBuilder itemBuilder;

@override
State<StatefulWidget> createState() => _MultipleChoiceBlockPickerState();
}

class _MultipleChoiceBlockPickerState extends State<MultipleChoiceBlockPicker> {
List<Color> _currentColors;

@override
void initState() {
_currentColors = widget.pickerColors;
super.initState();
}

void toggleColor(Color color) {
setState(() {
_currentColors.contains(color)
? _currentColors.remove(color)
: _currentColors.add(color);
});
widget.onColorsChanged(_currentColors);
}

@override
Widget build(BuildContext context) {
return widget.layoutBuilder(
context,
widget.availableColors,
(Color color, [bool _, Function __]) => widget.itemBuilder(
color,
_currentColors.contains(color),
() => toggleColor(color),
),
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_colorpicker
description: A HSV(HSB)/HSL color picker inspired by chrome devtools and a material color picker for your flutter app.
version: 0.3.4
version: 0.3.5
homepage: https://github.com/mchome/flutter_colorpicker

dependencies:
Expand Down

0 comments on commit b9c8bf0

Please sign in to comment.