Skip to content
Snippets Groups Projects
Commit 5b8567a8 authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Use flutter_custom_toolbox for common features

parent 3b5eb869
No related branches found
No related tags found
1 merge request!24Resolve "Use flutter_custom_toolbox for common features"
Pipeline #6472 passed
This commit is part of merge request !24. Comments created here will be created in the context of that merge request.
Showing
with 22 additions and 315 deletions
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/config/default_global_settings.dart';
import 'package:reversi/utils/tools.dart';
class GlobalSettings {
String skin;
......
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
class AppHeader extends StatelessWidget {
const AppHeader({super.key, required this.text});
final String text;
@override
Widget build(BuildContext context) {
return Text(
tr(text),
textAlign: TextAlign.start,
style: Theme.of(context).textTheme.headlineMedium!.apply(fontWeightDelta: 2),
);
}
}
class AppTitle extends StatelessWidget {
const AppTitle({super.key, required this.text});
final String text;
@override
Widget build(BuildContext context) {
return Text(
tr(text),
textAlign: TextAlign.start,
style: Theme.of(context).textTheme.titleLarge!.apply(fontWeightDelta: 2),
);
}
}
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:reversi/utils/color_extensions.dart';
class StyledButton extends StatelessWidget {
const StyledButton({
super.key,
required this.color,
required this.onPressed,
this.onLongPress,
required this.child,
});
final Color color;
final VoidCallback? onPressed;
final VoidCallback? onLongPress;
final Widget child;
factory StyledButton.text({
Key? key,
required VoidCallback? onPressed,
VoidCallback? onLongPress,
required String caption,
required Color color,
}) {
final Widget captionWidget = AutoSizeText(
caption,
maxLines: 1,
style: TextStyle(
inherit: true,
fontWeight: FontWeight.w900,
color: color.darken(60),
shadows: [
Shadow(
blurRadius: 5.0,
color: color.lighten(60),
offset: const Offset(2, 2),
),
Shadow(
blurRadius: 5.0,
color: color.lighten(60),
offset: const Offset(2, -2),
),
Shadow(
blurRadius: 5.0,
color: color.lighten(60),
offset: const Offset(-2, 2),
),
Shadow(
blurRadius: 5.0,
color: color.lighten(60),
offset: const Offset(-2, -2),
),
],
),
);
return StyledButton(
color: color,
onPressed: onPressed,
onLongPress: onLongPress,
child: captionWidget,
);
}
factory StyledButton.icon({
Key? key,
required VoidCallback? onPressed,
VoidCallback? onLongPress,
required Icon icon,
required Color color,
required double iconSize,
}) {
return StyledButton(
color: color,
onPressed: onPressed,
onLongPress: onLongPress,
child: Icon(
icon.icon,
color: icon.color ?? color.darken(60),
size: iconSize,
shadows: [
Shadow(
blurRadius: 5.0,
color: color.lighten(60),
offset: const Offset(2, 2),
),
Shadow(
blurRadius: 5.0,
color: color.lighten(60),
offset: const Offset(2, -2),
),
Shadow(
blurRadius: 5.0,
color: color.lighten(60),
offset: const Offset(-2, 2),
),
Shadow(
blurRadius: 5.0,
color: color.lighten(60),
offset: const Offset(-2, -2),
),
],
),
);
}
@override
Widget build(BuildContext context) {
const double borderWidth = 4;
final Color borderColor = color.darken(40);
const double borderRadius = 10;
return Container(
margin: const EdgeInsets.all(2),
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
color: color,
border: Border.all(
color: borderColor,
width: borderWidth,
),
borderRadius: BorderRadius.circular(borderRadius),
),
child: CustomPaint(
painter: StyledButtonPainter(
baseColor: color,
),
child: MaterialButton(
onPressed: onPressed,
onLongPress: onLongPress,
padding: const EdgeInsets.all(8),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
minWidth: 40,
child: child,
),
),
);
}
}
class StyledButtonPainter extends CustomPainter {
StyledButtonPainter({
required this.baseColor,
});
final Color baseColor;
@override
void paint(Canvas canvas, Size size) {
final Color lightColor = baseColor.lighten(20);
final Color darkColor = baseColor.darken(20);
final Paint paint = Paint()..style = PaintingStyle.fill;
const double cornerRadius = 6;
Path topPath = Path()
..moveTo(cornerRadius, 0)
..lineTo(size.width - cornerRadius, 0)
..arcToPoint(
Offset(size.width, cornerRadius),
radius: const Radius.circular(cornerRadius),
)
..lineTo(size.width, size.height * .35)
..quadraticBezierTo(
size.width * .4,
size.height * .1,
0,
size.height * .3,
)
..lineTo(0, cornerRadius)
..arcToPoint(
const Offset(cornerRadius, 0),
radius: const Radius.circular(cornerRadius),
);
Path bottomPath = Path()
..moveTo(cornerRadius, size.height)
..lineTo(size.width - cornerRadius, size.height)
..arcToPoint(
Offset(size.width, size.height - cornerRadius),
radius: const Radius.circular(cornerRadius),
clockwise: false,
)
..lineTo(size.width, size.height * .7)
..quadraticBezierTo(
size.width * .6,
size.height * .9,
0,
size.height * .7,
)
..lineTo(0, size.height - cornerRadius)
..arcToPoint(
Offset(cornerRadius, size.height),
radius: const Radius.circular(cornerRadius),
clockwise: false,
);
paint.color = lightColor;
canvas.drawPath(topPath, paint);
paint.color = darkColor;
canvas.drawPath(bottomPath, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/config/default_game_settings.dart';
import 'package:reversi/config/default_global_settings.dart';
......
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/models/settings/settings_game.dart';
import 'package:reversi/models/settings/settings_global.dart';
import 'package:reversi/utils/tools.dart';
class ParameterPainter extends CustomPainter {
const ParameterPainter({
required this.code,
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/models/settings/settings_game.dart';
import 'package:reversi/models/settings/settings_global.dart';
import 'package:reversi/ui/helpers/styled_button.dart';
import 'package:reversi/utils/tools.dart';
class ParameterWidget extends StatelessWidget {
const ParameterWidget({
super.key,
......
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:reversi/ui/helpers/app_titles.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class PageAbout extends StatelessWidget {
const PageAbout({super.key});
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/cubit/game_cubit.dart';
import 'package:reversi/models/game/game.dart';
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/ui/helpers/app_titles.dart';
import 'package:reversi/ui/settings/settings_form.dart';
class PageSettings extends StatelessWidget {
......
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:unicons/unicons.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/ui/settings/theme_card.dart';
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/cubit/theme_cubit.dart';
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/config/menu.dart';
import 'package:reversi/cubit/nav_cubit.dart';
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/cubit/game_cubit.dart';
import 'package:reversi/ui/helpers/styled_button.dart';
class DeleteSavedGameButton extends StatelessWidget {
const DeleteSavedGameButton({super.key});
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/cubit/game_cubit.dart';
import 'package:reversi/ui/helpers/styled_button.dart';
class QuitGameButton extends StatelessWidget {
const QuitGameButton({super.key});
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/cubit/game_cubit.dart';
import 'package:reversi/cubit/settings_game_cubit.dart';
import 'package:reversi/cubit/settings_global_cubit.dart';
import 'package:reversi/ui/helpers/styled_button.dart';
class StartNewGameButton extends StatelessWidget {
const StartNewGameButton({super.key});
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/cubit/game_cubit.dart';
import 'package:reversi/ui/helpers/styled_button.dart';
class ResumeSavedGameButton extends StatelessWidget {
const ResumeSavedGameButton({super.key});
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/cubit/game_cubit.dart';
import 'package:reversi/game/game_engine.dart';
import 'package:reversi/models/game/game.dart';
import 'package:reversi/utils/tools.dart';
class GameBoardWidget extends StatelessWidget {
const GameBoardWidget({super.key});
......
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:reversi/config/menu.dart';
import 'package:reversi/cubit/game_cubit.dart';
import 'package:reversi/cubit/nav_cubit.dart';
import 'package:reversi/models/game/game.dart';
import 'package:reversi/ui/helpers/app_titles.dart';
import 'package:reversi/ui/helpers/styled_button.dart';
class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
const GlobalAppBar({super.key});
......@@ -38,7 +36,7 @@ class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
// go to Settings page
menuActions.add(ElevatedButton(
onPressed: () {
context.read<NavCubit>().goToSettingsPage();
BlocProvider.of<NavCubit>(context).goToSettingsPage();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
......@@ -49,7 +47,7 @@ class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
// go to About page
menuActions.add(ElevatedButton(
onPressed: () {
context.read<NavCubit>().goToAboutPage();
BlocProvider.of<NavCubit>(context).goToAboutPage();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
......@@ -60,7 +58,7 @@ class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
// back to Home page
menuActions.add(ElevatedButton(
onPressed: () {
context.read<NavCubit>().goToGamePage();
BlocProvider.of<NavCubit>(context).goToGamePage();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
......
import 'dart:ui';
extension ColorExtension on Color {
Color darken([int percent = 40]) {
assert(1 <= percent && percent <= 100);
final value = 1 - percent / 100;
return Color.fromARGB(
alpha,
(red * value).round(),
(green * value).round(),
(blue * value).round(),
);
}
Color lighten([int percent = 40]) {
assert(1 <= percent && percent <= 100);
final value = percent / 100;
return Color.fromARGB(
alpha,
(red + ((255 - red) * value)).round(),
(green + ((255 - green) * value)).round(),
(blue + ((255 - blue) * value)).round(),
);
}
Color avg(Color other) {
final red = (this.red + other.red) ~/ 2;
final green = (this.green + other.green) ~/ 2;
final blue = (this.blue + other.blue) ~/ 2;
final alpha = (this.alpha + other.alpha) ~/ 2;
return Color.fromARGB(alpha, red, green, blue);
}
}
import 'package:flutter/foundation.dart';
void printlog(String message) {
if (!kReleaseMode) {
debugPrint(message);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment