Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MultipleChoiceBlockPicker #25

Merged
merged 4 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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