Files
SDKmuta/lib/screens/settings_screen.dart
T
2026-11-28 12:22:02 +01:00

571 lines
21 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/foundation.dart' show kDebugMode;
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../models/holiday_theme.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import '../models/target_style.dart';
import '../services/app_settings.dart';
import '../utils/translations.dart';
import '../services/notification_service.dart';
import '../theme/app_theme.dart';
import '../widgets/section.dart';
import '../widgets/target_face.dart';
import '../widgets/text_prompt.dart';
import '../widgets/ui.dart';
import 'camera_settings_screen.dart';
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
static const _privacyPolicyUrl = 'https://sdkovinarmuta.si/privacy-and-policy/';
static final Future<PackageInfo> _packageInfo = PackageInfo.fromPlatform();
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: AppSettings.instance,
builder: (context, _) {
final s = AppSettings.instance;
final t = s.t;
final c = Palette.current;
return ListView(
padding: EdgeInsets.zero,
children: [
PageHeader(title: t('Settings')),
Padding(
padding: const EdgeInsets.fromLTRB(AppTheme.edge, 0, AppTheme.edge, 28),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SectionHeader(t('Language')),
GroupCard(children: [
SettingsTile(
icon: Icons.language_rounded,
title: t('Language'),
value: t(s.language),
onTap: () => _pickLanguage(context),
),
]),
SectionHeader(t('Appearance')),
GroupCard(children: [
SettingsTile(
icon: Icons.dark_mode_outlined,
title: t('Theme'),
trailing: SizedBox(
width: 196,
child: SegmentedTabs(
height: 36,
accentSelected: true,
items: [SegmentItem(t('System')), SegmentItem(t('Light')), SegmentItem(t('Dark'))],
selected: AppSettings.themeModes.indexOf(s.themeMode),
onSelect: (i) => s.setThemeMode(AppSettings.themeModes[i]),
),
),
),
// Hit colour and target design are picked in one sheet
SettingsTile(
icon: Icons.track_changes_rounded,
title: t('Hits and target'),
valueLeading: Row(
mainAxisSize: MainAxisSize.min,
children: [
_ColorDot(s.bulletColor, size: 16),
const SizedBox(width: 8),
TargetThumb(s.target, size: 26),
],
),
value: s.target.name(s.language),
onTap: () => showAppearanceSheet(context),
),
]),
SectionHeader('${t('Club')} · SD Kovinar Muta'),
GroupCard(children: [
if (s.clubUnlocked)
SettingsTile(
icon: Icons.verified_user_outlined,
title: t('Member access'),
value: t('Unlocked'),
valueColor: c.pos,
showChevron: false,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(t('Unlocked'), style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: c.pos)),
const SizedBox(width: 8),
Container(
width: 22,
height: 22,
decoration: BoxDecoration(color: c.pos, shape: BoxShape.circle),
child: Icon(Icons.check_rounded, size: 15, color: Palette.onColor(c.pos)),
),
],
),
onTap: () => _confirmLock(context),
)
else
SettingsTile(
icon: Icons.lock_outline_rounded,
title: t('Club code'),
subtitle: t('Unlocks calendar and contacts'),
onTap: () => _enterClubCode(context),
),
]),
if (!s.clubUnlocked)
Padding(
padding: const EdgeInsets.fromLTRB(4, 10, 4, 0),
child: Text(
t('Member access is currently available only for SD Kovinar Muta. Support for other clubs may be added in the future.'),
style: TextStyle(fontSize: 12, height: 1.35, color: c.muted),
),
),
if (s.clubUnlocked) ...[
SectionHeader(t('Notifications')),
GroupCard(children: notificationTiles(context)),
],
SectionHeader(t('Cameras')),
GroupCard(children: [
SettingsTile(
icon: Icons.videocam_outlined,
title: t('Camera Settings'),
value: _camerasSummary(s),
onTap: () => Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const CameraSettingsScreen()),
),
),
]),
SectionHeader(t('About')),
GroupCard(children: [
SettingsTile(
icon: Icons.shield_outlined,
title: t('Privacy policy'),
chevron: Icons.open_in_new_rounded,
onTap: () => launchUrl(Uri.parse(_privacyPolicyUrl), mode: LaunchMode.externalApplication),
),
FutureBuilder<PackageInfo>(
future: _packageInfo,
// Hidden (any build): long press previews a holiday
builder: (context, snap) => GestureDetector(
onLongPress: () => _previewHoliday(context),
child: SettingsTile(
icon: Icons.info_outline_rounded,
title: t('Version'),
value: snap.data?.version ?? '',
),
),
),
]),
// Debug builds only (never in a release): check the holiday looks
if (kDebugMode) ...[
const SectionHeader('Debug'),
GroupCard(children: [
SettingsTile(
icon: Icons.celebration_outlined,
title: 'Holiday preview',
subtitle: 'Shows the app as on a day of the holiday',
value: s.debugNoHoliday ? 'Default' : (s.debugToday == null ? 'Today' : (s.holiday?.name ?? 'Today')),
onTap: () => _previewHoliday(context),
),
]),
],
],
),
),
],
);
},
);
}
static void _reschedule() => NotificationService().rescheduleFromCalendar();
// Debug: see the app as on a day of each holiday (Nastavitve → Debug)
static void _previewHoliday(BuildContext context) {
final s = AppSettings.instance;
final year = DateTime.now().year;
showAppSheet(
context,
title: 'Holiday preview (debug)',
builder: (sheet) => Column(
children: [
SheetOption(
label: 'Today',
subtitle: s.debugToday == null && !s.debugNoHoliday ? null : 'The real date',
selected: s.debugToday == null && !s.debugNoHoliday,
showDivider: false,
onTap: () {
s.previewDay(null);
Navigator.pop(sheet);
},
),
SheetOption(
label: 'Default (no holiday)',
subtitle: 'Everyday look, your own target and colour',
leading: TargetThumb(s.ownTarget, size: 32),
selected: s.debugNoHoliday,
onTap: () {
s.previewDay(null, noHoliday: true);
Navigator.pop(sheet);
},
),
for (final h in HolidayTheme.all)
Builder(builder: (_) {
// First day of the holiday this year
final day = h.easter != null
? HolidayTheme.easterDay(year, h.easter!.$1)
: DateTime(year, h.from!.$1, h.from!.$2);
final (start, end) = h.window(day);
return SheetOption(
label: h.name,
subtitle: '${DateFormat('d. M.').format(start)} – ${DateFormat('d. M.').format(end)}',
leading: TargetThumb(TargetStyle.byId(h.targetId), size: 32),
selected: s.holiday == h && s.debugToday != null && !s.debugNoHoliday,
onTap: () {
s.previewDay(day);
Navigator.pop(sheet);
},
);
}),
],
),
);
}
/// Dežuranje, Sestanki, Tekme and "Opomni me"; also shown from the calendar's bell.
static List<Widget> notificationTiles(BuildContext context) {
final s = AppSettings.instance;
final t = s.t;
return [
SettingsSwitchTile(
icon: Icons.shield_outlined,
title: t('Dežuranje'),
value: s.notifyDezuranje,
onChanged: (v) => s.setNotifyDezuranje(v).then((_) => _reschedule()),
),
SettingsSwitchTile(
icon: Icons.person_add_alt_outlined,
title: t('Sestanek'),
value: s.notifySestanek,
onChanged: (v) => s.setNotifySestanek(v).then((_) => _reschedule()),
),
SettingsSwitchTile(
icon: Icons.emoji_events_outlined,
title: t('Tekma'),
value: s.notifyTekma,
onChanged: (v) => s.setNotifyTekma(v).then((_) => _reschedule()),
),
SettingsTile(
icon: Icons.notifications_none_rounded,
title: t('Remind me'),
value: hoursBefore(s.reminderHours),
onTap: () => pickReminder(context),
),
];
}
Future<void> _enterClubCode(BuildContext context) async {
final s = AppSettings.instance;
final code = await showTextPrompt(
context,
title: s.t('Club code'),
message: s.t('Unlocks calendar and contacts.'),
titleIcon: Icons.lock_outline_rounded,
fieldIcon: Icons.key_rounded,
mono: true,
note: s.t('Member access is currently available only for SD Kovinar Muta.'),
confirmLabel: s.t('Unlock'),
confirmIcon: Icons.key_rounded,
);
if (code == null || code.isEmpty || !context.mounted) return;
// Unlocking takes about a second (slow key derivation on purpose)
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => const Center(child: CircularProgressIndicator()),
);
final ok = await s.unlockClub(code);
if (!context.mounted) return;
Navigator.of(context).pop();
if (ok) _reschedule();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(s.t(ok ? 'Club access unlocked' : 'Wrong club code'))));
}
Future<void> _confirmLock(BuildContext context) async {
final s = AppSettings.instance;
final confirmed = await confirmDialog(
context,
title: s.t('Member access'),
message: s.t('Remove club access from this phone?'),
confirm: s.t('Remove'),
);
if (confirmed) {
await s.lockClub();
_reschedule(); // cancels reminders
}
}
static String hoursBefore(int hours) => AppSettings.instance.t('{n} h before').replaceAll('{n}', '$hours');
// "2 strelišči", or a hint when nothing is set up yet.
static String _camerasSummary(AppSettings s) {
final n = s.cameras.locations.length;
if (n == 0) return s.t('Not set up');
if (s.language == 'Slovenian') {
final mod = n % 100;
return '$n ${mod == 1 ? 'strelišče' : mod == 2 ? 'strelišči' : (mod == 3 || mod == 4) ? 'strelišča' : 'strelišč'}';
}
return s.cameras.locations.join(' · ');
}
void _pickLanguage(BuildContext context) {
final s = AppSettings.instance;
showAppSheet(
context,
title: s.t('Select Language'),
builder: (sheet) => Column(
children: [
// Each language in its own name, so it can be found from any language
for (final lang in AppSettings.languages)
SheetOption(
label: AppTranslations.translate(lang['name']!, lang['name']!),
leading: Text(lang['flag']!, style: const TextStyle(fontSize: 22)),
selected: lang['name'] == s.language,
onTap: () {
s.setLanguage(lang['name']!);
Navigator.pop(sheet);
},
),
],
),
);
}
static void pickReminder(BuildContext context) {
final s = AppSettings.instance;
showAppSheet(
context,
title: s.t('Remind me'),
builder: (sheet) => Column(
children: [
for (final h in AppSettings.reminderHourOptions)
SheetOption(
label: hoursBefore(h),
selected: h == s.reminderHours,
onTap: () {
s.setReminderHours(h).then((_) => _reschedule());
Navigator.pop(sheet);
},
),
],
),
);
}
}
/// Videz: hit colour (the accent of the whole app) and the target design.
void showAppearanceSheet(BuildContext context) {
final s = AppSettings.instance;
showAppSheet(
context,
builder: (sheet) => ListenableBuilder(
listenable: s,
builder: (context, _) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SheetTitle(s.t('Appearance'), trailing: TargetThumb(s.target, size: 52)),
Kicker(s.t('Bullet color'), padding: const EdgeInsets.only(bottom: 12)),
// During a holiday its colour gets its own line first (and is the
// one in use until another is picked)
if (s.holiday != null)
Padding(
padding: const EdgeInsets.only(bottom: 12),
child: GestureDetector(
onTap: s.useHolidayColor,
behavior: HitTestBehavior.opaque,
child: Row(
children: [
_ColorDot(s.holiday!.color, size: 32, selected: s.holidayColorActive),
const SizedBox(width: 10),
Text(s.holiday!.name, style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Palette.current.ink2)),
],
),
),
),
// Two rows of 8, round the colour wheel
LayoutBuilder(builder: (context, box) {
const perRow = 8;
final size = ((box.maxWidth - (perRow - 1) * 10) / perRow).clamp(28.0, 40.0);
final gap = (box.maxWidth - perRow * size) / (perRow - 1);
return Wrap(
spacing: gap - 0.01, // exactly 8 per row at any width
runSpacing: 12,
children: [
for (final name in AppSettings.bulletColors.keys)
GestureDetector(
onTap: () => s.setBulletColor(name),
child: Tooltip(
message: s.t(name),
child: _ColorDot(s.colorFor(name), size: size,
selected: !s.holidayColorActive && name == s.bulletColorName),
),
),
],
);
}),
Kicker(s.t('Target style'), padding: const EdgeInsets.only(top: 22, bottom: 12)),
if (s.holiday != null) _HolidayCard(holiday: s.holiday!) else const _TargetGrid(styles: TargetStyle.all),
],
);
},
),
);
}
/// Three target designs per row; the chosen one has an accent border.
class _TargetGrid extends StatelessWidget {
final List<TargetStyle> styles;
const _TargetGrid({required this.styles});
// Listens itself: the sheet builds it as a const, so the sheet's own
// rebuild never reaches it and the check would stay on the old target
@override
Widget build(BuildContext context) => ListenableBuilder(
listenable: AppSettings.instance,
builder: (context, _) => _grid(),
);
Widget _grid() {
final s = AppSettings.instance;
final c = Palette.current;
return LayoutBuilder(builder: (context, box) {
final w = (box.maxWidth - 2 * 10) / 3;
return Wrap(
spacing: 10,
runSpacing: 14,
children: [
for (final style in styles)
SizedBox(
width: w,
child: GestureDetector(
onTap: () => s.setTarget(style),
child: Column(
children: [
Container(
height: w * 0.92,
width: w,
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: c.isDark ? c.card2 : c.tile,
borderRadius: BorderRadius.circular(14),
border: Border.all(color: style == s.target ? c.accent : Colors.transparent, width: 2),
),
child: Center(child: TargetThumb(style, size: w * 0.92 - 16)),
),
const SizedBox(height: 6),
Text(
style.name(s.language),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
fontWeight: style == s.target ? FontWeight.w700 : FontWeight.w500,
color: style == s.target ? c.ink : c.muted,
),
),
],
),
),
),
],
);
});
}
}
/// In the Videz sheet during a holiday: its target, locked until the holiday ends.
class _HolidayCard extends StatelessWidget {
final HolidayTheme holiday;
const _HolidayCard({required this.holiday});
@override
Widget build(BuildContext context) {
final s = AppSettings.instance;
final c = Palette.current;
final (_, end) = holiday.window(s.today);
return AppCard(
color: c.isDark ? c.card2 : c.card,
borderColor: holiday.color.withValues(alpha: 0.5),
padding: const EdgeInsets.all(14),
child: Row(
children: [
TargetThumb(TargetStyle.byId(holiday.targetId), size: 72),
const SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.celebration_outlined, size: 18, color: holiday.color),
const SizedBox(width: 6),
Flexible(
child: Text(holiday.name,
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: c.ink)),
),
],
),
const SizedBox(height: 4),
Text(
'${s.t('Holiday target until')} ${DateFormat('d. M.').format(end)}',
style: TextStyle(fontSize: 12.5, height: 1.35, fontWeight: FontWeight.w600, color: c.ink2),
),
Text(s.t('You can still change the hit colour.'),
style: TextStyle(fontSize: 12.5, height: 1.35, color: c.muted)),
],
),
),
const SizedBox(width: 6),
Icon(Icons.lock_outline_rounded, size: 18, color: c.faint),
],
),
);
}
}
class _ColorDot extends StatelessWidget {
final Color color;
final double size;
final bool selected;
const _ColorDot(this.color, {required this.size, this.selected = false});
@override
Widget build(BuildContext context) {
final c = Palette.current;
return Container(
width: size,
height: size,
padding: EdgeInsets.all(selected ? 3 : 0),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: selected ? Border.all(color: c.ink, width: 2) : null,
),
child: Container(
decoration: BoxDecoration(color: color, shape: BoxShape.circle),
child: selected ? Icon(Icons.check_rounded, size: size * 0.5, color: Palette.onColor(color)) : null,
),
);
}
}