From 733c9bd1b0ffa10e86d700a4301ddd0a797d0647 Mon Sep 17 00:00:00 2001 From: mark8044 <87546778+mark8044@users.noreply.github.com> Date: Sun, 14 Nov 2021 10:11:18 -0800 Subject: [PATCH] Add ability to hide/show Left, Center, Right and Justify alignment buttons (#460) --- lib/src/widgets/toolbar.dart | 12 +++++++ .../toolbar/select_alignment_button.dart | 36 ++++++++++++------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/src/widgets/toolbar.dart b/lib/src/widgets/toolbar.dart index 9d74ca45..1c9a0391 100644 --- a/lib/src/widgets/toolbar.dart +++ b/lib/src/widgets/toolbar.dart @@ -79,6 +79,10 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget { bool showBackgroundColorButton = true, bool showClearFormat = true, bool showAlignmentButtons = false, + bool showLeftAlignment = true, + bool showCenterAlignment = true, + bool showRightAlignment = true, + bool showJustifyAlignment = true, bool showHeaderStyle = true, bool showListNumbers = true, bool showListBullets = true, @@ -130,6 +134,10 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget { onImagePickCallback != null || onVideoPickCallback != null, showAlignmentButtons, + showLeftAlignment, + showCenterAlignment, + showRightAlignment, + showJustifyAlignment, showHeaderStyle, showListNumbers || showListBullets || showListCheck || showCodeBlock, showQuote || showIndent, @@ -282,6 +290,10 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget { controller: controller, iconSize: toolbarIconSize, iconTheme: iconTheme, + showLeftAlignment: showLeftAlignment, + showCenterAlignment: showCenterAlignment, + showRightAlignment: showRightAlignment, + showJustifyAlignment: showJustifyAlignment, ), if (isButtonGroupShown[1] && (isButtonGroupShown[2] || diff --git a/lib/src/widgets/toolbar/select_alignment_button.dart b/lib/src/widgets/toolbar/select_alignment_button.dart index 9761a96d..df3abb8b 100644 --- a/lib/src/widgets/toolbar/select_alignment_button.dart +++ b/lib/src/widgets/toolbar/select_alignment_button.dart @@ -12,6 +12,10 @@ class SelectAlignmentButton extends StatefulWidget { required this.controller, this.iconSize = kDefaultIconSize, this.iconTheme, + this.showLeftAlignment, + this.showCenterAlignment, + this.showRightAlignment, + this.showJustifyAlignment, Key? key, }) : super(key: key); @@ -19,6 +23,10 @@ class SelectAlignmentButton extends StatefulWidget { final double iconSize; final QuillIconTheme? iconTheme; + final bool? showLeftAlignment; + final bool? showCenterAlignment; + final bool? showRightAlignment; + final bool? showJustifyAlignment; @override _SelectAlignmentButtonState createState() => _SelectAlignmentButtonState(); @@ -42,30 +50,32 @@ class _SelectAlignmentButtonState extends State { @override Widget build(BuildContext context) { final _valueToText = { - Attribute.leftAlignment: Attribute.leftAlignment.value!, - Attribute.centerAlignment: Attribute.centerAlignment.value!, - Attribute.rightAlignment: Attribute.rightAlignment.value!, - Attribute.justifyAlignment: Attribute.justifyAlignment.value!, + if (widget.showLeftAlignment!) Attribute.leftAlignment: Attribute.leftAlignment.value!, + if (widget.showCenterAlignment!) Attribute.centerAlignment: Attribute.centerAlignment.value!, + if (widget.showRightAlignment!) Attribute.rightAlignment: Attribute.rightAlignment.value!, + if (widget.showJustifyAlignment!) Attribute.justifyAlignment: Attribute.justifyAlignment.value!, }; final _valueAttribute = [ - Attribute.leftAlignment, - Attribute.centerAlignment, - Attribute.rightAlignment, - Attribute.justifyAlignment + if (widget.showLeftAlignment!) Attribute.leftAlignment, + if (widget.showCenterAlignment!) Attribute.centerAlignment, + if (widget.showRightAlignment!) Attribute.rightAlignment, + if (widget.showJustifyAlignment!) Attribute.justifyAlignment ]; final _valueString = [ - Attribute.leftAlignment.value!, - Attribute.centerAlignment.value!, - Attribute.rightAlignment.value!, - Attribute.justifyAlignment.value!, + if (widget.showLeftAlignment!) Attribute.leftAlignment.value!, + if (widget.showCenterAlignment!) Attribute.centerAlignment.value!, + if (widget.showRightAlignment!) Attribute.rightAlignment.value!, + if (widget.showJustifyAlignment!) Attribute.justifyAlignment.value!, ]; final theme = Theme.of(context); + + final buttonCount = ((widget.showLeftAlignment!) ? 1 : 0) + ((widget.showCenterAlignment!) ? 1 : 0) + ((widget.showRightAlignment!) ? 1 : 0) + ((widget.showJustifyAlignment!) ? 1 : 0); return Row( mainAxisSize: MainAxisSize.min, - children: List.generate(4, (index) { + children: List.generate(buttonCount, (index) { return Padding( padding: const EdgeInsets.symmetric(horizontal: !kIsWeb ? 1.0 : 5.0), child: ConstrainedBox(