From ce8773198d90f379653d5bf2931e23b58a40f763 Mon Sep 17 00:00:00 2001 From: singerdmx Date: Wed, 23 Dec 2020 00:41:47 -0800 Subject: [PATCH] Support color picker --- app/lib/pages/home_page.dart | 1 - app/pubspec.lock | 7 ++++ lib/widgets/toolbar.dart | 63 +++++++++++++++++++++++++++++++++--- pubspec.lock | 7 ++++ pubspec.yaml | 1 + 5 files changed, 74 insertions(+), 5 deletions(-) diff --git a/app/lib/pages/home_page.dart b/app/lib/pages/home_page.dart index 8e4261d2..60383e94 100644 --- a/app/lib/pages/home_page.dart +++ b/app/lib/pages/home_page.dart @@ -6,7 +6,6 @@ import 'package:flutter_quill/models/documents/document.dart'; import 'package:flutter_quill/widgets/controller.dart'; import 'package:flutter_quill/widgets/editor.dart'; import 'package:flutter_quill/widgets/toolbar.dart'; -import 'package:url_launcher/url_launcher.dart'; class HomePage extends StatefulWidget { @override diff --git a/app/pubspec.lock b/app/pubspec.lock index 74769e2c..71bc7f7d 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -62,6 +62,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_colorpicker: + dependency: transitive + description: + name: flutter_colorpicker + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.4" flutter_quill: dependency: "direct main" description: diff --git a/lib/widgets/toolbar.dart b/lib/widgets/toolbar.dart index b15f4323..8f91be17 100644 --- a/lib/widgets/toolbar.dart +++ b/lib/widgets/toolbar.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_colorpicker/flutter_colorpicker.dart'; import 'package:flutter_quill/models/documents/attribute.dart'; import 'package:flutter_quill/models/documents/nodes/embed.dart'; import 'package:flutter_quill/models/documents/style.dart'; @@ -373,17 +374,71 @@ Widget _selectHeadingStyleButtonBuilder( /// When pressed, this button displays overlay toolbar with /// buttons for each color. class ColorButton extends StatefulWidget { - const ColorButton({Key key}) : super(key: key); + final Attribute attribute; + + final IconData icon; + + final QuillController controller; + + ColorButton( + {Key key, + @required this.attribute, + @required this.icon, + @required this.controller}) + : assert(attribute.value != null), + assert(icon != null), + assert(controller != null), + super(key: key); @override _ColorButtonState createState() => _ColorButtonState(); } class _ColorButtonState extends State { + @override Widget build(BuildContext context) { - // TODO - return null; + return _defaultToggleStyleButtonBuilder( + context, widget.attribute, widget.icon, _showColorPicker); + } + + Widget _defaultToggleStyleButtonBuilder( + BuildContext context, + Attribute attribute, + IconData icon, + VoidCallback onPressed, + ) { + final theme = Theme.of(context); + final iconColor = theme.iconTheme.color; + final fillColor = theme.canvasColor; + return QuillIconButton( + highlightElevation: 0, + hoverElevation: 0, + size: 32, + icon: Icon(icon, size: 18, color: iconColor), + fillColor: fillColor, + onPressed: onPressed, + ); + } + + void _changeColor(Color color) { + widget.controller + .formatSelection(ColorAttribute('#${color.value.toRadixString(16)}')); + Navigator.of(context).pop(); + } + + _showColorPicker() { + showDialog( + context: context, + child: AlertDialog( + title: const Text('Select Color'), + content: SingleChildScrollView( + child: MaterialPicker( + pickerColor: Color(0), + onColorChanged: _changeColor, + ), + )), + ); } } @@ -447,7 +502,7 @@ class QuillToolbar extends StatefulWidget implements PreferredSizeWidget { SizedBox(width: 1), Visibility( visible: showColorButton, - child: ToggleStyleButton( + child: ColorButton( attribute: ColorAttribute('#000000'), icon: Icons.format_color_text, controller: controller, diff --git a/pubspec.lock b/pubspec.lock index c9a89aa9..b025d85c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -55,6 +55,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_colorpicker: + dependency: "direct main" + description: + name: flutter_colorpicker + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.4" flutter_test: dependency: "direct dev" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 0fe6fc80..9272c752 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,6 +17,7 @@ dependencies: collection: ^1.14.13 tuple: ^1.0.3 url_launcher: ^5.7.10 + flutter_colorpicker: ^0.3.4 dev_dependencies: flutter_test: