From 81ea1670ec9a7c3164a5b65ee68377343f43087c Mon Sep 17 00:00:00 2001 From: Serge Matveenko Date: Fri, 4 Dec 2020 02:39:26 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=B7=20Migrate=20to=20null-safety?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/pubspec.yaml | 3 ++ lib/src/block_picker.dart | 22 +++++++------- lib/src/colorpicker.dart | 30 ++++++++----------- lib/src/hsv_colorpicker.dart | 11 +++---- lib/src/hsv_picker.dart | 58 +++++++++++++++++++++--------------- lib/src/material_picker.dart | 28 ++++++++--------- lib/src/utils.dart | 1 - pubspec.yaml | 6 ++-- 8 files changed, 81 insertions(+), 78 deletions(-) diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 1a58c6d..2719d6b 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -9,3 +9,6 @@ dependencies: flutter: uses-material-design: true + +environment: + sdk: ">=2.12.0-29.10.beta <3.0.0" diff --git a/lib/src/block_picker.dart b/lib/src/block_picker.dart index 6fe47dc..acdb0de 100644 --- a/lib/src/block_picker.dart +++ b/lib/src/block_picker.dart @@ -33,12 +33,12 @@ typedef PickerLayoutBuilder = Widget Function( BuildContext context, List 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, @@ -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( @@ -104,7 +104,7 @@ class BlockPicker extends StatefulWidget { } class _BlockPickerState extends State { - Color _currentColor; + late Color _currentColor; @override void initState() { @@ -121,8 +121,8 @@ class _BlockPickerState extends State { 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)), ); } @@ -130,8 +130,8 @@ class _BlockPickerState extends State { 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, @@ -148,7 +148,7 @@ class MultipleChoiceBlockPicker extends StatefulWidget { } class _MultipleChoiceBlockPickerState extends State { - List _currentColors; + late List _currentColors; @override void initState() { @@ -170,7 +170,7 @@ class _MultipleChoiceBlockPickerState extends State { return widget.layoutBuilder( context, widget.availableColors, - (Color color, [bool _, Function __]) => widget.itemBuilder( + (Color color, [bool? _, Function? __]) => widget.itemBuilder( color, _currentColors.contains(color), () => toggleColor(color), diff --git a/lib/src/colorpicker.dart b/lib/src/colorpicker.dart index 2796add..9b1800a 100644 --- a/lib/src/colorpicker.dart +++ b/lib/src/colorpicker.dart @@ -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, @@ -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 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; @@ -85,7 +82,8 @@ class _ColorPickerState extends State { @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: [ SizedBox( @@ -179,8 +177,8 @@ class _ColorPickerState extends State { 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), @@ -194,11 +192,7 @@ 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 onColorChanged; @@ -206,9 +200,9 @@ class SlidePicker extends StatefulWidget { 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; @@ -305,7 +299,7 @@ class _SlidePickerState extends State { 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), ), ), diff --git a/lib/src/hsv_colorpicker.dart b/lib/src/hsv_colorpicker.dart index 273e31a..b64901d 100644 --- a/lib/src/hsv_colorpicker.dart +++ b/lib/src/hsv_colorpicker.dart @@ -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, @@ -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 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; diff --git a/lib/src/hsv_picker.dart b/lib/src/hsv_picker.dart index 6f93acf..c926975 100644 --- a/lib/src/hsv_picker.dart +++ b/lib/src/hsv_picker.dart @@ -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) { @@ -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, ); @@ -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) { @@ -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, ); @@ -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 @@ -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); } } @@ -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 onColorChanged; + final ValueChanged? onColorChanged; @override _ColorPickerLabelState createState() => _ColorPickerLabelState(); @@ -406,7 +407,7 @@ class _ColorPickerLabelState extends State { List 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), @@ -416,14 +417,14 @@ class _ColorPickerLabelState extends State { 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, ), ), @@ -439,7 +440,11 @@ class _ColorPickerLabelState extends State { return Row(mainAxisAlignment: MainAxisAlignment.center, children: [ 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( @@ -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, ); }, ), @@ -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; diff --git a/lib/src/material_picker.dart b/lib/src/material_picker.dart index 4c22083..44f952a 100644 --- a/lib/src/material_picker.dart +++ b/lib/src/material_picker.dart @@ -8,8 +8,8 @@ import 'package:flutter_colorpicker/src/utils.dart'; class MaterialPicker extends StatefulWidget { MaterialPicker({ - @required this.pickerColor, - @required this.onColorChanged, + required this.pickerColor, + required this.onColorChanged, this.enableLabel: false, }); @@ -46,7 +46,7 @@ class _MaterialPickerState extends State { ]; List _currentColor = [Colors.red, Colors.redAccent]; - Color _currentShading; + Color? _currentShading; List _shadingTypes(List colors) { List result = []; @@ -67,18 +67,18 @@ class _MaterialPickerState extends State { 850, 900 ].map((int shade) { - return Colors.grey[shade]; + return Colors.grey[shade]!; }).toList()); } else if (colorType == Colors.black || colorType == Colors.white) { result.addAll([Colors.black, Colors.white]); } else if (colorType is MaterialAccentColor) { result.addAll([100, 200, 400, 700].map((int shade) { - return colorType[shade]; + return colorType[shade]!; }).toList()); } else if (colorType is MaterialColor) { result.addAll( [50, 100, 200, 300, 400, 500, 600, 700, 800, 900].map((int shade) { - return colorType[shade]; + return colorType[shade]!; }).toList()); } else { result.add(Color(0)); @@ -114,8 +114,8 @@ class _MaterialPickerState extends State { height: _isPortrait ? null : 60.0, decoration: BoxDecoration( border: _isPortrait - ? Border(right: BorderSide(color: Colors.grey[300], width: 1.0)) - : Border(top: BorderSide(color: Colors.grey[300], width: 1.0)), + ? Border(right: BorderSide(color: Colors.grey[300]!, width: 1.0)) + : Border(top: BorderSide(color: Colors.grey[300]!, width: 1.0)), ), child: ListView( scrollDirection: _isPortrait ? Axis.vertical : Axis.horizontal, @@ -144,7 +144,7 @@ class _MaterialPickerState extends State { ? [ _colorType == Theme.of(context).cardColor ? BoxShadow( - color: Colors.grey[300], + color: Colors.grey[300]!, blurRadius: 5.0, ) : BoxShadow( @@ -154,7 +154,7 @@ class _MaterialPickerState extends State { ] : null, border: _colorType == Theme.of(context).cardColor - ? Border.all(color: Colors.grey[300], width: 1.0) + ? Border.all(color: Colors.grey[300]!, width: 1.0) : null, ), ), @@ -181,7 +181,7 @@ class _MaterialPickerState extends State { return GestureDetector( onTap: () { setState(() => _currentShading = _color); - widget.onColorChanged(_currentShading); + widget.onColorChanged(_color); }, child: Container( color: Color(0), @@ -199,17 +199,17 @@ class _MaterialPickerState extends State { ? [ _color == Theme.of(context).cardColor ? BoxShadow( - color: Colors.grey[300], + color: Colors.grey[300]!, blurRadius: 5.0, ) : BoxShadow( - color: _currentShading, + color: _color, blurRadius: 5.0, ), ] : null, border: _color == Theme.of(context).cardColor - ? Border.all(color: Colors.grey[300], width: 1.0) + ? Border.all(color: Colors.grey[300]!, width: 1.0) : null, ), child: (_isPortrait && widget.enableLabel) diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 68f74a6..8bc8ea5 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -7,7 +7,6 @@ bool useWhiteForeground(Color color, {double bias: 1.0}) { // return 1.05 / (color.computeLuminance() + 0.05) > 4.5; // New: - bias ??= 1.0; int v = sqrt(pow(color.red, 2) * 0.299 + pow(color.green, 2) * 0.587 + pow(color.blue, 2) * 0.114) diff --git a/pubspec.yaml b/pubspec.yaml index 79f5016..2aea1a4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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.5 +version: 0.4.0-nullsafety.0 homepage: https://github.com/mchome/flutter_colorpicker dependencies: @@ -8,5 +8,5 @@ dependencies: sdk: flutter environment: - sdk: ">=2.3.0 <3.0.0" - flutter: ">=1.5.4 <2.0.0" \ No newline at end of file + sdk: ">=2.12.0-29.10.beta <3.0.0" + flutter: ">=1.24.0-10.2.pre <2.0.0"