layout method

  1. @override
PwBox layout(
  1. Context context,
  2. BoxConstraints constraints
)
override

layout API.

Implementation

@override
PwBox layout(Context context, BoxConstraints constraints) {
  final List<bool> modules =
      const Code128Encoder().encodeWithQuietZone(data);
  final double textSlot = drawText ? 12.0 : 0.0;
  final double barH = (height - textSlot).clamp(8.0, height);
  final PwSize barSize = PwSize(width, barH);
  final Widget bars = CustomPaint(
    size: barSize,
    painter: (PdfCanvas canvas, PwSize box) {
      canvas.endText();
      final double moduleW = box.width / modules.length;
      canvas.setFillColor(color);
      for (int i = 0; i < modules.length; i++) {
        if (!modules[i]) {
          continue;
        }
        canvas.rect(i * moduleW, 0, moduleW + 0.05, box.height);
        canvas.fill();
      }
    },
  );
  if (!drawText) {
    return bars.layout(context, constraints);
  }
  return Column(
    children: <Widget>[
      bars,
      const SizedBox(height: 2),
      Text(
        data,
        style: const TextStyle(fontSize: 8, color: '455A64'),
      ),
    ],
  ).layout(context, constraints);
}