Add motion switch

This commit is contained in:
Anton Tananaev 2025-06-13 18:29:48 -07:00
parent 1dc26d8f4a
commit a19da07ac5
2 changed files with 26 additions and 1 deletions

View file

@ -17,6 +17,8 @@ class MainScreen extends StatefulWidget {
class _MainScreenState extends State<MainScreen> {
bool trackingEnabled = false;
bool? stopDetection;
bool? isMoving;
@override
void initState() {
@ -28,12 +30,19 @@ class _MainScreenState extends State<MainScreen> {
final state = await bg.BackgroundGeolocation.state;
setState(() {
trackingEnabled = state.enabled;
stopDetection = Preferences.instance.getBool(Preferences.stopDetection);
isMoving = state.isMoving;
});
bg.BackgroundGeolocation.onEnabledChange((bool enabled) {
setState(() {
trackingEnabled = enabled;
});
});
bg.BackgroundGeolocation.onMotionChange((bg.Location location) {
setState(() {
isMoving = location.isMoving;
});
});
}
Widget _buildTrackingCard() {
@ -65,6 +74,19 @@ class _MainScreenState extends State<MainScreen> {
}
},
),
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);
}
},
),
const SizedBox(height: 8),
OverflowBar(
spacing: 8,
@ -118,7 +140,9 @@ class _MainScreenState extends State<MainScreen> {
FilledButton.tonal(
onPressed: () async {
await Navigator.push(context, MaterialPageRoute(builder: (_) => const SettingsScreen()));
setState(() {});
setState(() {
stopDetection = Preferences.instance.getBool(Preferences.stopDetection);
});
},
child: Text(AppLocalizations.of(context)!.settingsButton),
),