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

🏷 Migrate to null-safety #36

Merged
merged 1 commit into from
Dec 4, 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
3 changes: 3 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ dependencies:

flutter:
uses-material-design: true

environment:
sdk: ">=2.12.0-29.10.beta <3.0.0"
22 changes: 11 additions & 11 deletions lib/src/block_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ typedef PickerLayoutBuilder = Widget Function(
BuildContext context, List<Color> colors, PickerItem child);
typedef PickerItem = Widget Function(Color color);
typedef PickerItemBuilder = Widget Function(
Color color, bool isCurrentColor, Function changeColor);
Color color, bool isCurrentColor, void Function() changeColor);

class BlockPicker extends StatefulWidget {
const BlockPicker({
@required this.pickerColor,
@required this.onColorChanged,
required this.pickerColor,
required this.onColorChanged,
this.availableColors = _defaultColors,
this.layoutBuilder = defaultLayoutBuilder,
this.itemBuilder = defaultItemBuilder,
Expand Down Expand Up @@ -67,7 +67,7 @@ class BlockPicker extends StatefulWidget {
}

static Widget defaultItemBuilder(
Color color, bool isCurrentColor, Function changeColor) {
Color color, bool isCurrentColor, void Function() changeColor) {
return Container(
margin: EdgeInsets.all(5.0),
decoration: BoxDecoration(
Expand Down Expand Up @@ -104,7 +104,7 @@ class BlockPicker extends StatefulWidget {
}

class _BlockPickerState extends State<BlockPicker> {
Color _currentColor;
late Color _currentColor;

@override
void initState() {
Expand All @@ -121,17 +121,17 @@ class _BlockPickerState extends State<BlockPicker> {
Widget build(BuildContext context) {
return widget.layoutBuilder(
context,
widget.availableColors??_defaultColors,
(Color color, [bool _, Function __]) => widget.itemBuilder(
widget.availableColors,
(Color color, [bool? _, Function? __]) => widget.itemBuilder(
color, _currentColor.value == color.value, () => changeColor(color)),
);
}
}

class MultipleChoiceBlockPicker extends StatefulWidget {
const MultipleChoiceBlockPicker({
@required this.pickerColors,
@required this.onColorsChanged,
required this.pickerColors,
required this.onColorsChanged,
this.availableColors = _defaultColors,
this.layoutBuilder = BlockPicker.defaultLayoutBuilder,
this.itemBuilder = BlockPicker.defaultItemBuilder,
Expand All @@ -148,7 +148,7 @@ class MultipleChoiceBlockPicker extends StatefulWidget {
}

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

@override
void initState() {
Expand All @@ -170,7 +170,7 @@ class _MultipleChoiceBlockPickerState extends State<MultipleChoiceBlockPicker> {
return widget.layoutBuilder(
context,
widget.availableColors,
(Color color, [bool _, Function __]) => widget.itemBuilder(
(Color color, [bool? _, Function? __]) => widget.itemBuilder(
color,
_currentColors.contains(color),
() => toggleColor(color),
Expand Down
30 changes: 12 additions & 18 deletions lib/src/colorpicker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import 'package:flutter_colorpicker/src/hsv_picker.dart';

class ColorPicker extends StatefulWidget {
const ColorPicker({
@required this.pickerColor,
@required this.onColorChanged,
required this.pickerColor,
required this.onColorChanged,
this.paletteType: PaletteType.hsv,
this.enableAlpha: true,
this.showLabel: true,
Expand All @@ -21,17 +21,14 @@ class ColorPicker extends StatefulWidget {
this.colorPickerWidth: 300.0,
this.pickerAreaHeightPercent: 1.0,
this.pickerAreaBorderRadius: const BorderRadius.all(Radius.zero),
}) : assert(paletteType != null),
assert(enableAlpha != null),
assert(showLabel != null),
assert(pickerAreaBorderRadius != null);
});

final Color pickerColor;
final ValueChanged<Color> onColorChanged;
final PaletteType paletteType;
final bool enableAlpha;
final bool showLabel;
final TextStyle labelTextStyle;
final TextStyle? labelTextStyle;
final bool displayThumbColor;
final bool portraitOnly;
final double colorPickerWidth;
Expand Down Expand Up @@ -85,7 +82,8 @@ class _ColorPickerState extends State<ColorPicker> {

@override
Widget build(BuildContext context) {
if (MediaQuery.of(context).orientation == Orientation.portrait || widget.portraitOnly) {
if (MediaQuery.of(context).orientation == Orientation.portrait ||
widget.portraitOnly) {
return Column(
children: <Widget>[
SizedBox(
Expand Down Expand Up @@ -179,8 +177,8 @@ class _ColorPickerState extends State<ColorPicker> {

class SlidePicker extends StatefulWidget {
const SlidePicker({
@required this.pickerColor,
@required this.onColorChanged,
required this.pickerColor,
required this.onColorChanged,
this.paletteType: PaletteType.hsv,
this.enableAlpha: true,
this.sliderSize: const Size(260, 40),
Expand All @@ -194,21 +192,17 @@ class SlidePicker extends StatefulWidget {
this.indicatorAlignmentEnd: const Alignment(1.0, 3.0),
this.displayThumbColor: false,
this.indicatorBorderRadius: const BorderRadius.all(Radius.zero),
}) : assert(paletteType != null),
assert(showSliderText != null),
assert(enableAlpha != null),
assert(showLabel != null),
assert(indicatorBorderRadius != null);
});

final Color pickerColor;
final ValueChanged<Color> onColorChanged;
final PaletteType paletteType;
final bool enableAlpha;
final Size sliderSize;
final bool showSliderText;
final TextStyle sliderTextStyle;
final TextStyle? sliderTextStyle;
final bool showLabel;
final TextStyle labelTextStyle;
final TextStyle? labelTextStyle;
final bool showIndicator;
final Size indicatorSize;
final AlignmentGeometry indicatorAlignmentBegin;
Expand Down Expand Up @@ -305,7 +299,7 @@ class _SlidePickerState extends State<SlidePicker> {
child: Text(
palette.toString().split('.').last[0].toUpperCase(),
style: widget.sliderTextStyle ??
Theme.of(context).textTheme.bodyText1.copyWith(
Theme.of(context).textTheme.bodyText1?.copyWith(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
),
Expand Down
11 changes: 4 additions & 7 deletions lib/src/hsv_colorpicker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import 'package:flutter_colorpicker/src/hsv_picker.dart';

class HsvColorPicker extends StatefulWidget {
const HsvColorPicker({
@required this.pickerColor,
@required this.onColorChanged,
required this.pickerColor,
required this.onColorChanged,
this.paletteType: PaletteType.hsv,
this.enableAlpha: true,
this.showLabel: true,
Expand All @@ -20,17 +20,14 @@ class HsvColorPicker extends StatefulWidget {
this.colorPickerWidth: 300.0,
this.pickerAreaHeightPercent: 1.0,
this.pickerAreaBorderRadius: const BorderRadius.all(Radius.zero),
}) : assert(paletteType != null),
assert(enableAlpha != null),
assert(showLabel != null),
assert(pickerAreaBorderRadius != null);
});

final HSVColor pickerColor;
final ValueChanged<HSVColor> onColorChanged;
final PaletteType paletteType;
final bool enableAlpha;
final bool showLabel;
final TextStyle labelTextStyle;
final TextStyle? labelTextStyle;
final bool displayThumbColor;
final double colorPickerWidth;
final double pickerAreaHeightPercent;
Expand Down
58 changes: 34 additions & 24 deletions lib/src/hsv_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HSVColorPainter extends CustomPainter {
const HSVColorPainter(this.hsvColor, {this.pointerColor});

final HSVColor hsvColor;
final Color pointerColor;
final Color? pointerColor;

@override
void paint(Canvas canvas, Size size) {
Expand Down Expand Up @@ -50,9 +50,10 @@ class HSVColorPainter extends CustomPainter {
size.width * hsvColor.saturation, size.height * (1 - hsvColor.value)),
size.height * 0.04,
Paint()
..color = pointerColor ?? useWhiteForeground(hsvColor.toColor())
? Colors.white
: Colors.black
..color = pointerColor ??
(useWhiteForeground(hsvColor.toColor())
? Colors.white
: Colors.black)
..strokeWidth = 1.5
..style = PaintingStyle.stroke,
);
Expand All @@ -66,7 +67,7 @@ class HSLColorPainter extends CustomPainter {
const HSLColorPainter(this.hslColor, {this.pointerColor});

final HSLColor hslColor;
final Color pointerColor;
final Color? pointerColor;

@override
void paint(Canvas canvas, Size size) {
Expand Down Expand Up @@ -96,9 +97,10 @@ class HSLColorPainter extends CustomPainter {
size.height * (1 - hslColor.lightness)),
size.height * 0.04,
Paint()
..color = pointerColor ?? useWhiteForeground(hslColor.toColor())
? Colors.white
: Colors.black
..color = pointerColor ??
(useWhiteForeground(hslColor.toColor())
? Colors.white
: Colors.black)
..strokeWidth = 1.5
..style = PaintingStyle.stroke,
);
Expand Down Expand Up @@ -251,7 +253,7 @@ class TrackPainter extends CustomPainter {
class ThumbPainter extends CustomPainter {
const ThumbPainter({this.thumbColor, this.fullThumbColor: false});

final Color thumbColor;
final Color? thumbColor;
final bool fullThumbColor;

@override
Expand All @@ -276,7 +278,7 @@ class ThumbPainter extends CustomPainter {
Offset(0.0, size.height * 0.4),
size.height * (fullThumbColor ? 1.0 : 0.65),
Paint()
..color = thumbColor
..color = thumbColor!
..style = PaintingStyle.fill);
}
}
Expand Down Expand Up @@ -345,14 +347,13 @@ class ColorPickerLabel extends StatefulWidget {
this.textStyle,
this.editable: false, // TODO: TBD
this.onColorChanged, // TODO: TBD
}) : assert(enableAlpha != null),
assert(editable != null);
});

final HSVColor hsvColor;
final bool enableAlpha;
final TextStyle textStyle;
final TextStyle? textStyle;
final bool editable;
final ValueChanged<HSVColor> onColorChanged;
final ValueChanged<HSVColor>? onColorChanged;

@override
_ColorPickerLabelState createState() => _ColorPickerLabelState();
Expand Down Expand Up @@ -406,7 +407,7 @@ class _ColorPickerLabelState extends State<ColorPickerLabel> {

List<Widget> colorValueLabels() {
return [
for (String item in _colorTypes[_colorType])
for (String item in _colorTypes[_colorType] ?? [])
if (widget.enableAlpha || item != 'A')
Padding(
padding: const EdgeInsets.symmetric(horizontal: 7.0),
Expand All @@ -416,14 +417,14 @@ class _ColorPickerLabelState extends State<ColorPickerLabel> {
Text(
item,
style: widget.textStyle ??
Theme.of(context).textTheme.bodyText2.copyWith(
Theme.of(context).textTheme.bodyText2?.copyWith(
fontWeight: FontWeight.bold, fontSize: 16.0),
),
SizedBox(height: 10.0),
Expanded(
child: Text(
colorValue(widget.hsvColor, _colorType)[
_colorTypes[_colorType].indexOf(item)],
_colorTypes[_colorType]!.indexOf(item)],
overflow: TextOverflow.ellipsis,
),
),
Expand All @@ -439,7 +440,11 @@ class _ColorPickerLabelState extends State<ColorPickerLabel> {
return Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
DropdownButton(
value: _colorType,
onChanged: (ColorModel type) => setState(() => _colorType = type),
onChanged: (ColorModel? type) {
if (type != null) {
setState(() => _colorType = type);
}
},
items: [
for (ColorModel type in _colorTypes.keys)
DropdownMenuItem(
Expand Down Expand Up @@ -592,12 +597,14 @@ class ColorPickerSlider extends StatelessWidget {
id: _SliderLayout.gestureContainer,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints box) {
RenderBox getBox = context.findRenderObject();
RenderBox? getBox = context.findRenderObject() as RenderBox?;
return GestureDetector(
onPanDown: (DragDownDetails details) =>
slideEvent(getBox, box, details.globalPosition),
onPanUpdate: (DragUpdateDetails details) =>
slideEvent(getBox, box, details.globalPosition),
onPanDown: (DragDownDetails details) => getBox != null
? slideEvent(getBox, box, details.globalPosition)
: null,
onPanUpdate: (DragUpdateDetails details) => getBox != null
? slideEvent(getBox, box, details.globalPosition)
: null,
);
},
),
Expand Down Expand Up @@ -664,7 +671,10 @@ class ColorPickerArea extends StatelessWidget {

void _handleGesture(
Offset position, BuildContext context, double height, double width) {
RenderBox getBox = context.findRenderObject();
RenderBox? getBox = context.findRenderObject() as RenderBox?;
if (getBox == null) {
return;
}
Offset localOffset = getBox.globalToLocal(position);
double horizontal = localOffset.dx.clamp(0.0, width) / width;
double vertical = 1 - localOffset.dy.clamp(0.0, height) / height;
Expand Down
Loading