Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Dec 16, 2024
1 parent 873ee77 commit 4e171f8
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 92 deletions.
8 changes: 2 additions & 6 deletions lib/src/model/settings/board_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,8 @@ class BoardPreferences extends _$BoardPreferences with PreferencesStorage<BoardP
return save(state.copyWith(shapeColor: shapeColor));
}

Future<void> setBrightness(double brightness) {
return save(state.copyWith(brightness: brightness));
}

Future<void> setHue(double hue) {
return save(state.copyWith(hue: hue));
Future<void> adjustColors({double? brightness, double? hue}) {
return save(state.copyWith(brightness: brightness ?? state.brightness, hue: hue ?? state.hue));
}
}

Expand Down
11 changes: 7 additions & 4 deletions lib/src/view/settings/settings_tab_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,13 @@ class _Body extends ConsumerWidget {
}
},
),
SettingsListTile(
icon: const Icon(Icons.palette_outlined),
settingsLabel: Text(context.l10n.mobileTheme),
settingsValue: '${boardPrefs.boardTheme.label} / ${boardPrefs.pieceSet.label}',
PlatformListTile(
leading: const Icon(Icons.palette_outlined),
title: Text(context.l10n.mobileTheme),
trailing:
Theme.of(context).platform == TargetPlatform.iOS
? const CupertinoListTileChevron()
: null,
onTap: () {
pushPlatformRoute(context, title: 'Theme', builder: (context) => const ThemeScreen());
},
Expand Down
76 changes: 41 additions & 35 deletions lib/src/view/settings/theme_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:lichess_mobile/src/view/settings/board_theme_screen.dart';
import 'package:lichess_mobile/src/view/settings/piece_set_screen.dart';
import 'package:lichess_mobile/src/widgets/adaptive_choice_picker.dart';
import 'package:lichess_mobile/src/widgets/buttons.dart';
import 'package:lichess_mobile/src/widgets/change_colors.dart';
import 'package:lichess_mobile/src/widgets/list.dart';
import 'package:lichess_mobile/src/widgets/platform_alert_dialog.dart';
import 'package:lichess_mobile/src/widgets/platform_scaffold.dart';
Expand Down Expand Up @@ -75,25 +76,29 @@ class _BodyState extends ConsumerState<_Body> {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: horizontalPadding, vertical: 16),
child: Center(
child: Chessboard.fixed(
size: boardSize,
orientation: Side.white,
lastMove: const NormalMove(from: Square.e2, to: Square.e4),
fen: 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
shapes:
<Shape>{
Circle(color: boardPrefs.shapeColor.color, orig: Square.fromName('b8')),
Arrow(
color: boardPrefs.shapeColor.color,
orig: Square.fromName('b8'),
dest: Square.fromName('c6'),
),
}.lock,
settings: boardPrefs.toBoardSettings().copyWith(
brightness: brightness,
hue: hue,
borderRadius: const BorderRadius.all(Radius.circular(4.0)),
boxShadow: boardShadows,
child: ChangeColors(
brightness: brightness,
hue: hue,
child: Chessboard.fixed(
size: boardSize,
orientation: Side.white,
lastMove: const NormalMove(from: Square.e2, to: Square.e4),
fen: 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
shapes:
<Shape>{
Circle(color: boardPrefs.shapeColor.color, orig: Square.fromName('b8')),
Arrow(
color: boardPrefs.shapeColor.color,
orig: Square.fromName('b8'),
dest: Square.fromName('c6'),
),
}.lock,
settings: boardPrefs.toBoardSettings().copyWith(
brightness: 0.0,
hue: 0.0,
borderRadius: const BorderRadius.all(Radius.circular(4.0)),
boxShadow: boardShadows,
),
),
),
),
Expand All @@ -103,18 +108,6 @@ class _BodyState extends ConsumerState<_Body> {
ListSection(
hasLeading: true,
children: [
SettingsListTile(
icon: const Icon(LichessIcons.chess_board),
settingsLabel: Text(context.l10n.board),
settingsValue: boardPrefs.boardTheme.label,
onTap: () {
pushPlatformRoute(
context,
title: context.l10n.board,
builder: (context) => const BoardThemeScreen(),
);
},
),
PlatformListTile(
leading: const Icon(Icons.brightness_6),
title: Slider.adaptive(
Expand All @@ -127,7 +120,7 @@ class _BodyState extends ConsumerState<_Body> {
});
},
onChangeEnd: (value) {
ref.read(boardPreferencesProvider.notifier).setBrightness(brightness);
ref.read(boardPreferencesProvider.notifier).adjustColors(brightness: brightness);
},
),
),
Expand All @@ -143,7 +136,7 @@ class _BodyState extends ConsumerState<_Body> {
});
},
onChangeEnd: (value) {
ref.read(boardPreferencesProvider.notifier).setHue(hue);
ref.read(boardPreferencesProvider.notifier).adjustColors(hue: hue);
},
),
),
Expand All @@ -162,8 +155,9 @@ class _BodyState extends ConsumerState<_Body> {
brightness = 0.0;
hue = 0.0;
});
ref.read(boardPreferencesProvider.notifier).setBrightness(0.0);
ref.read(boardPreferencesProvider.notifier).setHue(0.0);
ref
.read(boardPreferencesProvider.notifier)
.adjustColors(brightness: 0.0, hue: 0.0);
},
),
),
Expand All @@ -172,6 +166,18 @@ class _BodyState extends ConsumerState<_Body> {
ListSection(
hasLeading: true,
children: [
SettingsListTile(
icon: const Icon(LichessIcons.chess_board),
settingsLabel: Text(context.l10n.board),
settingsValue: boardPrefs.boardTheme.label,
onTap: () {
pushPlatformRoute(
context,
title: context.l10n.board,
builder: (context) => const BoardThemeScreen(),
);
},
),
SettingsListTile(
icon: const Icon(LichessIcons.chess_pawn),
settingsLabel: Text(context.l10n.pieceSet),
Expand Down
101 changes: 54 additions & 47 deletions lib/src/widgets/board_carousel_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/widgets/buttons.dart';
import 'package:lichess_mobile/src/widgets/change_colors.dart';
import 'package:lichess_mobile/src/widgets/platform.dart';

const _kBoardCarouselItemMargin = EdgeInsets.symmetric(vertical: 8.0, horizontal: 6.0);
Expand Down Expand Up @@ -51,58 +52,64 @@ class BoardCarouselItem extends ConsumerWidget {
return LayoutBuilder(
builder: (context, constraints) {
final boardSize = constraints.biggest.shortestSide - _kBoardCarouselItemMargin.horizontal;
final card = PlatformCard(
color: backgroundColor,
margin:
Theme.of(context).platform == TargetPlatform.iOS
? EdgeInsets.zero
: _kBoardCarouselItemMargin,
child: AdaptiveInkWell(
splashColor: splashColor,
borderRadius: BorderRadius.circular(10),
onTap: onTap,
child: Stack(
children: [
ShaderMask(
blendMode: BlendMode.dstOut,
shaderCallback: (bounds) {
return LinearGradient(
begin: Alignment.center,
end: Alignment.bottomCenter,
colors: [
backgroundColor.withValues(alpha: 0.25),
backgroundColor.withValues(alpha: 1.0),
],
stops: const [0.3, 1.00],
tileMode: TileMode.clamp,
).createShader(bounds);
},
child: SizedBox(
height: boardSize,
child: Chessboard.fixed(
size: boardSize,
fen: fen,
orientation: orientation,
lastMove: lastMove,
settings: boardPrefs.toBoardSettings().copyWith(
enableCoordinates: false,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0),
final card = ChangeColors(
brightness: boardPrefs.brightness,
hue: boardPrefs.hue,
child: PlatformCard(
color: backgroundColor,
margin:
Theme.of(context).platform == TargetPlatform.iOS
? EdgeInsets.zero
: _kBoardCarouselItemMargin,
child: AdaptiveInkWell(
splashColor: splashColor,
borderRadius: BorderRadius.circular(10),
onTap: onTap,
child: Stack(
children: [
ShaderMask(
blendMode: BlendMode.dstOut,
shaderCallback: (bounds) {
return LinearGradient(
begin: Alignment.center,
end: Alignment.bottomCenter,
colors: [
backgroundColor.withValues(alpha: 0.25),
backgroundColor.withValues(alpha: 1.0),
],
stops: const [0.3, 1.00],
tileMode: TileMode.clamp,
).createShader(bounds);
},
child: SizedBox(
height: boardSize,
child: Chessboard.fixed(
size: boardSize,
fen: fen,
orientation: orientation,
lastMove: lastMove,
settings: boardPrefs.toBoardSettings().copyWith(
brightness: 0.0,
hue: 0.0,
enableCoordinates: false,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0),
),
),
),
),
),
),
Positioned(
left: 0,
bottom: 8,
child: DefaultTextStyle.merge(
style: const TextStyle(color: Colors.white),
child: description,
Positioned(
left: 0,
bottom: 8,
child: DefaultTextStyle.merge(
style: const TextStyle(color: Colors.white),
child: description,
),
),
),
],
],
),
),
),
);
Expand Down
81 changes: 81 additions & 0 deletions lib/src/widgets/change_colors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'dart:math';

import 'package:flutter/material.dart';

// Based upon: https://stackoverflow.com/questions/64639589/how-to-adjust-hue-saturation-and-brightness-of-an-image-in-flutter
// from BananaNeil: https://stackoverflow.com/users/937841/banananeil.
// This is, in turn, based upon: https://stackoverflow.com/a/7917978/937841

/// Use the [ChangeColors] widget to change the brightness and hue of any widget, including images.
///
/// Example:
///
/// ```
/// ChangeColors(
/// hue: 0.55,
/// brightness: 0.2,
/// child: Image.asset('myImage.png'),
/// );
/// ```
///
/// To achieve a greyscale effect, you may also use the
/// [ChangeColors.greyscale] constructor.
///
class ChangeColors extends StatelessWidget {
const ChangeColors({this.brightness = 0.0, this.hue = 0.0, required this.child, super.key});

/// Negative value will make it darker (-1 is darkest).
/// Positive value will make it lighter (1 is the maximum, but you can go above it).
/// Note: 0.0 is unchanged.
final double brightness;

/// From -1.0 to 1.0 (Note: 1.0 wraps into -1.0, such as 1.2 is the same as -0.8).
/// Note: 0.0 is unchanged. Adding or subtracting multiples of 2.0 also keeps it unchanged.
final double hue;

final Widget child;

@override
Widget build(BuildContext context) {
return ColorFiltered(
colorFilter: ColorFilter.matrix(_adjustMatrix(hue: hue * pi, brightness: brightness)),
child: child,
);
}
}

List<double> _adjustMatrix({required double hue, required double brightness}) {
if (hue == 0 && brightness == 0) {
// dart format off
return [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
];
// dart format on
}
final brightnessValue = brightness <= 0 ? brightness * 255 : brightness * 100;
return List<double>.from(<double>[
0.213 + cos(hue) * 0.787 + sin(hue) * -0.213,
0.715 + cos(hue) * -0.715 + sin(hue) * -0.715,
0.072 + cos(hue) * -0.072 + sin(hue) * 0.928,
0,
brightnessValue,
0.213 + cos(hue) * -0.213 + sin(hue) * 0.143,
0.715 + cos(hue) * 0.285 + sin(hue) * 0.140,
0.072 + cos(hue) * -0.072 + sin(hue) * -0.283,
0,
brightnessValue,
0.213 + cos(hue) * -0.213 + sin(hue) * -0.787,
0.715 + cos(hue) * -0.715 + sin(hue) * 0.715,
0.072 + cos(hue) * 0.928 + sin(hue) * 0.072,
0,
brightnessValue,
0,
0,
0,
1,
0,
]).map((i) => i).toList();
}

0 comments on commit 4e171f8

Please sign in to comment.