Add accent colour picker, fix empty user card when accepting friend request, add button to transfer allocations
All checks were successful
Release / meta (push) Successful in 8s
Release / linux-build (push) Successful in 56s
Release / web-build (push) Successful in 2m15s
Release / android-build (push) Successful in 6m47s
Release / release-master (push) Successful in 19s
Release / release-dev (push) Successful in 21s
All checks were successful
Release / meta (push) Successful in 8s
Release / linux-build (push) Successful in 56s
Release / web-build (push) Successful in 2m15s
Release / android-build (push) Successful in 6m47s
Release / release-master (push) Successful in 19s
Release / release-dev (push) Successful in 21s
This commit is contained in:
@@ -2,8 +2,10 @@ import 'package:flutter/material.dart';
|
||||
import 'package:mileograph_flutter/services/api_service.dart';
|
||||
import 'package:mileograph_flutter/services/authservice.dart';
|
||||
import 'package:mileograph_flutter/services/data_service.dart';
|
||||
import 'package:mileograph_flutter/services/accent_color_service.dart';
|
||||
import 'package:mileograph_flutter/services/distance_unit_service.dart';
|
||||
import 'package:mileograph_flutter/services/endpoint_service.dart';
|
||||
import 'package:mileograph_flutter/services/theme_mode_service.dart';
|
||||
import 'package:mileograph_flutter/ui/app_shell.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
@@ -17,9 +19,15 @@ class App extends StatelessWidget {
|
||||
ChangeNotifierProvider<EndpointService>(
|
||||
create: (_) => EndpointService(),
|
||||
),
|
||||
ChangeNotifierProvider<AccentColorService>(
|
||||
create: (_) => AccentColorService(),
|
||||
),
|
||||
ChangeNotifierProvider<DistanceUnitService>(
|
||||
create: (_) => DistanceUnitService(),
|
||||
),
|
||||
ChangeNotifierProvider<ThemeModeService>(
|
||||
create: (_) => ThemeModeService(),
|
||||
),
|
||||
ProxyProvider<EndpointService, ApiService>(
|
||||
update: (_, endpoint, api) {
|
||||
final service = api ?? ApiService(baseUrl: endpoint.baseUrl);
|
||||
|
||||
@@ -127,6 +127,9 @@ class _ProfilePageState extends State<ProfilePage> {
|
||||
);
|
||||
if (!mounted) return;
|
||||
setState(() => _status = status);
|
||||
final data = context.read<DataService>();
|
||||
await data.fetchFriendships();
|
||||
await data.fetchPendingFriendships();
|
||||
_showSnack('Friend request sent');
|
||||
} catch (e) {
|
||||
_showSnack('Failed to send request: $e');
|
||||
@@ -159,6 +162,7 @@ class _ProfilePageState extends State<ProfilePage> {
|
||||
final updated = await context.read<DataService>().acceptFriendship(id);
|
||||
if (!mounted) return;
|
||||
setState(() => _status = updated);
|
||||
await context.read<DataService>().fetchFriendships();
|
||||
_showSnack('Friend request accepted');
|
||||
} catch (e) {
|
||||
_showSnack('Failed to accept: $e');
|
||||
@@ -175,6 +179,7 @@ class _ProfilePageState extends State<ProfilePage> {
|
||||
final updated = await context.read<DataService>().rejectFriendship(id);
|
||||
if (!mounted) return;
|
||||
setState(() => _status = updated);
|
||||
await context.read<DataService>().fetchPendingFriendships();
|
||||
_showSnack('Friend request rejected');
|
||||
} catch (e) {
|
||||
_showSnack('Failed to reject: $e');
|
||||
|
||||
@@ -3,8 +3,10 @@ import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:mileograph_flutter/services/accent_color_service.dart';
|
||||
import 'package:mileograph_flutter/services/distance_unit_service.dart';
|
||||
import 'package:mileograph_flutter/services/endpoint_service.dart';
|
||||
import 'package:mileograph_flutter/services/theme_mode_service.dart';
|
||||
import 'package:mileograph_flutter/services/data_service.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
@@ -18,6 +20,18 @@ class SettingsPage extends StatefulWidget {
|
||||
class _SettingsPageState extends State<SettingsPage> {
|
||||
late final TextEditingController _endpointController;
|
||||
bool _saving = false;
|
||||
static const List<Color> _accentPalette = [
|
||||
Colors.red,
|
||||
Colors.pink,
|
||||
Colors.orange,
|
||||
Colors.amber,
|
||||
Colors.green,
|
||||
Colors.teal,
|
||||
Colors.blue,
|
||||
Colors.indigo,
|
||||
Colors.purple,
|
||||
Colors.cyan,
|
||||
];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -130,7 +144,12 @@ class _SettingsPageState extends State<SettingsPage> {
|
||||
Widget build(BuildContext context) {
|
||||
final endpointService = context.watch<EndpointService>();
|
||||
final distanceUnitService = context.watch<DistanceUnitService>();
|
||||
if (!endpointService.isLoaded || !distanceUnitService.isLoaded) {
|
||||
final accentService = context.watch<AccentColorService>();
|
||||
final themeModeService = context.watch<ThemeModeService>();
|
||||
if (!endpointService.isLoaded ||
|
||||
!distanceUnitService.isLoaded ||
|
||||
!accentService.isLoaded ||
|
||||
!themeModeService.isLoaded) {
|
||||
return const Scaffold(
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
@@ -184,6 +203,73 @@ class _SettingsPageState extends State<SettingsPage> {
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
'Accent colour',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Choose your preferred accent colour or use system colours.',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
onPressed:
|
||||
accentService.useSystem ? null : () => accentService.setUseSystem(true),
|
||||
icon: const Icon(Icons.phone_android),
|
||||
label: const Text('Use system colours'),
|
||||
),
|
||||
..._accentPalette.map(
|
||||
(color) => _AccentSwatchButton(
|
||||
color: color,
|
||||
selected:
|
||||
!accentService.useSystem &&
|
||||
accentService.seedColor.toARGB32() == color.toARGB32(),
|
||||
onTap: () => accentService.setSeedColor(color),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
'Theme mode',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
SegmentedButton<ThemeMode>(
|
||||
segments: const [
|
||||
ButtonSegment(
|
||||
value: ThemeMode.system,
|
||||
icon: Icon(Icons.settings_suggest),
|
||||
label: Text('System'),
|
||||
),
|
||||
ButtonSegment(
|
||||
value: ThemeMode.light,
|
||||
icon: Icon(Icons.light_mode),
|
||||
label: Text('Light'),
|
||||
),
|
||||
ButtonSegment(
|
||||
value: ThemeMode.dark,
|
||||
icon: Icon(Icons.dark_mode),
|
||||
label: Text('Dark'),
|
||||
),
|
||||
],
|
||||
selected: {themeModeService.mode},
|
||||
onSelectionChanged: (selection) {
|
||||
final mode = selection.first;
|
||||
themeModeService.setMode(mode);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
'API endpoint',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
@@ -241,3 +327,54 @@ class _SettingsPageState extends State<SettingsPage> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _AccentSwatchButton extends StatelessWidget {
|
||||
const _AccentSwatchButton({
|
||||
required this.color,
|
||||
required this.selected,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
final Color color;
|
||||
final bool selected;
|
||||
final VoidCallback onTap;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final borderColor = selected
|
||||
? Theme.of(context).colorScheme.onSurface
|
||||
: Colors.black26;
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
customBorder: const CircleBorder(),
|
||||
child: Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(
|
||||
color: borderColor,
|
||||
width: selected ? 3 : 1,
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: 0.15),
|
||||
blurRadius: 4,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: selected
|
||||
? const Center(
|
||||
child: Icon(
|
||||
Icons.check,
|
||||
size: 18,
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ class TractionPage extends StatefulWidget {
|
||||
this.selectionMode = false,
|
||||
this.selectionSingle = false,
|
||||
this.replacementPendingLocoId,
|
||||
this.transferFromLocoId,
|
||||
this.onSelect,
|
||||
this.selectedKeys = const {},
|
||||
});
|
||||
@@ -19,6 +20,7 @@ class TractionPage extends StatefulWidget {
|
||||
final bool selectionMode;
|
||||
final bool selectionSingle;
|
||||
final int? replacementPendingLocoId;
|
||||
final int? transferFromLocoId;
|
||||
final ValueChanged<LocoSummary>? onSelect;
|
||||
final Set<String> selectedKeys;
|
||||
|
||||
@@ -33,6 +35,7 @@ class _TractionPageState extends State<TractionPage> {
|
||||
final _nameController = TextEditingController();
|
||||
bool _mileageFirst = true;
|
||||
bool _initialised = false;
|
||||
int? get _transferFromLocoId => widget.transferFromLocoId;
|
||||
bool _showAdvancedFilters = false;
|
||||
String? _selectedClass;
|
||||
late Set<String> _selectedKeys;
|
||||
@@ -1200,6 +1203,53 @@ class _TractionPageState extends State<TractionPage> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _confirmTransfer(LocoSummary target) async {
|
||||
final fromId = _transferFromLocoId;
|
||||
if (fromId == null) return;
|
||||
final navContext = context;
|
||||
final messenger = ScaffoldMessenger.of(navContext);
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: navContext,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
title: const Text('Transfer allocations?'),
|
||||
content: Text(
|
||||
'Transfer all allocations from this loco to ${target.locoClass} ${target.number}?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: const Text('Transfer'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
if (confirmed != true) return;
|
||||
if (!navContext.mounted) return;
|
||||
try {
|
||||
final data = navContext.read<DataService>();
|
||||
await data.transferAllocations(fromLocoId: fromId, toLocoId: target.id);
|
||||
if (navContext.mounted) {
|
||||
messenger.showSnackBar(
|
||||
const SnackBar(content: Text('Allocations transferred')),
|
||||
);
|
||||
}
|
||||
await _refreshTraction(preservePosition: true);
|
||||
if (navContext.mounted) navContext.pop();
|
||||
} catch (e) {
|
||||
if (navContext.mounted) {
|
||||
messenger.showSnackBar(
|
||||
SnackBar(content: Text('Failed to transfer allocations: $e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool _isSelected(LocoSummary loco) {
|
||||
final keyVal = '${loco.locoClass}-${loco.number}';
|
||||
return _selectedKeys.contains(keyVal);
|
||||
@@ -1348,12 +1398,18 @@ class _TractionPageState extends State<TractionPage> {
|
||||
widget.replacementPendingLocoId == null
|
||||
? () => _toggleSelection(loco)
|
||||
: null,
|
||||
onReplacePending: widget.selectionMode &&
|
||||
widget.selectionSingle &&
|
||||
widget.replacementPendingLocoId != null
|
||||
? () => _confirmReplacePending(loco)
|
||||
: null,
|
||||
);
|
||||
onReplacePending: widget.selectionMode &&
|
||||
widget.selectionSingle &&
|
||||
widget.replacementPendingLocoId != null
|
||||
? () => _confirmReplacePending(loco)
|
||||
: null,
|
||||
onTransferAllocations: widget.selectionMode &&
|
||||
widget.selectionSingle &&
|
||||
widget.transferFromLocoId != null &&
|
||||
widget.transferFromLocoId != loco.id
|
||||
? () => _confirmTransfer(loco)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
return Padding(
|
||||
|
||||
@@ -20,6 +20,7 @@ class TractionCard extends StatelessWidget {
|
||||
this.onToggleSelect,
|
||||
this.onReplacePending,
|
||||
this.onActionComplete,
|
||||
this.onTransferAllocations,
|
||||
});
|
||||
|
||||
final LocoSummary loco;
|
||||
@@ -31,6 +32,7 @@ class TractionCard extends StatelessWidget {
|
||||
final VoidCallback? onToggleSelect;
|
||||
final VoidCallback? onReplacePending;
|
||||
final Future<void> Function()? onActionComplete;
|
||||
final VoidCallback? onTransferAllocations;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -145,23 +147,30 @@ class TractionCard extends StatelessWidget {
|
||||
];
|
||||
|
||||
// Prefer replace action when picking a replacement loco.
|
||||
final addButton = onReplacePending != null
|
||||
final addButton = onTransferAllocations != null
|
||||
? TextButton.icon(
|
||||
onPressed: onReplacePending,
|
||||
onPressed: onTransferAllocations,
|
||||
icon: const Icon(Icons.swap_horiz),
|
||||
label: const Text('Replace'),
|
||||
label: const Text('Transfer'),
|
||||
)
|
||||
: (!isRejected && selectionMode && onToggleSelect != null)
|
||||
: onReplacePending != null
|
||||
? TextButton.icon(
|
||||
onPressed: onToggleSelect,
|
||||
icon: Icon(
|
||||
isSelected
|
||||
? Icons.remove_circle_outline
|
||||
: Icons.add_circle_outline,
|
||||
),
|
||||
label: Text(isSelected ? 'Remove' : 'Add to entry'),
|
||||
onPressed: onReplacePending,
|
||||
icon: const Icon(Icons.swap_horiz),
|
||||
label: const Text('Replace'),
|
||||
)
|
||||
: null;
|
||||
: (!isRejected && selectionMode && onToggleSelect != null)
|
||||
? TextButton.icon(
|
||||
onPressed: onToggleSelect,
|
||||
icon: Icon(
|
||||
isSelected
|
||||
? Icons.remove_circle_outline
|
||||
: Icons.add_circle_outline,
|
||||
),
|
||||
label:
|
||||
Text(isSelected ? 'Remove' : 'Add to entry'),
|
||||
)
|
||||
: null;
|
||||
|
||||
if (isNarrow) {
|
||||
return Column(
|
||||
@@ -551,6 +560,7 @@ Future<void> showTractionDetails(
|
||||
LocoSummary loco, {
|
||||
Future<void> Function()? onActionComplete,
|
||||
}) async {
|
||||
final navContext = context;
|
||||
final hasMileageOrTrips = _hasMileageOrTrips(loco);
|
||||
final isVisibilityPending =
|
||||
(loco.visibility ?? '').toLowerCase().trim() == 'pending';
|
||||
@@ -583,11 +593,11 @@ Future<void> showTractionDetails(
|
||||
builder: (_, controller) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: () => Navigator.of(ctx).pop(),
|
||||
@@ -620,16 +630,37 @@ Future<void> showTractionDetails(
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Expanded(
|
||||
child: ListView(
|
||||
controller: controller,
|
||||
children: [
|
||||
if (isRejected && rejectedReason.isNotEmpty)
|
||||
...[
|
||||
_detailRow(
|
||||
context,
|
||||
'Rejection reason',
|
||||
const SizedBox(height: 4),
|
||||
Expanded(
|
||||
child: ListView(
|
||||
controller: controller,
|
||||
children: [
|
||||
FilledButton.icon(
|
||||
onPressed: () {
|
||||
Navigator.of(ctx).pop();
|
||||
navContext.push(
|
||||
Uri(
|
||||
path: '/traction',
|
||||
queryParameters: {
|
||||
'selection': 'single',
|
||||
'transferFromLocoId': loco.id.toString(),
|
||||
},
|
||||
).toString(),
|
||||
extra: {
|
||||
'selection': 'single',
|
||||
'transferFromLocoId': loco.id,
|
||||
},
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.swap_horiz),
|
||||
label: const Text('Transfer allocations'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (isRejected && rejectedReason.isNotEmpty)
|
||||
...[
|
||||
_detailRow(
|
||||
context,
|
||||
'Rejection reason',
|
||||
rejectedReason,
|
||||
),
|
||||
const Divider(),
|
||||
|
||||
51
lib/services/accent_color_service.dart
Normal file
51
lib/services/accent_color_service.dart
Normal file
@@ -0,0 +1,51 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class AccentColorService extends ChangeNotifier {
|
||||
static const _prefsKeyUseSystem = 'accent_use_system';
|
||||
static const _prefsKeySeed = 'accent_seed';
|
||||
static const Color defaultSeed = Colors.red;
|
||||
|
||||
bool _useSystem = true;
|
||||
Color _seedColor = defaultSeed;
|
||||
bool _hasSavedSeed = false;
|
||||
bool _loaded = false;
|
||||
|
||||
bool get useSystem => _useSystem;
|
||||
Color get seedColor => _seedColor;
|
||||
bool get hasSavedSeed => _hasSavedSeed;
|
||||
bool get isLoaded => _loaded;
|
||||
|
||||
AccentColorService() {
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_useSystem = prefs.getBool(_prefsKeyUseSystem) ?? true;
|
||||
final seedValue = prefs.getInt(_prefsKeySeed);
|
||||
if (seedValue != null) {
|
||||
_seedColor = Color(seedValue);
|
||||
_hasSavedSeed = true;
|
||||
}
|
||||
_loaded = true;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setUseSystem(bool value) async {
|
||||
_useSystem = value;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_prefsKeyUseSystem, _useSystem);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setSeedColor(Color color) async {
|
||||
_seedColor = color;
|
||||
_useSystem = false;
|
||||
_hasSavedSeed = true;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_prefsKeySeed, color.toARGB32());
|
||||
await prefs.setBool(_prefsKeyUseSystem, _useSystem);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -151,6 +151,8 @@ extension DataServiceFriendships on DataService {
|
||||
overrideAddressee: targetUser,
|
||||
);
|
||||
_pendingOutgoing = [friendship, ..._pendingOutgoing];
|
||||
await fetchFriendships();
|
||||
await fetchPendingFriendships();
|
||||
_notifyAsync();
|
||||
return friendship;
|
||||
}
|
||||
@@ -159,6 +161,8 @@ extension DataServiceFriendships on DataService {
|
||||
final json = await api.post('/friendships/$friendshipId/accept', {});
|
||||
final friendship = _parseAndUpsertFriendship(json, fallbackStatus: 'accepted');
|
||||
_pendingIncoming = _pendingIncoming.where((f) => f.id != friendshipId).toList();
|
||||
await fetchFriendships();
|
||||
await fetchPendingFriendships();
|
||||
_notifyAsync();
|
||||
return friendship;
|
||||
}
|
||||
@@ -177,6 +181,8 @@ extension DataServiceFriendships on DataService {
|
||||
_parseAndRemoveFriendship(json, friendshipId, status: 'none');
|
||||
_pendingOutgoing =
|
||||
_pendingOutgoing.where((f) => f.id != friendshipId).toList();
|
||||
await fetchFriendships();
|
||||
await fetchPendingFriendships();
|
||||
_notifyAsync();
|
||||
return friendship;
|
||||
}
|
||||
@@ -193,6 +199,8 @@ extension DataServiceFriendships on DataService {
|
||||
_pendingIncoming.where((f) => f.id != friendshipId).toList();
|
||||
_pendingOutgoing =
|
||||
_pendingOutgoing.where((f) => f.id != friendshipId).toList();
|
||||
await fetchFriendships();
|
||||
await fetchPendingFriendships();
|
||||
_notifyAsync();
|
||||
}
|
||||
|
||||
|
||||
@@ -466,6 +466,21 @@ extension DataServiceTraction on DataService {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> transferAllocations({
|
||||
required int fromLocoId,
|
||||
required int toLocoId,
|
||||
}) async {
|
||||
try {
|
||||
await api.post('/loco/alloc/transfer', {
|
||||
'loco_id': fromLocoId,
|
||||
'to_loco_id': toLocoId,
|
||||
});
|
||||
} catch (e) {
|
||||
debugPrint('Failed to transfer allocations $fromLocoId -> $toLocoId: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> adminDeleteLoco({required int locoId}) async {
|
||||
try {
|
||||
await api.delete('/loco/admin/delete/$locoId');
|
||||
|
||||
47
lib/services/theme_mode_service.dart
Normal file
47
lib/services/theme_mode_service.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class ThemeModeService extends ChangeNotifier {
|
||||
static const _prefsKey = 'theme_mode_preference';
|
||||
|
||||
ThemeMode _mode = ThemeMode.system;
|
||||
bool _loaded = false;
|
||||
|
||||
ThemeMode get mode => _mode;
|
||||
bool get isLoaded => _loaded;
|
||||
|
||||
ThemeModeService() {
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final saved = prefs.getString(_prefsKey);
|
||||
if (saved != null) {
|
||||
switch (saved) {
|
||||
case 'light':
|
||||
_mode = ThemeMode.light;
|
||||
break;
|
||||
case 'dark':
|
||||
_mode = ThemeMode.dark;
|
||||
break;
|
||||
default:
|
||||
_mode = ThemeMode.system;
|
||||
}
|
||||
}
|
||||
_loaded = true;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setMode(ThemeMode mode) async {
|
||||
_mode = mode;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final value = switch (mode) {
|
||||
ThemeMode.light => 'light',
|
||||
ThemeMode.dark => 'dark',
|
||||
_ => 'system',
|
||||
};
|
||||
await prefs.setString(_prefsKey, value);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,10 @@ import 'package:mileograph_flutter/components/widgets/leg_share_edit_notificatio
|
||||
import 'package:mileograph_flutter/components/widgets/leg_share_notification_card.dart';
|
||||
import 'package:mileograph_flutter/objects/objects.dart';
|
||||
import 'package:mileograph_flutter/services/authservice.dart';
|
||||
import 'package:mileograph_flutter/services/accent_color_service.dart';
|
||||
import 'package:mileograph_flutter/services/data_service.dart';
|
||||
import 'package:mileograph_flutter/services/navigation_guard.dart';
|
||||
import 'package:mileograph_flutter/services/theme_mode_service.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
final GlobalKey<NavigatorState> _shellNavigatorKey =
|
||||
@@ -103,12 +105,6 @@ class _MyAppState extends State<MyApp> {
|
||||
late final GoRouter _router;
|
||||
bool _routerInitialized = false;
|
||||
|
||||
final ColorScheme defaultLight = ColorScheme.fromSeed(seedColor: Colors.red);
|
||||
final ColorScheme defaultDark = ColorScheme.fromSeed(
|
||||
seedColor: Colors.red,
|
||||
brightness: Brightness.dark,
|
||||
);
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
@@ -188,10 +184,23 @@ class _MyAppState extends State<MyApp> {
|
||||
'',
|
||||
)
|
||||
: null;
|
||||
final transferFromLocoIdStr =
|
||||
state.uri.queryParameters['transferFromLocoId'];
|
||||
final transferFromLocoId = transferFromLocoIdStr != null
|
||||
? int.tryParse(transferFromLocoIdStr)
|
||||
: state.extra is Map
|
||||
? int.tryParse(
|
||||
(state.extra as Map)['transferFromLocoId']
|
||||
?.toString() ??
|
||||
'',
|
||||
)
|
||||
: null;
|
||||
final selectionMode =
|
||||
(selectionParam != null && selectionParam.isNotEmpty) ||
|
||||
replacementPendingLocoId != null;
|
||||
replacementPendingLocoId != null ||
|
||||
transferFromLocoId != null;
|
||||
final selectionSingle = replacementPendingLocoId != null ||
|
||||
transferFromLocoId != null ||
|
||||
selectionParam?.toLowerCase() == 'single' ||
|
||||
selectionParam == '1' ||
|
||||
selectionParam?.toLowerCase() == 'true';
|
||||
@@ -199,6 +208,7 @@ class _MyAppState extends State<MyApp> {
|
||||
selectionMode: selectionMode,
|
||||
selectionSingle: selectionSingle,
|
||||
replacementPendingLocoId: replacementPendingLocoId,
|
||||
transferFromLocoId: transferFromLocoId,
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -325,20 +335,34 @@ class _MyAppState extends State<MyApp> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final accent = context.watch<AccentColorService>();
|
||||
final themeModeService = context.watch<ThemeModeService>();
|
||||
final seedColor =
|
||||
accent.hasSavedSeed ? accent.seedColor : AccentColorService.defaultSeed;
|
||||
final useSystemColors = accent.useSystem;
|
||||
return DynamicColorBuilder(
|
||||
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
|
||||
final colorSchemeLight = useSystemColors && lightDynamic != null
|
||||
? lightDynamic
|
||||
: ColorScheme.fromSeed(seedColor: seedColor);
|
||||
final colorSchemeDark = useSystemColors && darkDynamic != null
|
||||
? darkDynamic
|
||||
: ColorScheme.fromSeed(
|
||||
seedColor: seedColor,
|
||||
brightness: Brightness.dark,
|
||||
);
|
||||
return MaterialApp.router(
|
||||
title: 'Mileograph',
|
||||
routerConfig: _router,
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: lightDynamic ?? defaultLight,
|
||||
colorScheme: colorSchemeLight,
|
||||
),
|
||||
darkTheme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: darkDynamic ?? defaultDark,
|
||||
colorScheme: colorSchemeDark,
|
||||
),
|
||||
themeMode: ThemeMode.system,
|
||||
themeMode: themeModeService.mode,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
|
||||
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
|
||||
# In Windows, build-name is used as the major, minor, and patch parts
|
||||
# of the product and file versions while build-number is used as the build suffix.
|
||||
version: 0.7.0+8
|
||||
version: 0.7.1+9
|
||||
|
||||
environment:
|
||||
sdk: ^3.8.1
|
||||
|
||||
Reference in New Issue
Block a user