1
0
forked from firka/firka

feat(theme): add grade color customization screen

This commit is contained in:
2026-09-11 12:12:31 +02:00
parent fd909ade1d
commit b933e1e7b8
7 changed files with 691 additions and 2 deletions

View File

@@ -1,3 +1,6 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:firka/l10n/app_localizations.dart';
@@ -284,6 +287,15 @@ extension LessonExtension on List<Lesson> {
}
}
extension RandomColorExtension on Random {
HSVColor nextVividHSVColor() {
final hue = nextDouble() * 360;
final saturation = 0.55 + nextDouble() * 0.45;
final value = 0.7 + nextDouble() * 0.3;
return HSVColor.fromAHSV(1.0, hue, saturation, value);
}
}
extension StringExtension on String {
bool isNumeric() {
final regex = RegExp(r'^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$');

View File

@@ -194,4 +194,21 @@ abstract class SettingsSchema {
@BoolSetting(id: 1049, defaultValue: false)
bool get progressNotificationEnabled;
// Custom grade colors stored as 0xAARRGGBB hex strings.
// Defaults match the firka grade theme.
@StringSetting(id: 1050, defaultValue: "0xFF22CCAD")
String get customGradeColor5;
@StringSetting(id: 1051, defaultValue: "0xFF92EA3B")
String get customGradeColor4;
@StringSetting(id: 1052, defaultValue: "0xFFF9CF00")
String get customGradeColor3;
@StringSetting(id: 1053, defaultValue: "0xFFFFA046")
String get customGradeColor2;
@StringSetting(id: 1054, defaultValue: "0xFFFF54A1")
String get customGradeColor1;
}

View File

@@ -47,9 +47,14 @@ class SettingsRegistry {
static const notificationDeliveryMethod = EnumSetting(id: 1047, defaultValue: NotificationDeliveryMethod.auto, values: NotificationDeliveryMethod.values);
static const lessonCacheKeyMigrationDone = BoolSetting(id: 1048, defaultValue: false);
static const progressNotificationEnabled = BoolSetting(id: 1049, defaultValue: false);
static const customGradeColor5 = StringSetting(id: 1050, defaultValue: "0xFF22CCAD");
static const customGradeColor4 = StringSetting(id: 1051, defaultValue: "0xFF92EA3B");
static const customGradeColor3 = StringSetting(id: 1052, defaultValue: "0xFFF9CF00");
static const customGradeColor2 = StringSetting(id: 1053, defaultValue: "0xFFFFA046");
static const customGradeColor1 = StringSetting(id: 1054, defaultValue: "0xFFFF54A1");
static const all = <Setting>[
bellDelay, rounding1, rounding2, rounding3, rounding4, classAvgOnGraph, leftHandedMode, language, appIcon, childProtection, betaWarning, ttToastLessonNo, ttToastTestsAndHw, ttToastBreaks, statsForNerds, developerOptsEnabled, themeBrightness, ttToastSubstitution, ttToastABTimetable, wearOsSupport, titleFont, titleWeight, titleCapitalization, selectedThemeId, selectedCoreThemeId, selectedGradeThemeId, mockBackendEnabled, mockBackendUrl, lastSeen, surpriseGrades, seasonalAppIcons, uwuMode, notifyAll, notifyGrades, notifyHomeworkTests, notifyAbsences, notifyLessons, notifyMessages, notifyMutedSubjects, notifyWakeupInterval, fcmDebugNotifyOnMessage, notificationDeliveryMethod, lessonCacheKeyMigrationDone, progressNotificationEnabled,
bellDelay, rounding1, rounding2, rounding3, rounding4, classAvgOnGraph, leftHandedMode, language, appIcon, childProtection, betaWarning, ttToastLessonNo, ttToastTestsAndHw, ttToastBreaks, statsForNerds, developerOptsEnabled, themeBrightness, ttToastSubstitution, ttToastABTimetable, wearOsSupport, titleFont, titleWeight, titleCapitalization, selectedThemeId, selectedCoreThemeId, selectedGradeThemeId, mockBackendEnabled, mockBackendUrl, lastSeen, surpriseGrades, seasonalAppIcons, uwuMode, notifyAll, notifyGrades, notifyHomeworkTests, notifyAbsences, notifyLessons, notifyMessages, notifyMutedSubjects, notifyWakeupInterval, fcmDebugNotifyOnMessage, notificationDeliveryMethod, lessonCacheKeyMigrationDone, progressNotificationEnabled, customGradeColor5, customGradeColor4, customGradeColor3, customGradeColor2, customGradeColor1,
];
}
@@ -98,4 +103,9 @@ extension SettingsAccessors on SettingsRepository {
SilentlySettable<NotificationDeliveryMethod> get notificationDeliveryMethod => SilentlySettable(this, SettingsRegistry.notificationDeliveryMethod);
SilentlySettable<bool> get lessonCacheKeyMigrationDone => SilentlySettable(this, SettingsRegistry.lessonCacheKeyMigrationDone);
SilentlySettable<bool> get progressNotificationEnabled => SilentlySettable(this, SettingsRegistry.progressNotificationEnabled);
SilentlySettable<String> get customGradeColor5 => SilentlySettable(this, SettingsRegistry.customGradeColor5);
SilentlySettable<String> get customGradeColor4 => SilentlySettable(this, SettingsRegistry.customGradeColor4);
SilentlySettable<String> get customGradeColor3 => SilentlySettable(this, SettingsRegistry.customGradeColor3);
SilentlySettable<String> get customGradeColor2 => SilentlySettable(this, SettingsRegistry.customGradeColor2);
SilentlySettable<String> get customGradeColor1 => SilentlySettable(this, SettingsRegistry.customGradeColor1);
}

View File

@@ -20,6 +20,7 @@ import 'package:firka/ui/phone/screens/login/login_screen.dart';
import 'package:firka/ui/phone/screens/message/message_screen.dart';
import 'package:firka/ui/phone/screens/home/home_screen.dart';
import 'package:firka/ui/phone/screens/settings/settings_screen.dart';
import 'package:firka/ui/phone/screens/grade_colors/grade_colors_screen.dart';
import 'package:firka/ui/phone/screens/themes/theme_screen.dart';
import 'package:firka/ui/phone/screens/themes/themes_screen.dart';
import 'package:firka/ui/phone/screens/themes/user_theme.dart';
@@ -115,6 +116,13 @@ GoRouter createAppRouter() {
);
},
),
GoRoute(
path: '/grade-colors',
builder: (context, state) => DefaultAssetBundle(
bundle: FirkaBundle(),
child: GradeColorsScreen(initData, key: state.pageKey),
),
),
GoRoute(
path: '/message',
builder: (context, state) {

View File

@@ -0,0 +1,87 @@
import "package:flutter/material.dart";
import "package:firka/ui/theme/style.dart";
import "package:firka_common/ui/components/firka_card.dart";
class ColorSlider extends StatelessWidget {
final double value;
final LinearGradient trackGradient;
final Color thumbColor;
final ValueChanged<double> onChanged;
const ColorSlider({
required this.value,
required this.trackGradient,
required this.thumbColor,
required this.onChanged,
super.key,
});
static const _trackHeight = 52.0;
static const _thumbSize = 28.0;
static const _thumbRadius = _thumbSize / 2;
static const _edgePad = 16.0;
void _setFromLocalX(double localX, double width) {
final start = _edgePad + _thumbRadius;
final end = width - _edgePad - _thumbRadius;
if (end <= start) return;
final t = ((localX - start) / (end - start)).clamp(0.0, 1.0);
onChanged(t);
}
@override
Widget build(BuildContext context) {
return FirkaCard.single(
width: double.infinity,
height: _trackHeight,
padding: EdgeInsets.zero,
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: LayoutBuilder(
builder: (context, constraints) {
final width = constraints.maxWidth;
final start = _edgePad + _thumbRadius;
final end = width - _edgePad - _thumbRadius;
final thumbLeft = start + value * (end - start) - _thumbRadius;
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTapDown: (d) => _setFromLocalX(d.localPosition.dx, width),
onHorizontalDragUpdate: (d) =>
_setFromLocalX(d.localPosition.dx, width),
child: Stack(
fit: StackFit.expand,
children: [
Container(decoration: BoxDecoration(gradient: trackGradient)),
Positioned(
left: thumbLeft,
top: (_trackHeight - _thumbSize) / 2,
child: Container(
width: _thumbSize,
height: _thumbSize,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: thumbColor,
border: Border.all(
color: appStyle.colors.buttonSecondaryFill,
width: 3,
),
boxShadow: [
BoxShadow(
color: appStyle.colors.shadowColor,
offset: const Offset(0, 1),
blurRadius: appStyle.isLight ? 3 : 0,
),
],
),
),
),
],
),
);
},
),
),
);
}
}

