trcr/lib/main_screen.dart

201 lines
6.3 KiB
Dart
Raw Normal View History

2025-05-08 22:10:56 -07:00
import 'dart:developer' as developer;
2025-05-05 07:47:09 -07:00
import 'package:flutter/material.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 08:02:02 -07:00
import 'status_screen.dart';
import 'settings_screen.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? stopDetection;
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
stopDetection = Preferences.instance.getBool(Preferences.stopDetection);
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(
content: Text(AppLocalizations.of(context)!.optimizationMessage),
actions: [
TextButton(
onPressed: () => bg.DeviceSettings.show(request),
child: Text(AppLocalizations.of(context)!.okButton),
),
],
),
);
}
}
} catch (error) {
debugPrint(error.toString());
}
}
2025-05-07 22:06:29 -07:00
Widget _buildTrackingCard() {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
contentPadding: EdgeInsets.zero,
title: Text(AppLocalizations.of(context)!.trackingTitle),
titleTextStyle: Theme.of(context).textTheme.headlineMedium,
),
ListTile(
contentPadding: EdgeInsets.zero,
title: Text(AppLocalizations.of(context)!.idLabel),
2025-05-13 07:40:09 -07:00
subtitle: Text(Preferences.instance.getString(Preferences.id) ?? ''),
2025-05-07 22:06:29 -07:00
),
SwitchListTile(
contentPadding: EdgeInsets.zero,
title: Text(AppLocalizations.of(context)!.trackingLabel),
value: trackingEnabled,
onChanged: (bool value) {
if (value) {
bg.BackgroundGeolocation.start();
2025-06-14 13:26:13 -07:00
_checkBatteryOptimizations(context);
2025-05-07 22:06:29 -07:00
} else {
bg.BackgroundGeolocation.stop();
}
},
),
2025-06-13 18:29:48 -07:00
if (stopDetection == false)
SwitchListTile(
contentPadding: EdgeInsets.zero,
title: Text(AppLocalizations.of(context)!.motionLabel),
value: isMoving == true,
onChanged: (bool value) {
if (value) {
bg.BackgroundGeolocation.changePace(true);
} else {
bg.BackgroundGeolocation.changePace(false);
}
},
),
2025-05-07 22:06:29 -07:00
const SizedBox(height: 8),
2025-05-08 22:10:56 -07:00
OverflowBar(
spacing: 8,
children: [
FilledButton.tonal(
onPressed: () async {
try {
2025-05-31 22:23:11 -07:00
await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true);
2025-05-09 07:56:34 -07:00
await bg.BackgroundGeolocation.sync();
2025-05-08 22:10:56 -07:00
} catch (error) {
developer.log('Failed to fetch location', error: error);
}
},
child: Text(AppLocalizations.of(context)!.locationButton),
),
FilledButton.tonal(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (_) => const StatusScreen()));
},
child: Text(AppLocalizations.of(context)!.statusButton),
),
],
2025-05-07 22:06:29 -07:00
),
],
),
),
);
}
Widget _buildSettingsCard() {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
contentPadding: EdgeInsets.zero,
title: Text(AppLocalizations.of(context)!.settingsTitle),
titleTextStyle: Theme.of(context).textTheme.headlineMedium,
),
ListTile(
contentPadding: EdgeInsets.zero,
title: Text(AppLocalizations.of(context)!.urlLabel),
2025-05-13 07:40:09 -07:00
subtitle: Text(Preferences.instance.getString(Preferences.url) ?? ''),
2025-05-07 22:06:29 -07:00
),
const SizedBox(height: 8),
2025-05-08 22:10:56 -07:00
OverflowBar(
spacing: 8,
children: [
FilledButton.tonal(
2025-05-13 07:40:09 -07:00
onPressed: () async {
await Navigator.push(context, MaterialPageRoute(builder: (_) => const SettingsScreen()));
2025-06-13 18:29:48 -07:00
setState(() {
stopDetection = Preferences.instance.getBool(Preferences.stopDetection);
});
2025-05-08 22:10:56 -07:00
},
child: Text(AppLocalizations.of(context)!.settingsButton),
),
],
2025-05-07 22:06:29 -07:00
),
]
),
),
);
}
2025-05-05 07:47:09 -07:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2025-06-05 20:16:27 -07:00
title: Text('Traccar Client'),
2025-05-05 07:47:09 -07:00
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
2025-05-07 22:06:29 -07:00
_buildTrackingCard(),
const SizedBox(height: 16),
_buildSettingsCard(),
2025-05-05 07:47:09 -07:00
],
),
),
);
}
}