Skip to content

Commit

Permalink
Custom label for null color in color picker
Browse files Browse the repository at this point in the history
  • Loading branch information
amantoux committed Dec 7, 2023
1 parent fb8df3f commit 7b58999
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
32 changes: 22 additions & 10 deletions packages/fleather/lib/src/widgets/editor_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,10 @@ Widget defaultToggleStyleButtonBuilder(
);
}

/// Signature of callbacks that return a [Color] picked from a [BuildContext].
typedef PickColor = Future<Color?> Function(BuildContext);
/// Signature of callbacks that return a [Color] picked from a palette built in
/// a [BuildContext] with a [String] specifying the label of the `null` selection
/// option
typedef PickColor = Future<Color?> Function(BuildContext, String);

/// Signature of callbacks the returns a [Widget] from a [BuildContext]
/// and a [Color] (`null` color to use the default color of the text - copes with dark mode).
Expand All @@ -365,12 +367,14 @@ class ColorButton extends StatefulWidget {
{Key? key,
required this.controller,
required this.attributeKey,
required this.nullColorLabel,
required this.builder,
this.pickColor})
: super(key: key);

final FleatherController controller;
final ColorParchmentAttributeBuilder attributeKey;
final String nullColorLabel;
final ColorButtonBuilder builder;
final PickColor? pickColor;

Expand All @@ -393,7 +397,8 @@ class _ColorButtonState extends State<ColorButton> {
});
}

Future<Color?> _defaultPickColor(BuildContext context) async {
Future<Color?> _defaultPickColor(
BuildContext context, String nullColorLabel) async {
// kIsWeb important here as Platform.xxx will cause a crash en web
final isMobile = !kIsWeb && (Platform.isAndroid || Platform.isIOS);
final maxWidth = isMobile ? 200.0 : 100.0;
Expand All @@ -407,7 +412,7 @@ class _ColorButtonState extends State<ColorButton> {
child: Container(
constraints: BoxConstraints(maxWidth: maxWidth),
padding: const EdgeInsets.all(8.0),
child: const _ColorPalette()),
child: _ColorPalette(nullColorLabel)),
);

return Navigator.of(context).push<Color>(
Expand Down Expand Up @@ -471,8 +476,8 @@ class _ColorButtonState extends State<ColorButton> {
hoverElevation: 1,
highlightElevation: 1,
onPressed: () async {
final selectedColor =
await (widget.pickColor ?? _defaultPickColor)(context);
final selectedColor = await (widget.pickColor ?? _defaultPickColor)(
context, widget.nullColorLabel);
final attribute = selectedColor != null
? widget.attributeKey.withColor(selectedColor.value)
: widget.attributeKey.unset;
Expand Down Expand Up @@ -505,7 +510,9 @@ class _ColorPalette extends StatelessWidget {
Colors.black,
];

const _ColorPalette();
const _ColorPalette(this.nullColorLabel);

final String nullColorLabel;

@override
Widget build(BuildContext context) {
Expand All @@ -514,15 +521,18 @@ class _ColorPalette extends StatelessWidget {
alignment: WrapAlignment.start,
runSpacing: 4,
spacing: 4,
children: [...colors].map((e) => _ColorPaletteElement(color: e)).toList(),
children: [...colors]
.map((e) => _ColorPaletteElement(e, nullColorLabel))
.toList(),
);
}
}

class _ColorPaletteElement extends StatelessWidget {
const _ColorPaletteElement({required this.color});
const _ColorPaletteElement(this.color, this.nullColorLabel);

final Color? color;
final String nullColorLabel;

@override
Widget build(BuildContext context) {
Expand All @@ -539,7 +549,7 @@ class _ColorPaletteElement extends StatelessWidget {
onPressed: () => Navigator.pop(context, color),
child: color == null
? Text(
'Automatic',
nullColorLabel,
style: Theme.of(context).textTheme.bodySmall,
)
: null,
Expand Down Expand Up @@ -914,6 +924,7 @@ class FleatherToolbar extends StatefulWidget implements PreferredSizeWidget {
child: ColorButton(
controller: controller,
attributeKey: ParchmentAttribute.foregroundColor,
nullColorLabel: 'Automatic',
builder: textColorBuilder,
),
),
Expand All @@ -922,6 +933,7 @@ class FleatherToolbar extends StatefulWidget implements PreferredSizeWidget {
child: ColorButton(
controller: controller,
attributeKey: ParchmentAttribute.backgroundColor,
nullColorLabel: 'No color',
builder: backgroundColorBuilder,
),
),
Expand Down
4 changes: 3 additions & 1 deletion packages/fleather/test/widgets/editor_toolbar_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:fleather/fleather.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:quill_delta/quill_delta.dart';

Expand Down Expand Up @@ -84,11 +84,13 @@ Widget widget(FleatherController controller, {bool withBasic = false}) {
ColorButton(
controller: controller,
attributeKey: ParchmentAttribute.backgroundColor,
nullColorLabel: 'No color',
builder: backgroundColorBuilder,
),
ColorButton(
controller: controller,
attributeKey: ParchmentAttribute.foregroundColor,
nullColorLabel: 'Automatic',
builder: textColorBuilder,
),
IndentationButton(controller: controller),
Expand Down

0 comments on commit 7b58999

Please sign in to comment.