View File

@@ -0,0 +1,539 @@
import "dart:math" as math;
import "package:firka/app/app_state.dart";
import "package:firka/core/bloc/theme_cubit.dart";
import "package:firka/core/extensions.dart";
import "package:firka/core/settings/setting.dart";
import "package:firka/core/settings/settings_repository.dart";
import "package:firka/core/settings/settings_schema.dart";
import "package:firka/ui/components/color_slider.dart";
import "package:firka/ui/shared/firka_icon.dart";
import "package:firka/ui/theme/style.dart";
import "package:firka_common/ui/components/filled_circle.dart";
import "package:firka_common/ui/components/firka_card.dart";
import "package:firka_common/ui/theme/style.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:flutter_bloc/flutter_bloc.dart";
import "package:go_router/go_router.dart";
import "package:majesticons_flutter/majesticons_flutter.dart";
class GradeColorsScreen extends StatefulWidget {
final AppInitialization data;
const GradeColorsScreen(this.data, {super.key});
@override
State<GradeColorsScreen> createState() => _GradeColorsScreenState();
}
StringSetting _settingFor(int grade) {
switch (grade) {
case 5:
return SettingsRegistry.customGradeColor5;
case 4:
return SettingsRegistry.customGradeColor4;
case 3:
return SettingsRegistry.customGradeColor3;
case 2:
return SettingsRegistry.customGradeColor2;
default:
return SettingsRegistry.customGradeColor1;
}
}
Color _defaultFor(int grade) {
switch (grade) {
case 5:
return const Color(0xFF22CCAD);
case 4:
return const Color(0xFF92EA3B);
case 3:
return const Color(0xFFF9CF00);
case 2:
return const Color(0xFFFFA046);
default:
return const Color(0xFFFF54A1);
}
}
Color _parseColor(String hex) {
final value = int.tryParse(hex) ?? 0xFFFFFFFF;
return Color(value);
}
String _encodeColor(Color color) =>
"0x${color.toARGB32().toRadixString(16).toUpperCase().padLeft(8, '0')}";
class _GradeColorsScreenState extends State<GradeColorsScreen>
with SingleTickerProviderStateMixin {
late int _selectedGrade;
late HSVColor _hsv;
final _hexController = TextEditingController();
bool _hexFocused = false;
late final AnimationController _ringController;
Tween<double> _ringTween = Tween<double>(begin: 0, end: 0);
double _ringRotation = 0;
SettingsRepository get _settings => widget.data.settings;
@override
void initState() {
super.initState();
_selectedGrade = 5;
_hsv = HSVColor.fromColor(_currentColor(_selectedGrade));
_syncHexField();
_ringController = AnimationController(vsync: this)
..addListener(() {
setState(() {
_ringRotation = _ringTween.transform(
Curves.easeInOutCubic.transform(_ringController.value),
);
});
});
}
@override
void dispose() {
_hexController.dispose();
_ringController.dispose();
super.dispose();
}
Color _currentColor(int grade) {
final raw = _settings.get(_settingFor(grade));
return _parseColor(raw);
}
void _syncHexField() {
if (!_hexFocused) {
final hex = _encodeColor(_hsv.toColor()).substring(4);
_hexController.text = hex.toUpperCase();
}
}
Future<void> _applyHsv(HSVColor hsv) async {
setState(() => _hsv = hsv);
_syncHexField();
await _settings.set(
_settingFor(_selectedGrade),
_encodeColor(hsv.toColor()),
);
if (!mounted) return;
context.read<ThemeCubit>().refresh();
}
Future<void> _selectGrade(int grade) async {
final hubIndex = _roleOrder.indexOf("hub");
final targetMod = (hubIndex - _ring.indexOf(grade)) % _ring.length;
var delta = (targetMod - _ringRotation) % _ring.length;
if (delta > _ring.length / 2) delta -= _ring.length;
final target = _ringRotation + delta;
_ringTween = Tween<double>(begin: _ringRotation, end: target);
_ringController
..duration = Duration(
milliseconds: (150 * delta.abs()).clamp(180, 420).round(),
)
..forward(from: 0);
setState(() {
_selectedGrade = grade;
_hsv = HSVColor.fromColor(_currentColor(grade));
});
_syncHexField();
}
Future<void> _reset() async {
final defaultColor = _defaultFor(_selectedGrade);
await _applyHsv(HSVColor.fromColor(defaultColor));
}
Future<void> _randomize() async {
await _applyHsv(math.Random().nextVividHSVColor());
}
void _onHexSubmit(String value) {
final cleaned = value.replaceAll("#", "").trim();
if (cleaned.length != 6) return;
final parsed = int.tryParse("0xFF$cleaned");
if (parsed == null) return;
_applyHsv(HSVColor.fromColor(Color(parsed)));
}
@override
Widget build(BuildContext context) {
return BlocProvider<ThemeCubit>.value(
value: widget.data.themeCubit,
child: BlocBuilder<ThemeCubit, ThemeState>(
builder: (context, _) {
return Scaffold(
backgroundColor: appStyle.colors.background,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_header(context),
const SizedBox(height: 24),
Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_gradeBubbles(),
const SizedBox(height: 28),
_hueSlider(),
const SizedBox(height: 8),
_saturationSlider(),
const SizedBox(height: 8),
_valueSlider(),
const SizedBox(height: 20),
_hexRow(),
const SizedBox(height: 12),
_gradeTabBar(),
const SizedBox(height: 24),
],
),
),
),
],
),
),
),
);
},
),
);
}
Widget _header(BuildContext context) {
final l10n = widget.data.l10n;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Transform.translate(
offset: const Offset(-4, 0),
child: GestureDetector(
onTap: () => context.pop(),
child: FirkaIconWidget(
FirkaIconType.majesticons,
Majesticon.chevronLeftLine,
color: appStyle.colors.textSecondary,
),
),
),
Text(
l10n.s_customization,
style: appStyle.fonts.B_16R.apply(
color: appStyle.colors.textSecondary,
),
),
],
),
const SizedBox(height: 4),
Text(
headingText(l10n.s_c_grade_colors_header),
style: appStyle.fonts.H_H1.apply(color: appStyle.colors.textPrimary),
),
],
);
}
static const _roleSlots = <String, (double dx, double dy, double sizeFactor)>{
"topLeft": (0.39, 0.22, 0.82),
"topRight": (0.625, 0.22, 0.82),
"midRight": (0.80, 0.46, 0.76),
"hub": (0.505, 0.66, 1.0),
"midLeft": (0.22, 0.46, 0.76),
};
static const _roleOrder = [
"topLeft",
"topRight",
"midRight",
"hub",
"midLeft",
];
static const _ring = [2, 3, 4, 5, 1];
Widget _gradeBubbles() {
const maxCenterSize = 88.0;
const clusterHeight = 190.0;
return LayoutBuilder(
builder: (context, constraints) {
final width = constraints.maxWidth;
final centerSize = math.min(maxCenterSize, width * 0.24);
return SizedBox(
height: clusterHeight,
width: width,
child: Stack(
children: [
for (final grade in _ring)
Builder(
builder: (context) {
final u = _ring.indexOf(grade) + _ringRotation;
final idx0 = u.floor();
final frac = u - idx0;
final slotA = _roleSlots[_roleOrder[idx0 % _ring.length]]!;
final slotB =
_roleSlots[_roleOrder[(idx0 + 1) % _ring.length]]!;
final dx = slotA.$1 + (slotB.$1 - slotA.$1) * frac;
final dy = slotA.$2 + (slotB.$2 - slotA.$2) * frac;
final sizeFactor = slotA.$3 + (slotB.$3 - slotA.$3) * frac;
final size = centerSize * sizeFactor;
return Positioned(
left: dx * width - size / 2,
top: dy * clusterHeight - size / 2,
child: _gradeBubble(grade, size: size),
);
},
),
],
),
);
},
);
}
Widget _gradeBubble(int grade, {required double size}) {
final isSelected = grade == _selectedGrade;
final color = isSelected ? _hsv.toColor() : _currentColor(grade);
final pastel = Color.alphaBlend(color.withAlpha(38), appStyle.colors.card);
const ringWidth = 4.0;
final circle = FilledCircle(
diameter: isSelected ? size - ringWidth * 2 : size,
color: pastel,
child: Text(
"$grade",
style: appStyle.fonts.H_H1.copyWith(color: color, fontSize: size * 0.4),
),
);
return GestureDetector(
onTap: isSelected ? null : () => _selectGrade(grade),
child: Container(
width: size,
height: size,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: isSelected
? Border.all(
color: appStyle.colors.buttonSecondaryFill,
width: ringWidth,
)
: null,
boxShadow: isSelected && appStyle.isLight
? [
BoxShadow(
color: appStyle.colors.shadowColor,
blurRadius: 8,
offset: const Offset(0, 3),
),
]
: null,
),
child: Center(child: circle),
),
);
}
Widget _hueSlider() {
return ColorSlider(
value: _hsv.hue / 360,
trackGradient: const LinearGradient(
colors: [
Color(0xFFFF0000),
Color(0xFFFFFF00),
Color(0xFF00FF00),
Color(0xFF00FFFF),
Color(0xFF0000FF),
Color(0xFFFF00FF),
Color(0xFFFF0000),
],
),
thumbColor: _hsv.toColor(),
onChanged: (t) => _applyHsv(_hsv.withHue(t * 360)),
);
}
Widget _saturationSlider() {
final fullSat = _hsv.withSaturation(1).withValue(1).toColor();
return ColorSlider(
value: _hsv.saturation,
trackGradient: LinearGradient(colors: [Colors.white, fullSat]),
thumbColor: _hsv.withValue(1).toColor(),
onChanged: (t) => _applyHsv(_hsv.withSaturation(t)),
);
}
Widget _valueSlider() {
final fullV = _hsv.withValue(1).toColor();
return ColorSlider(
value: _hsv.value,
trackGradient: LinearGradient(colors: [Colors.black, fullV]),
thumbColor: _hsv.toColor(),
onChanged: (t) => _applyHsv(_hsv.withValue(t)),
);
}
Widget _hexRow() {
return FirkaCard(
left: [
_iconCircle(icon: Majesticon.reloadLine, onTap: _reset, faded: true),
const SizedBox(width: 12),
Container(
width: 28,
height: 28,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _hsv.toColor(),
boxShadow: [
BoxShadow(
color: appStyle.colors.shadowColor,
blurRadius: appStyle.isLight ? 3 : 0,
offset: const Offset(0, 1),
),
],
),
),
const SizedBox(width: 10),
Text(
"HEX",
style: appStyle.fonts.B_14R.apply(
color: appStyle.colors.textSecondary,
),
),
const SizedBox(width: 8),
SizedBox(
width: 96,
child: Focus(
onFocusChange: (focused) => setState(() => _hexFocused = focused),
child: TextField(
controller: _hexController,
keyboardType: TextInputType.text,
textCapitalization: TextCapitalization.characters,
maxLength: 6,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'[0-9a-fA-F]')),
],
onSubmitted: _onHexSubmit,
style: appStyle.fonts.B_16SB.apply(
color: appStyle.colors.textPrimary,
),
decoration: InputDecoration(
counterText: "",
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.zero,
hintText: "FFFFFF",
hintStyle: appStyle.fonts.B_16SB.apply(
color: appStyle.colors.textTertiary,
),
),
),
),
),
],
right: [
_iconCircle(
icon: Majesticon.repeatCircleLine,
onTap: _randomize,
accent: true,
),
const SizedBox(width: 4),
],
);
}
Widget _iconCircle({
required Object icon,
required VoidCallback onTap,
bool faded = false,
bool accent = false,
}) {
final bgColor = accent
? appStyle.colors.accent
: appStyle.colors.buttonSecondaryFill;
final iconColor = accent
? appStyle.colors.textPrimary
: appStyle.colors.textSecondary;
return GestureDetector(
onTap: onTap,
child: Container(
width: 36,
height: 36,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: bgColor,
boxShadow: [
BoxShadow(
color: appStyle.colors.shadowColor,
blurRadius: appStyle.isLight ? 2 : 0,
offset: const Offset(0, 1),
),
],
),
child: Center(
child: FirkaIconWidget(
FirkaIconType.majesticons,
icon,
size: 18,
color: iconColor,
),
),
),
);
}
Widget _gradeTabBar() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (final grade in [1, 2, 3, 4, 5]) ...[
if (grade != 1) const SizedBox(width: 8),
_gradeTab(grade),
],
],
);
}
Widget _gradeTab(int grade) {
final isSelected = grade == _selectedGrade;
return GestureDetector(
onTap: isSelected ? null : () => _selectGrade(grade),
child: AnimatedContainer(
duration: const Duration(milliseconds: 180),
curve: Curves.easeOut,
width: 32,
height: 32,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: isSelected ? appStyle.colors.accent : appStyle.colors.card,
),
child: Center(
child: Text(
"$grade",
style: appStyle.fonts.B_14SB.apply(
color: isSelected
? appStyle.colors.textPrimary
: appStyle.colors.textSecondary,
),
),
),
),
);
}
}

View File

@@ -203,6 +203,22 @@ class _SettingsPersonalizationViewState
);
}
Widget _customizeButton({required String route}) {
return GestureDetector(
onTap: () => context.push(route),
child: FirkaCard(
left: [
Text(
_l10n.s_c_customize,
style: appStyle.fonts.B_16SB.apply(
color: appStyle.colors.textPrimary,
),
),
],
),
);
}
Widget _checkMark() {
return SizedBox(
width: 16,
@@ -620,7 +636,7 @@ class _SettingsPersonalizationViewState
_themeOption(_l10n.s_c_theme_dark, ThemeBrightness.dark),
_sectionLabel(_l10n.s_c_grade_colors_header),
_gradeThemePager(),
_stubCustomizeButton(),
_customizeButton(route: "/grade-colors"),
_sectionLabel(_l10n.s_c_title_style_header),
_fontPicker(selectedFont),
_weightSlider(),