Improve code quality

This commit is contained in:
Ivan Semkin
2023-05-01 18:32:40 +03:00
parent fbffb36f2d
commit 0315bb336d
5 changed files with 36 additions and 43 deletions

View File

@@ -46,8 +46,8 @@ class _MainScreenState extends State<MainScreen> {
),
// size: 320.0,
embeddedImage: snapshot.data,
embeddedImageStyle: QrEmbeddedImageStyle(
size: const Size.square(60),
embeddedImageStyle: const QrEmbeddedImageStyle(
size: Size.square(60),
),
),
);

View File

@@ -34,11 +34,9 @@ class PaintCache {
/// Retrieve the first [Paint] object from the paint cache for the provided
/// element and position.
Paint? firstPaint(QrCodeElement element, {FinderPatternPosition? position}) {
if (element == QrCodeElement.codePixel) {
return _pixelPaints.first;
} else {
return _keyedPaints[_cacheKey(element, position: position)];
}
return element == QrCodeElement.codePixel
? _pixelPaints.first
: _keyedPaints[_cacheKey(element, position: position)];
}
/// Retrieve all [Paint] objects from the paint cache for the provided
@@ -49,10 +47,8 @@ class PaintCache {
QrCodeElement element, {
FinderPatternPosition? position,
}) {
if (element == QrCodeElement.codePixel) {
return _pixelPaints;
} else {
return <Paint?>[_keyedPaints[_cacheKey(element, position: position)]];
}
return element == QrCodeElement.codePixel
? _pixelPaints
: <Paint?>[_keyedPaints[_cacheKey(element, position: position)]];
}
}

View File

@@ -44,7 +44,10 @@ class QrImageView extends StatefulWidget {
this.embeddedImageEmitsError = false,
@Deprecated('use colors in eyeStyle and dataModuleStyle instead')
this.foregroundColor,
}) : assert(QrVersions.isSupportedVersion(version)),
}) : assert(
QrVersions.isSupportedVersion(version),
'QR code version $version is not supported',
),
_data = data,
_qrCode = null;
@@ -75,7 +78,10 @@ class QrImageView extends StatefulWidget {
this.embeddedImageEmitsError = false,
@Deprecated('use colors in eyeStyle and dataModuleStyle instead')
this.foregroundColor,
}) : assert(QrVersions.isSupportedVersion(version)),
}) : assert(
QrVersions.isSupportedVersion(version),
'QR code version $version is not supported',
),
_data = null,
_qrCode = qr;
@@ -165,11 +171,7 @@ class _QrImageViewState extends State<QrImageView> {
version: widget.version,
errorCorrectionLevel: widget.errorCorrectionLevel,
);
if (_validationResult.isValid) {
_qr = _validationResult.qrCode;
} else {
_qr = null;
}
_qr = _validationResult.isValid ? _validationResult.qrCode : null;
} else if (widget._qrCode != null) {
_qr = widget._qrCode;
_validationResult =
@@ -192,29 +194,27 @@ class _QrImageViewState extends State<QrImageView> {
builder: (BuildContext ctx, AsyncSnapshot<ui.Image> snapshot) {
if (snapshot.error != null) {
print('snapshot error: ${snapshot.error}');
if (widget.embeddedImageEmitsError) {
return _errorWidget(context, constraints, snapshot.error);
} else {
return _qrWidget(context, null, widgetSize);
}
return widget.embeddedImageEmitsError
? _errorWidget(context, constraints, snapshot.error)
: _qrWidget(null, widgetSize);
}
if (snapshot.hasData) {
print('loaded image');
final ui.Image? loadedImage = snapshot.data;
return _qrWidget(context, loadedImage, widgetSize);
return _qrWidget(loadedImage, widgetSize);
} else {
return Container();
}
},
);
} else {
return _qrWidget(context, null, widgetSize);
return _qrWidget(null, widgetSize);
}
},
);
}
Widget _qrWidget(BuildContext context, ui.Image? image, double edgeLength) {
Widget _qrWidget(ui.Image? image, double edgeLength) {
final QrPainter painter = QrPainter.withQr(
qr: _qr!,
color: widget.foregroundColor,

View File

@@ -48,7 +48,10 @@ class QrPainter extends CustomPainter {
'You should use the background color value of your container widget',
)
this.emptyColor,
}) : assert(QrVersions.isSupportedVersion(version)) {
}) : assert(
QrVersions.isSupportedVersion(version),
'QR code version $version is not supported',
) {
_init(data);
}
@@ -351,11 +354,7 @@ class QrPainter extends CustomPainter {
position: position,
)!;
outerPaint.strokeWidth = metrics.pixelSize;
if (color != null) {
outerPaint.color = color!;
} else {
outerPaint.color = eyeStyle.color!;
}
outerPaint.color = color != null ? color! : eyeStyle.color!;
final ui.Paint innerPaint = _paintCache
.firstPaint(QrCodeElement.finderPatternInner, position: position)!;
@@ -477,10 +476,7 @@ class QrPainter extends CustomPainter {
}
/// Returns the raw QR code [ui.Image] object.
Future<ui.Image> toImage(
double size, {
ui.ImageByteFormat format = ui.ImageByteFormat.png,
}) {
Future<ui.Image> toImage(double size) {
return toPicture(size).toImage(size.toInt(), size.toInt());
}
@@ -489,7 +485,7 @@ class QrPainter extends CustomPainter {
double size, {
ui.ImageByteFormat format = ui.ImageByteFormat.png,
}) async {
final ui.Image image = await toImage(size, format: format);
final ui.Image image = await toImage(size);
return image.toByteData(format: format);
}
}

View File

@@ -4,8 +4,6 @@
* See LICENSE for distribution and usage details.
*/
import 'dart:ui';
import 'package:flutter/widgets.dart';
/// Represents a specific element / part of a QR code. This is used to isolate
@@ -59,6 +57,7 @@ enum QrDataModuleShape {
}
/// Styling options for finder pattern eye.
@immutable
class QrEyeStyle {
/// Create a new set of styling options for QR Eye.
const QrEyeStyle({this.eyeShape, this.color});
@@ -82,6 +81,7 @@ class QrEyeStyle {
}
/// Styling options for data module.
@immutable
class QrDataModuleStyle {
/// Create a new set of styling options for data modules.
const QrDataModuleStyle({
@@ -108,9 +108,10 @@ class QrDataModuleStyle {
}
/// Styling options for any embedded image overlay
@immutable
class QrEmbeddedImageStyle {
/// Create a new set of styling options.
QrEmbeddedImageStyle({
const QrEmbeddedImageStyle({
this.size,
this.color,
});
@@ -118,10 +119,10 @@ class QrEmbeddedImageStyle {
/// The size of the image. If one dimension is zero then the other dimension
/// will be used to scale the zero dimension based on the original image
/// size.
Size? size;
final Size? size;
/// Color to tint the image.
Color? color;
final Color? color;
/// Check to see if the style object has a non-null, non-zero size.
bool get hasDefinedSize => size != null && size!.longestSide > 0;