146 lines
4.6 KiB
Dart
146 lines
4.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:traccar_client/main.dart';
|
|
import 'package:traccar_client/preferences.dart';
|
|
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
|
|
|
|
import 'l10n/app_localizations.dart';
|
|
|
|
class MainScreen extends StatefulWidget {
|
|
const MainScreen({super.key});
|
|
|
|
@override
|
|
State<MainScreen> createState() => _MainScreenState();
|
|
}
|
|
|
|
class _MainScreenState extends State<MainScreen> {
|
|
bool trackingEnabled = false;
|
|
bool? isMoving;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initState();
|
|
}
|
|
|
|
void _initState() async {
|
|
final state = await bg.BackgroundGeolocation.state;
|
|
setState(() {
|
|
trackingEnabled = state.enabled;
|
|
isMoving = state.isMoving;
|
|
});
|
|
bg.BackgroundGeolocation.onEnabledChange((bool enabled) {
|
|
setState(() {
|
|
trackingEnabled = enabled;
|
|
});
|
|
});
|
|
bg.BackgroundGeolocation.onMotionChange((bg.Location location) {
|
|
setState(() {
|
|
isMoving = location.isMoving;
|
|
});
|
|
});
|
|
}
|
|
|
|
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(
|
|
scrollable: true,
|
|
content: Text(AppLocalizations.of(context)!.optimizationMessage),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
bg.DeviceSettings.show(request);
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.okButton),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
debugPrint(error.toString());
|
|
}
|
|
}
|
|
|
|
@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),
|
|
),
|
|
);
|
|
}
|
|
} else {
|
|
bg.BackgroundGeolocation.stop();
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|