74 lines
2.1 KiB
Dart
74 lines
2.1 KiB
Dart
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"
|
|
|
|
const GpsCompass({super.key, this.heading, this.gpsState});
|
|
|
|
bool get _hasSignal => heading != null;
|
|
bool get _isAcquiring => gpsState == 'acquiring';
|
|
|
|
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];
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = AppTheme.of(context);
|
|
|
|
// No signal = subdued color, valid = foreground
|
|
final iconColour = _hasSignal ? theme.foreground : theme.highlight;
|
|
|
|
// 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(
|
|
angle: _hasSignal ? angle : 0,
|
|
child: FittedBox(
|
|
fit: BoxFit.contain,
|
|
child: Icon(
|
|
_hasSignal ? Icons.navigation : Icons.navigation_outlined,
|
|
size: 120,
|
|
color: iconColour,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Flexible(
|
|
flex: 1,
|
|
child: FittedBox(
|
|
fit: BoxFit.contain,
|
|
child: Text(
|
|
_hasSignal ? "${_displayHeading} ${_compassDirection}" : (_isAcquiring ? "ACQ" : "N/A"),
|
|
style: TextStyle(
|
|
fontSize: 80,
|
|
color: theme.subdued, // less emphasis on text, let the icon have semantic colour
|
|
fontFamily: 'DIN1451',
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|