|
| 1 | +import 'package:dpip/utils/instrumental_intensity_color.dart'; |
| 2 | +import 'package:dpip/utils/intensity_color.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | + |
| 5 | +enum IntensityLegendMode { |
| 6 | + /// EEW mode: shows intensity 1-9 |
| 7 | + eew, |
| 8 | + |
| 9 | + /// RTS mode: shows instrumental intensity -3 to 7 |
| 10 | + rts, |
| 11 | +} |
| 12 | + |
| 13 | +class IntensityLegend extends StatelessWidget { |
| 14 | + final IntensityLegendMode mode; |
| 15 | + |
| 16 | + const IntensityLegend({super.key, this.mode = IntensityLegendMode.eew}); |
| 17 | + |
| 18 | + List<Color> get _colors => mode == IntensityLegendMode.eew |
| 19 | + ? List.generate(9, (i) => IntensityColor.intensity(i + 1)) |
| 20 | + : List.generate(11, (i) => InstrumentalIntensityColor.intensity(i - 3)); |
| 21 | + |
| 22 | + List<String> get _labels => mode == IntensityLegendMode.eew |
| 23 | + ? const ['1', '2', '3', '4', '5⁻', '5⁺', '6⁻', '6⁺', '7'] |
| 24 | + : const ['-3', '-2', '-1', '0', '1', '2', '3', '4', '5', '6', '7']; |
| 25 | + |
| 26 | + @override |
| 27 | + Widget build(BuildContext context) { |
| 28 | + final screenWidth = MediaQuery.of(context).size.width; |
| 29 | + final width = screenWidth / 2; |
| 30 | + |
| 31 | + return SizedBox( |
| 32 | + width: width, |
| 33 | + child: Column( |
| 34 | + mainAxisSize: MainAxisSize.min, |
| 35 | + crossAxisAlignment: CrossAxisAlignment.end, |
| 36 | + children: [ |
| 37 | + _buildColorBar(width), |
| 38 | + const SizedBox(height: 2), |
| 39 | + _buildLabels(width), |
| 40 | + ], |
| 41 | + ), |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + Widget _buildColorBar(double width) { |
| 46 | + if (mode == IntensityLegendMode.eew) { |
| 47 | + // EEW mode: discrete color blocks |
| 48 | + return ClipRRect( |
| 49 | + borderRadius: BorderRadius.circular(4), |
| 50 | + child: SizedBox( |
| 51 | + height: 8, |
| 52 | + width: width, |
| 53 | + child: Row( |
| 54 | + children: _colors.map((color) { |
| 55 | + return Expanded(child: Container(color: color)); |
| 56 | + }).toList(), |
| 57 | + ), |
| 58 | + ), |
| 59 | + ); |
| 60 | + } else { |
| 61 | + // RTS mode: gradient |
| 62 | + return Container( |
| 63 | + height: 8, |
| 64 | + width: width, |
| 65 | + decoration: BoxDecoration( |
| 66 | + borderRadius: BorderRadius.circular(4), |
| 67 | + gradient: LinearGradient(colors: _colors), |
| 68 | + ), |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Widget _buildLabels(double width) { |
| 74 | + if (mode == IntensityLegendMode.eew) { |
| 75 | + // EEW mode: labels centered under each color block |
| 76 | + return SizedBox( |
| 77 | + width: width, |
| 78 | + child: Row( |
| 79 | + children: _labels.map((label) { |
| 80 | + return Expanded( |
| 81 | + child: Text( |
| 82 | + label, |
| 83 | + style: const TextStyle(fontSize: 9), |
| 84 | + textAlign: TextAlign.center, |
| 85 | + ), |
| 86 | + ); |
| 87 | + }).toList(), |
| 88 | + ), |
| 89 | + ); |
| 90 | + } else { |
| 91 | + // RTS mode: labels at edges |
| 92 | + return SizedBox( |
| 93 | + width: width, |
| 94 | + child: Row( |
| 95 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 96 | + children: _labels.map((label) { |
| 97 | + return Text( |
| 98 | + label, |
| 99 | + style: const TextStyle(fontSize: 9), |
| 100 | + ); |
| 101 | + }).toList(), |
| 102 | + ), |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments