33library block_colorpicker;
44
55import 'package:flutter/material.dart' ;
6- import 'package:flutter_colorpicker/src/ utils.dart' ;
6+ import 'utils.dart' ;
77
88/// Child widget for layout builder.
99typedef PickerItem = Widget Function (Color color);
@@ -90,7 +90,7 @@ class BlockPicker extends StatefulWidget {
9090 this .itemBuilder = _defaultItemBuilder,
9191 }) : super (key: key);
9292
93- final Color pickerColor;
93+ final Color ? pickerColor;
9494 final ValueChanged <Color > onColorChanged;
9595 final List <Color > availableColors;
9696 final bool useInShowDialog;
@@ -102,7 +102,7 @@ class BlockPicker extends StatefulWidget {
102102}
103103
104104class _BlockPickerState extends State <BlockPicker > {
105- late Color _currentColor;
105+ Color ? _currentColor;
106106
107107 @override
108108 void initState () {
@@ -122,8 +122,10 @@ class _BlockPickerState extends State<BlockPicker> {
122122 widget.availableColors,
123123 (Color color) => widget.itemBuilder (
124124 color,
125- (_currentColor.value == color.value) &&
126- (widget.useInShowDialog ? true : widget.pickerColor.value == color.value),
125+ (_currentColor != null && (widget.useInShowDialog ? true : widget.pickerColor != null ))
126+ ? (_currentColor? .value == color.value) &&
127+ (widget.useInShowDialog ? true : widget.pickerColor? .value == color.value)
128+ : false ,
127129 () => changeColor (color),
128130 ),
129131 );
@@ -142,7 +144,7 @@ class MultipleChoiceBlockPicker extends StatefulWidget {
142144 this .itemBuilder = _defaultItemBuilder,
143145 }) : super (key: key);
144146
145- final List <Color > pickerColors;
147+ final List <Color >? pickerColors;
146148 final ValueChanged <List <Color >> onColorsChanged;
147149 final List <Color > availableColors;
148150 final bool useInShowDialog;
@@ -154,7 +156,7 @@ class MultipleChoiceBlockPicker extends StatefulWidget {
154156}
155157
156158class _MultipleChoiceBlockPickerState extends State <MultipleChoiceBlockPicker > {
157- late List <Color > _currentColors;
159+ List <Color >? _currentColors;
158160
159161 @override
160162 void initState () {
@@ -163,8 +165,12 @@ class _MultipleChoiceBlockPickerState extends State<MultipleChoiceBlockPicker> {
163165 }
164166
165167 void toggleColor (Color color) {
166- setState (() => _currentColors.contains (color) ? _currentColors.remove (color) : _currentColors.add (color));
167- widget.onColorsChanged (_currentColors);
168+ setState (() {
169+ if (_currentColors != null ) {
170+ _currentColors! .contains (color) ? _currentColors! .remove (color) : _currentColors! .add (color);
171+ }
172+ });
173+ widget.onColorsChanged (_currentColors ?? []);
168174 }
169175
170176 @override
@@ -174,7 +180,9 @@ class _MultipleChoiceBlockPickerState extends State<MultipleChoiceBlockPicker> {
174180 widget.availableColors,
175181 (Color color) => widget.itemBuilder (
176182 color,
177- _currentColors.contains (color) && (widget.useInShowDialog ? true : widget.pickerColors.contains (color)),
183+ (_currentColors != null && (widget.useInShowDialog ? true : widget.pickerColors != null ))
184+ ? _currentColors! .contains (color) && (widget.useInShowDialog ? true : widget.pickerColors! .contains (color))
185+ : false ,
178186 () => toggleColor (color),
179187 ),
180188 );
0 commit comments