fix: color selection compatible with older versions (#5276)

* fix: color selection compatible with older versions

* chore: add a cover color function
This commit is contained in:
Lucas.Xu 2024-05-07 12:13:52 +08:00 committed by GitHub
parent a5b5a1f679
commit aa4fe2ba50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 34 additions and 15 deletions

View File

@ -7,11 +7,11 @@ import 'package:appflowy/plugins/base/emoji/emoji_text.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy/shared/appflowy_network_image.dart';
import 'package:appflowy/shared/flowy_gradient_colors.dart';
import 'package:appflowy/util/string_extension.dart';
import 'package:appflowy/workspace/application/view/view_ext.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flowy_infra/theme_extension.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@ -171,9 +171,12 @@ class _RecentCover extends StatelessWidget {
}
if (type == PageStyleCoverImageType.pureColor) {
return ColoredBox(
color: FlowyTint.fromId(value).color(context),
);
final color = value.coverColor(context);
if (color != null) {
return ColoredBox(
color: color,
);
}
}
if (type == PageStyleCoverImageType.gradientColor) {

View File

@ -10,12 +10,12 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/header/emo
import 'package:appflowy/shared/appflowy_network_image.dart';
import 'package:appflowy/shared/flowy_gradient_colors.dart';
import 'package:appflowy/shared/google_fonts_extension.dart';
import 'package:appflowy/util/string_extension.dart';
import 'package:appflowy/workspace/application/settings/appearance/base_appearance.dart';
import 'package:appflowy/workspace/application/view/view_bloc.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flowy_infra/theme_extension.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flowy_infra_ui/widget/ignore_parent_gesture.dart';
import 'package:flutter/material.dart';
@ -210,7 +210,7 @@ class _DocumentImmersiveCoverState extends State<DocumentImmersiveCover> {
return Container(
height: height,
width: double.infinity,
color: FlowyTint.fromId(cover.value).color(context),
color: cover.value.coverColor(context),
);
}

View File

@ -89,10 +89,14 @@ class _DesktopCoverState extends State<DesktopCover> {
}
if (type == PageStyleCoverImageType.pureColor) {
// try to parse the color from the tint id,
// if it fails, try to parse the color as a hex string
final color = FlowyTint.fromId(cover.value)?.color(context) ??
cover.value.tryToColor();
return Container(
height: height,
width: double.infinity,
color: FlowyTint.fromId(cover.value).color(context),
color: color,
);
}

View File

@ -1,8 +1,9 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:appflowy/shared/patterns/common_patterns.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flowy_infra/theme_extension.dart';
import 'package:flutter/material.dart';
extension StringExtension on String {
static const _specialCharacters = r'\/:*?"<>| ';
@ -35,4 +36,13 @@ extension StringExtension on String {
/// Returns true if the string is a appflowy cloud url.
bool get isAppFlowyCloudUrl => appflowyCloudUrlRegex.hasMatch(this);
/// Returns the color of the string.
///
/// ONLY used for the cover.
Color? coverColor(BuildContext context) {
// try to parse the color from the tint id,
// if it fails, try to parse the color as a hex string
return FlowyTint.fromId(this)?.color(context) ?? tryToColor();
}
}

View File

@ -189,13 +189,15 @@ enum FlowyTint {
}
}
static FlowyTint fromId(String id) {
return FlowyTint.values.firstWhere(
(element) => element.id == id,
orElse: () => FlowyTint.tint1,
);
static FlowyTint? fromId(String id) {
for (final value in FlowyTint.values) {
if (value.id == id) {
return value;
}
}
return null;
}
Color color(BuildContext context) => switch (this) {
FlowyTint.tint1 => AFThemeExtension.of(context).tint1,
FlowyTint.tint2 => AFThemeExtension.of(context).tint2,