speed_settings_card.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import '../../core/theme/app_colors.dart';
  4. import '../../core/widgets/app_icon.dart';
  5. import '../home/obd_controller.dart';
  6. import '../home/telemetry_settings_controller.dart';
  7. import 'settings_widgets.dart';
  8. /// Settings card for showing speed on Home and choosing its source.
  9. class SpeedSettingsCard extends StatelessWidget {
  10. const SpeedSettingsCard({super.key});
  11. @override
  12. Widget build(BuildContext context) {
  13. final colors = context.colors;
  14. final theme = Theme.of(context);
  15. final telemetry = context.watch<TelemetrySettingsController>();
  16. final obd = context.watch<ObdController>();
  17. return Column(
  18. crossAxisAlignment: CrossAxisAlignment.start,
  19. children: [
  20. Row(
  21. children: [
  22. Container(
  23. width: 42,
  24. height: 42,
  25. decoration: BoxDecoration(
  26. color: colors.accentSoft,
  27. borderRadius: BorderRadius.circular(14),
  28. ),
  29. alignment: Alignment.center,
  30. child: AppIcon(
  31. 'assets/icons/gauge.svg',
  32. size: 20,
  33. color: colors.accent,
  34. ),
  35. ),
  36. const SizedBox(width: 14),
  37. Expanded(
  38. child: Column(
  39. crossAxisAlignment: CrossAxisAlignment.start,
  40. children: [
  41. Text('Show speed', style: theme.textTheme.titleMedium),
  42. const SizedBox(height: 2),
  43. Text(
  44. 'Show the speed card on the home screen.',
  45. style: theme.textTheme.bodySmall?.copyWith(
  46. color: colors.inkMuted,
  47. ),
  48. ),
  49. ],
  50. ),
  51. ),
  52. const SizedBox(width: 8),
  53. SettingsSwitch(
  54. value: telemetry.speedEnabled,
  55. semanticsLabel: telemetry.speedEnabled
  56. ? 'Hide the speed card'
  57. : 'Show the speed card',
  58. onChanged: (value) async {
  59. final ok = await telemetry.setSpeedEnabled(value);
  60. if (!ok && context.mounted) {
  61. _message(
  62. context,
  63. 'Location permission is required for GPS speed.',
  64. );
  65. }
  66. },
  67. ),
  68. ],
  69. ),
  70. if (obd.hasConfiguredAdapter) ...[
  71. Divider(color: colors.border, height: 30),
  72. Row(
  73. children: [
  74. Expanded(
  75. child: Column(
  76. crossAxisAlignment: CrossAxisAlignment.start,
  77. children: [
  78. Text(
  79. 'Use OBD-II for speed',
  80. style: theme.textTheme.bodyLarge,
  81. ),
  82. const SizedBox(height: 2),
  83. Text(
  84. 'GPS is used when this is off.',
  85. style: theme.textTheme.bodySmall?.copyWith(
  86. color: colors.inkMuted,
  87. ),
  88. ),
  89. ],
  90. ),
  91. ),
  92. const SizedBox(width: 8),
  93. SettingsSwitch(
  94. value: telemetry.speedUseObd,
  95. semanticsLabel: telemetry.speedUseObd
  96. ? 'Use GPS for speed'
  97. : 'Use OBD-II for speed',
  98. onChanged: (value) async {
  99. final ok = await telemetry.setSpeedUseObd(value);
  100. if (!ok && context.mounted) {
  101. _message(
  102. context,
  103. 'Location permission is required for GPS speed.',
  104. );
  105. }
  106. },
  107. ),
  108. ],
  109. ),
  110. ],
  111. ],
  112. );
  113. }
  114. void _message(BuildContext context, String message) {
  115. final messenger = ScaffoldMessenger.of(context);
  116. messenger.hideCurrentSnackBar();
  117. messenger.showSnackBar(SnackBar(content: Text(message)));
  118. }
  119. }