trcr/lib/main_screen.dart

147 lines
4.6 KiB
Dart
Raw Normal View History

2025-05-05 07:47:09 -07:00
import 'package:flutter/material.dart';
2025-06-30 23:00:08 -07:00
import 'package:flutter/services.dart';
import 'package:traccar_client/main.dart';
2025-05-07 22:06:29 -07:00
import 'package:traccar_client/preferences.dart';
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
2025-05-05 08:02:02 -07:00
2025-06-05 23:15:05 -07:00
import 'l10n/app_localizations.dart';
2025-05-05 07:47:09 -07:00
2025-05-07 22:06:29 -07:00
class MainScreen extends StatefulWidget {
2025-05-05 07:47:09 -07:00
const MainScreen({super.key});
2025-05-07 22:06:29 -07:00
@override
State<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
bool trackingEnabled = false;
2025-06-13 18:29:48 -07:00
bool? isMoving;
2025-05-07 22:06:29 -07:00
@override
void initState() {
super.initState();
_initState();
}
void _initState() async {
2025-05-10 09:51:51 -07:00
final state = await bg.BackgroundGeolocation.state;
2025-05-07 22:06:29 -07:00
setState(() {
trackingEnabled = state.enabled;
2025-06-13 18:29:48 -07:00
isMoving = state.isMoving;
2025-05-07 22:06:29 -07:00
});
bg.BackgroundGeolocation.onEnabledChange((bool enabled) {
setState(() {
trackingEnabled = enabled;
});
});
2025-06-13 18:29:48 -07:00
bg.BackgroundGeolocation.onMotionChange((bg.Location location) {
setState(() {
isMoving = location.isMoving;
});
});
2025-05-07 22:06:29 -07:00
}
2025-06-14 13:26:13 -07:00
Future<void> _checkBatteryOptimizations(BuildContext context) async {
try {
if (!await bg.DeviceSettings.isIgnoringBatteryOptimizations) {
final request = await bg.DeviceSettings.showIgnoreBatteryOptimizations();
if (!request.seen && context.mounted) {
showDialog(
context: context,
builder: (_) => AlertDialog(
2025-07-10 20:16:21 -07:00
scrollable: true,
2025-06-14 13:26:13 -07:00
content: Text(AppLocalizations.of(context)!.optimizationMessage),
actions: [
TextButton(
2025-06-18 17:43:07 -07:00
onPressed: () {
Navigator.of(context).pop();
bg.DeviceSettings.show(request);
},
2025-06-14 13:26:13 -07:00
child: Text(AppLocalizations.of(context)!.okButton),
),
],
),
);
}
}
} catch (error) {
debugPrint(error.toString());
}
}
2026-03-06 12:32:44 +11:00
@override
Widget build(BuildContext context) {
final deviceId = Preferences.instance.getString(Preferences.id) ?? '';
return Scaffold(
appBar: AppBar(
title: const Text('Traccar Client'),
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Text(
'Device ID',
style: Theme.of(context).textTheme.labelLarge,
),
const SizedBox(height: 8),
SelectableText(
deviceId,
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
fontFamily: 'monospace',
fontWeight: FontWeight.bold,
),
),
],
),
),
),
const SizedBox(height: 16),
Card(
child: SwitchListTile(
title: const Text('Continuous Tracking'),
subtitle: Text(
trackingEnabled
? 'Sending location updates'
: 'Location tracking disabled',
),
value: trackingEnabled,
activeTrackColor: isMoving == false
? Theme.of(context).colorScheme.error
: null,
onChanged: (bool value) async {
if (value) {
try {
await bg.BackgroundGeolocation.start();
if (mounted) {
_checkBatteryOptimizations(context);
}
} on PlatformException catch (error) {
if (!mounted) return;
messengerKey.currentState?.showSnackBar(
SnackBar(
content: Text(error.message ?? error.code),
),
);
2026-03-06 12:32:44 +11:00
}
} else {
bg.BackgroundGeolocation.stop();
2025-06-28 14:57:56 -07:00
}
2025-05-08 22:10:56 -07:00
},
),
2026-03-06 12:32:44 +11:00
),
],
),
2025-05-05 07:47:09 -07:00
),
),
);
}
}