tool_bar.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import 'dart:async';
  2. import 'dart:math';
  3. import 'package:app_flowy/workspace/presentation/widgets/emoji_picker/emoji_picker.dart';
  4. import 'package:easy_localization/easy_localization.dart';
  5. import 'package:flutter_quill/flutter_quill.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:styled_widget/styled_widget.dart';
  8. import 'check_button.dart';
  9. import 'color_picker.dart';
  10. import 'header_button.dart';
  11. import 'history_button.dart';
  12. import 'link_button.dart';
  13. import 'toggle_button.dart';
  14. import 'toolbar_icon_button.dart';
  15. import 'package:app_flowy/generated/locale_keys.g.dart';
  16. class EditorToolbar extends StatelessWidget implements PreferredSizeWidget {
  17. final List<Widget> children;
  18. final double toolBarHeight;
  19. final Color? color;
  20. const EditorToolbar({
  21. required this.children,
  22. this.toolBarHeight = 46,
  23. this.color,
  24. Key? key,
  25. }) : super(key: key);
  26. @override
  27. Widget build(BuildContext context) {
  28. return Container(
  29. color: Theme.of(context).canvasColor,
  30. constraints: BoxConstraints.tightFor(height: preferredSize.height),
  31. child: ToolbarButtonList(buttons: children)
  32. .padding(horizontal: 4, vertical: 4),
  33. );
  34. }
  35. @override
  36. Size get preferredSize => Size.fromHeight(toolBarHeight);
  37. factory EditorToolbar.basic({
  38. required QuillController controller,
  39. double toolbarIconSize = defaultIconSize,
  40. OnImagePickCallback? onImagePickCallback,
  41. OnVideoPickCallback? onVideoPickCallback,
  42. MediaPickSettingSelector? mediaPickSettingSelector,
  43. FilePickImpl? filePickImpl,
  44. WebImagePickImpl? webImagePickImpl,
  45. WebVideoPickImpl? webVideoPickImpl,
  46. Key? key,
  47. }) {
  48. return EditorToolbar(
  49. key: key,
  50. toolBarHeight: toolbarIconSize * 2,
  51. children: [
  52. FlowyHistoryButton(
  53. icon: Icons.undo_outlined,
  54. iconSize: toolbarIconSize,
  55. controller: controller,
  56. undo: true,
  57. tooltipText: LocaleKeys.toolbar_undo.tr(),
  58. ),
  59. FlowyHistoryButton(
  60. icon: Icons.redo_outlined,
  61. iconSize: toolbarIconSize,
  62. controller: controller,
  63. undo: false,
  64. tooltipText: LocaleKeys.toolbar_redo.tr(),
  65. ),
  66. FlowyToggleStyleButton(
  67. attribute: Attribute.bold,
  68. normalIcon: 'editor/bold',
  69. iconSize: toolbarIconSize,
  70. controller: controller,
  71. tooltipText: LocaleKeys.toolbar_bold.tr(),
  72. ),
  73. FlowyToggleStyleButton(
  74. attribute: Attribute.italic,
  75. normalIcon: 'editor/italic',
  76. iconSize: toolbarIconSize,
  77. controller: controller,
  78. tooltipText: LocaleKeys.toolbar_italic.tr(),
  79. ),
  80. FlowyToggleStyleButton(
  81. attribute: Attribute.underline,
  82. normalIcon: 'editor/underline',
  83. iconSize: toolbarIconSize,
  84. controller: controller,
  85. tooltipText: LocaleKeys.toolbar_underline.tr(),
  86. ),
  87. FlowyToggleStyleButton(
  88. attribute: Attribute.strikeThrough,
  89. normalIcon: 'editor/strikethrough',
  90. iconSize: toolbarIconSize,
  91. controller: controller,
  92. tooltipText: LocaleKeys.toolbar_strike.tr(),
  93. ),
  94. FlowyColorButton(
  95. icon: Icons.format_color_fill,
  96. iconSize: toolbarIconSize,
  97. controller: controller,
  98. background: true,
  99. ),
  100. // FlowyImageButton(
  101. // iconSize: toolbarIconSize,
  102. // controller: controller,
  103. // onImagePickCallback: onImagePickCallback,
  104. // filePickImpl: filePickImpl,
  105. // webImagePickImpl: webImagePickImpl,
  106. // mediaPickSettingSelector: mediaPickSettingSelector,
  107. // ),
  108. FlowyHeaderStyleButton(
  109. controller: controller,
  110. iconSize: toolbarIconSize,
  111. ),
  112. FlowyToggleStyleButton(
  113. attribute: Attribute.ol,
  114. controller: controller,
  115. normalIcon: 'editor/numbers',
  116. iconSize: toolbarIconSize,
  117. tooltipText: LocaleKeys.toolbar_numList.tr(),
  118. ),
  119. FlowyToggleStyleButton(
  120. attribute: Attribute.ul,
  121. controller: controller,
  122. normalIcon: 'editor/bullet_list',
  123. iconSize: toolbarIconSize,
  124. tooltipText: LocaleKeys.toolbar_bulletList.tr(),
  125. ),
  126. FlowyCheckListButton(
  127. attribute: Attribute.unchecked,
  128. controller: controller,
  129. iconSize: toolbarIconSize,
  130. tooltipText: LocaleKeys.toolbar_checkList.tr(),
  131. ),
  132. FlowyToggleStyleButton(
  133. attribute: Attribute.inlineCode,
  134. controller: controller,
  135. normalIcon: 'editor/inline_block',
  136. iconSize: toolbarIconSize,
  137. tooltipText: LocaleKeys.toolbar_inlineCode.tr(),
  138. ),
  139. FlowyToggleStyleButton(
  140. attribute: Attribute.blockQuote,
  141. controller: controller,
  142. normalIcon: 'editor/quote',
  143. iconSize: toolbarIconSize,
  144. tooltipText: LocaleKeys.toolbar_quote.tr(),
  145. ),
  146. FlowyLinkStyleButton(
  147. controller: controller,
  148. iconSize: toolbarIconSize,
  149. ),
  150. FlowyEmojiStyleButton(
  151. normalIcon: 'editor/insert_emoticon',
  152. controller: controller,
  153. tooltipText: "Emoji Picker",
  154. ),
  155. ],
  156. );
  157. }
  158. }
  159. class ToolbarButtonList extends StatefulWidget {
  160. const ToolbarButtonList({required this.buttons, Key? key}) : super(key: key);
  161. final List<Widget> buttons;
  162. @override
  163. ToolbarButtonListState createState() => ToolbarButtonListState();
  164. }
  165. class ToolbarButtonListState extends State<ToolbarButtonList>
  166. with WidgetsBindingObserver {
  167. final ScrollController _controller = ScrollController();
  168. bool _showLeftArrow = false;
  169. bool _showRightArrow = false;
  170. @override
  171. void initState() {
  172. super.initState();
  173. _controller.addListener(_handleScroll);
  174. // Listening to the WidgetsBinding instance is necessary so that we can
  175. // hide the arrows when the window gets a new size and thus the toolbar
  176. // becomes scrollable/unscrollable.
  177. WidgetsBinding.instance.addObserver(this);
  178. // Workaround to allow the scroll controller attach to our ListView so that
  179. // we can detect if overflow arrows need to be shown on init.
  180. Timer.run(_handleScroll);
  181. }
  182. @override
  183. Widget build(BuildContext context) {
  184. return LayoutBuilder(
  185. builder: (BuildContext context, BoxConstraints constraints) {
  186. List<Widget> children = [];
  187. double width =
  188. (widget.buttons.length + 2) * defaultIconSize * kIconButtonFactor;
  189. final isFit = constraints.maxWidth > width;
  190. if (!isFit) {
  191. children.add(_buildLeftArrow());
  192. width = width + 18;
  193. }
  194. children.add(_buildScrollableList(constraints, isFit));
  195. if (!isFit) {
  196. children.add(_buildRightArrow());
  197. width = width + 18;
  198. }
  199. return SizedBox(
  200. width: min(constraints.maxWidth, width),
  201. child: Row(
  202. children: children,
  203. ),
  204. );
  205. },
  206. );
  207. }
  208. @override
  209. void didChangeMetrics() => _handleScroll();
  210. @override
  211. void dispose() {
  212. _controller.dispose();
  213. WidgetsBinding.instance.removeObserver(this);
  214. super.dispose();
  215. }
  216. void _handleScroll() {
  217. if (!mounted) return;
  218. setState(() {
  219. _showLeftArrow =
  220. _controller.position.minScrollExtent != _controller.position.pixels;
  221. _showRightArrow =
  222. _controller.position.maxScrollExtent != _controller.position.pixels;
  223. });
  224. }
  225. Widget _buildLeftArrow() {
  226. return SizedBox(
  227. width: 8,
  228. child: Transform.translate(
  229. // Move the icon a few pixels to center it
  230. offset: const Offset(-5, 0),
  231. child: _showLeftArrow ? const Icon(Icons.arrow_left, size: 18) : null,
  232. ),
  233. );
  234. }
  235. // [[sliver: https://medium.com/flutter/slivers-demystified-6ff68ab0296f]]
  236. Widget _buildScrollableList(BoxConstraints constraints, bool isFit) {
  237. Widget child = Expanded(
  238. child: CustomScrollView(
  239. scrollDirection: Axis.horizontal,
  240. controller: _controller,
  241. physics: const ClampingScrollPhysics(),
  242. slivers: [
  243. SliverList(
  244. delegate: SliverChildBuilderDelegate(
  245. (BuildContext context, int index) {
  246. return widget.buttons[index];
  247. },
  248. childCount: widget.buttons.length,
  249. addAutomaticKeepAlives: false,
  250. ),
  251. )
  252. ],
  253. ),
  254. );
  255. if (!isFit) {
  256. child = ScrollConfiguration(
  257. // Remove the glowing effect, as we already have the arrow indicators
  258. behavior: _NoGlowBehavior(),
  259. // The CustomScrollView is necessary so that the children are not
  260. // stretched to the height of the toolbar, https://bit.ly/3uC3bjI
  261. child: child,
  262. );
  263. }
  264. return child;
  265. }
  266. Widget _buildRightArrow() {
  267. return SizedBox(
  268. width: 8,
  269. child: Transform.translate(
  270. // Move the icon a few pixels to center it
  271. offset: const Offset(-5, 0),
  272. child: _showRightArrow ? const Icon(Icons.arrow_right, size: 18) : null,
  273. ),
  274. );
  275. }
  276. }
  277. class _NoGlowBehavior extends ScrollBehavior {
  278. @override
  279. Widget buildViewportChrome(BuildContext _, Widget child, AxisDirection __) {
  280. return child;
  281. }
  282. }