Files
smart-serow/pi/ui/lib/widgets/gps_compass.dart

74 lines
2.0 KiB
Dart
Raw Normal View History

2026-02-08 00:26:59 +09:00
import 'dart:math' as math;
import 'package:flutter/material.dart';
import '../theme/app_theme.dart';
class GpsCompass extends StatelessWidget {
final double? heading;
final String? gpsState; // "acquiring", "fix", "lost"
2026-02-08 00:26:59 +09:00
const GpsCompass({super.key, this.heading, this.gpsState});
2026-02-08 00:26:59 +09:00
bool get _hasSignal => heading != null;
bool get _isAcquiring => gpsState == 'acquiring';
2026-02-08 00:26:59 +09:00
String get _displayHeading {
if (!_hasSignal) return 'N/A';
return '${(heading! % 360).round()}';
}
String get _compassDirection {
if (!_hasSignal) return '';
final directions = [
'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE',
'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'
];
final index = ((heading! % 360) / 22.5).round() % 16;
return directions[index];
2026-02-08 00:26:59 +09:00
}
@override
Widget build(BuildContext context) {
final theme = AppTheme.of(context);
// No signal = subdued color, valid = foreground
final iconColour = _hasSignal ? theme.foreground : theme.highlight;
2026-02-08 00:26:59 +09:00
// Convert to radians, 0 = no rotation when no signal
final angle = _hasSignal ? (heading! * math.pi / 180.0) : 0.0;
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Flexible(
flex: 3,
child: Transform.rotate(
2026-02-08 02:27:21 +09:00
angle: _hasSignal ? angle : 0,
2026-02-08 00:26:59 +09:00
child: FittedBox(
fit: BoxFit.contain,
child: Icon(
2026-02-08 02:27:21 +09:00
_hasSignal ? Icons.navigation : Icons.navigation_outlined,
size: 120,
color: iconColour,
2026-02-08 00:26:59 +09:00
),
),
),
),
Flexible(
flex: 1,
child: FittedBox(
fit: BoxFit.contain,
child: Text(
_hasSignal ? "${_displayHeading} ${_compassDirection}" : (_isAcquiring ? "ACQ" : "N/A"),
2026-02-08 00:26:59 +09:00
style: TextStyle(
fontSize: 80,
color: theme.subdued,
2026-02-08 00:26:59 +09:00
fontFamily: 'DIN1451',
),
),
),
),
],
);
}
}