134 lines
5.2 KiB
Dart
134 lines
5.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../theme/app_theme.dart';
|
|
|
|
class BarEntry {
|
|
final String label;
|
|
final double value;
|
|
final String valueText;
|
|
final bool highlight; // drawn in gold (best target, the 10s)
|
|
final bool goldLabel;
|
|
|
|
const BarEntry(this.label, this.value, this.valueText, {this.highlight = false, this.goldLabel = false});
|
|
}
|
|
|
|
/// Horizontal bars, one per row: label, bar on a track, value. Ticks along the
|
|
/// bottom. The rows share the height they get.
|
|
class BarList extends StatelessWidget {
|
|
final List<BarEntry> entries;
|
|
final double max;
|
|
final List<double> ticks;
|
|
final double labelWidth;
|
|
final double valueWidth;
|
|
final double scale; // text size factor (share image)
|
|
final Palette? palette;
|
|
|
|
const BarList({
|
|
super.key,
|
|
required this.entries,
|
|
required this.max,
|
|
this.ticks = const [],
|
|
this.labelWidth = 32,
|
|
this.valueWidth = 40,
|
|
this.scale = 1,
|
|
this.palette,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final c = palette ?? Palette.current;
|
|
String tick(double v) => v == v.roundToDouble() ? '${v.round()}' : v.toStringAsFixed(1);
|
|
return LayoutBuilder(builder: (context, box) {
|
|
final barWidth = box.maxWidth - labelWidth - valueWidth;
|
|
// 26 high, thinner when many rows share little height (e.g. 10 … 0)
|
|
final ticksHeight = ticks.isEmpty ? 0.0 : 20 * scale;
|
|
final rowHeight = entries.isEmpty || !box.hasBoundedHeight
|
|
? double.infinity
|
|
: (box.maxHeight - ticksHeight) / entries.length;
|
|
final barHeight = (rowHeight * 0.75).clamp(4.0, 26 * scale);
|
|
// Text shrinks with thin rows too, so many bars (a session of 20
|
|
// targets) don't run their labels and values into each other
|
|
final labelSize = (rowHeight * 0.78).clamp(6.0, 12 * scale);
|
|
final valueSize = (rowHeight * 0.85).clamp(6.0, 15 * scale);
|
|
return Column(
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
children: [
|
|
for (final e in entries)
|
|
Expanded(
|
|
child: Row(
|
|
children: [
|
|
SizedBox(
|
|
width: labelWidth,
|
|
child: Text(e.label,
|
|
maxLines: 1,
|
|
style: TextStyle(
|
|
fontSize: labelSize,
|
|
height: 1,
|
|
fontWeight: FontWeight.w600,
|
|
color: e.goldLabel ? c.gold : c.muted)),
|
|
),
|
|
SizedBox(
|
|
width: barWidth,
|
|
child: Stack(
|
|
alignment: Alignment.centerLeft,
|
|
children: [
|
|
Container(
|
|
height: barHeight,
|
|
decoration: BoxDecoration(color: c.tile, borderRadius: BorderRadius.circular(barHeight * 0.27)),
|
|
),
|
|
FractionallySizedBox(
|
|
widthFactor: max <= 0 ? 0 : (e.value / max).clamp(0.0, 1.0),
|
|
child: Container(
|
|
height: barHeight,
|
|
decoration: BoxDecoration(
|
|
color: e.highlight ? c.gold : c.accent,
|
|
borderRadius: BorderRadius.circular(barHeight * 0.27),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(
|
|
width: valueWidth,
|
|
child: Padding(
|
|
padding: EdgeInsets.only(left: 10 * scale),
|
|
child: Text(e.valueText,
|
|
maxLines: 1,
|
|
style: TextStyle(
|
|
fontSize: valueSize,
|
|
height: 1,
|
|
fontWeight: FontWeight.w700,
|
|
color: e.highlight ? c.gold : (e.value == 0 ? c.faint : c.ink),
|
|
fontFeatures: AppTheme.tnum)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
if (ticks.isNotEmpty)
|
|
Padding(
|
|
padding: EdgeInsets.only(left: labelWidth, right: valueWidth, top: 6 * scale),
|
|
child: SizedBox(
|
|
height: 14 * scale,
|
|
child: Stack(
|
|
children: [
|
|
for (final t in ticks)
|
|
Align(
|
|
alignment: Alignment(max <= 0 ? -1 : (t / max) * 2 - 1, 0),
|
|
child: Text(tick(t), style: TextStyle(fontSize: 10 * scale, color: c.faint, fontFeatures: AppTheme.tnum)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
});
|
|
}
|
|
}
|