todos-contra-el-fuego-mobile/lib/compassMapPlugin.dart
vjrj 2bf42ce262 refactor: remove comunes-flutter dependency and inline components
Remove the external comunes-flutter library dependency and replace all its
components with local implementations:

- Utility functions: Created lib/utils/widget_utils.dart with compactWidgets()
  and ellipse() functions, replacing the comunes-flutter helpers with modern
  Dart null-safety patterns (whereType instead of where(notNull))
- Secret loader: Inlined as lib/utils/secret_loader.dart for loading JSON config
- Layout widgets: Replaced CenteredRow/CenteredColumn with standard Row/Column
  with MainAxisAlignment.center
- Intro screens: Copied AppIntroPage and MaterialAppWithIntro locally in
  lib/widgets/ with cleaned-up deprecated code patterns
- Button widget: Created lib/widgets/rounded_btn.dart for the RoundedBtn used
  in activeFires.dart

Changes span 20 files:
- 5 new utility/widget files created
- Updated imports in 15 files
- Removed path dependency from pubspec.yaml
- All functionality preserved, build verified with flutter pub get

Impact:
- Removes external dependency, simplifying project structure
- Modern Dart patterns (proper null-safety)
- Better code clarity with explicit widget properties
- Easier onboarding (no 'what is comunes-flutter?' questions)
2026-03-07 18:19:33 +01:00

111 lines
2.9 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
/// Compass button widget for resetting map rotation to north
/// Only visible when map is rotated (rotation != 0)
class CompassMapPluginWidget extends StatefulWidget {
const CompassMapPluginWidget({super.key});
@override
_CompassMapPluginWidgetState createState() => _CompassMapPluginWidgetState();
}
class _CompassMapPluginWidgetState extends State<CompassMapPluginWidget> {
late MapController _mapController;
bool _isRotated = false;
late Timer _rotationCheckTimer;
@override
void didChangeDependencies() {
super.didChangeDependencies();
try {
_mapController = MapController.of(context);
_initRotationMonitoring();
} catch (_) {
// Ignore - MapController may not be available yet
}
}
void _initRotationMonitoring() {
// Check rotation periodically (every 100ms) to detect changes
_rotationCheckTimer =
Timer.periodic(const Duration(milliseconds: 100), (_) {
_checkRotation();
});
// Initial state check
_checkRotation();
}
void _checkRotation() {
try {
if (!mounted) {
return;
}
final double rotation = _mapController.camera.rotation;
final bool isRotated = rotation.abs() > 0.0;
if (isRotated != _isRotated) {
setState(() {
_isRotated = isRotated;
});
}
} catch (e) {
// Silently ignore errors (controller may not be ready)
}
}
void _resetRotation() {
try {
final MapController controller = MapController.of(context);
controller.rotate(0);
} catch (_) {
// Ignore - MapController may not be available
}
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) => Stack(
fit: StackFit.expand,
children: <Widget>[
if (_isRotated)
Positioned(
top: 10.0,
right: 10.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Column(
children: <Widget>[
_compassButton(context),
],
)
],
),
)
],
));
}
FloatingActionButton _compassButton(BuildContext context) {
return FloatingActionButton(
backgroundColor: Colors.black26,
mini: true,
heroTag: 'compass_button',
tooltip: 'Go to North',
onPressed: _resetRotation,
child: const Icon(Icons.explore),
);
}
@override
void dispose() {
_rotationCheckTimer.cancel();
super.dispose();
}
}