card.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import 'package:app_flowy/plugins/board/application/card/card_bloc.dart';
  2. import 'package:app_flowy/plugins/board/application/card/card_data_controller.dart';
  3. import 'package:app_flowy/plugins/grid/presentation/widgets/row/row_action_sheet.dart';
  4. import 'package:appflowy_popover/appflowy_popover.dart';
  5. import 'package:flowy_infra/image.dart';
  6. import 'package:flowy_infra/theme.dart';
  7. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  8. import 'package:flutter/foundation.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_bloc/flutter_bloc.dart';
  11. import 'board_cell.dart';
  12. import 'card_cell_builder.dart';
  13. import 'container/accessory.dart';
  14. import 'container/card_container.dart';
  15. class BoardCard extends StatefulWidget {
  16. final String gridId;
  17. final String groupId;
  18. final String fieldId;
  19. final bool isEditing;
  20. final CardDataController dataController;
  21. final BoardCellBuilder cellBuilder;
  22. final void Function(BuildContext) openCard;
  23. final VoidCallback onStartEditing;
  24. final VoidCallback onEndEditing;
  25. const BoardCard({
  26. required this.gridId,
  27. required this.groupId,
  28. required this.fieldId,
  29. required this.isEditing,
  30. required this.dataController,
  31. required this.cellBuilder,
  32. required this.openCard,
  33. required this.onStartEditing,
  34. required this.onEndEditing,
  35. Key? key,
  36. }) : super(key: key);
  37. @override
  38. State<BoardCard> createState() => _BoardCardState();
  39. }
  40. class _BoardCardState extends State<BoardCard> {
  41. late BoardCardBloc _cardBloc;
  42. late EditableRowNotifier rowNotifier;
  43. late PopoverController popoverController;
  44. AccessoryType? accessoryType;
  45. @override
  46. void initState() {
  47. rowNotifier = EditableRowNotifier(isEditing: widget.isEditing);
  48. _cardBloc = BoardCardBloc(
  49. gridId: widget.gridId,
  50. groupFieldId: widget.fieldId,
  51. dataController: widget.dataController,
  52. isEditing: widget.isEditing,
  53. )..add(const BoardCardEvent.initial());
  54. rowNotifier.isEditing.addListener(() {
  55. if (!mounted) return;
  56. _cardBloc.add(BoardCardEvent.setIsEditing(rowNotifier.isEditing.value));
  57. if (rowNotifier.isEditing.value) {
  58. widget.onStartEditing();
  59. } else {
  60. widget.onEndEditing();
  61. }
  62. });
  63. popoverController = PopoverController();
  64. super.initState();
  65. }
  66. @override
  67. Widget build(BuildContext context) {
  68. return BlocProvider.value(
  69. value: _cardBloc,
  70. child: BlocBuilder<BoardCardBloc, BoardCardState>(
  71. buildWhen: (previous, current) {
  72. // Rebuild when:
  73. // 1.If the length of the cells is not the same
  74. // 2.isEditing changed
  75. if (previous.cells.length != current.cells.length ||
  76. previous.isEditing != current.isEditing) {
  77. return true;
  78. }
  79. // 3.Compare the content of the cells. The cells consists of
  80. // list of [BoardCellEquatable] that extends the [Equatable].
  81. return !listEquals(previous.cells, current.cells);
  82. },
  83. builder: (context, state) {
  84. return AppFlowyPopover(
  85. controller: popoverController,
  86. triggerActions: PopoverTriggerFlags.none,
  87. constraints: BoxConstraints.loose(const Size(140, 200)),
  88. margin: const EdgeInsets.all(6),
  89. direction: PopoverDirection.rightWithCenterAligned,
  90. popupBuilder: (popoverContext) => _handlePopoverBuilder(
  91. context,
  92. popoverContext,
  93. ),
  94. child: BoardCardContainer(
  95. buildAccessoryWhen: () => state.isEditing == false,
  96. accessoryBuilder: (context) {
  97. return [
  98. _CardEditOption(rowNotifier: rowNotifier),
  99. _CardMoreOption(),
  100. ];
  101. },
  102. openAccessory: _handleOpenAccessory,
  103. openCard: (context) => widget.openCard(context),
  104. child: _CellColumn(
  105. groupId: widget.groupId,
  106. rowNotifier: rowNotifier,
  107. cellBuilder: widget.cellBuilder,
  108. cells: state.cells,
  109. ),
  110. ),
  111. );
  112. },
  113. ),
  114. );
  115. }
  116. void _handleOpenAccessory(AccessoryType newAccessoryType) {
  117. accessoryType = newAccessoryType;
  118. switch (newAccessoryType) {
  119. case AccessoryType.edit:
  120. break;
  121. case AccessoryType.more:
  122. popoverController.show();
  123. break;
  124. }
  125. }
  126. Widget _handlePopoverBuilder(
  127. BuildContext context,
  128. BuildContext popoverContext,
  129. ) {
  130. switch (accessoryType!) {
  131. case AccessoryType.edit:
  132. throw UnimplementedError();
  133. case AccessoryType.more:
  134. return GridRowActionSheet(
  135. rowData: context.read<BoardCardBloc>().rowInfo(),
  136. );
  137. }
  138. }
  139. @override
  140. Future<void> dispose() async {
  141. rowNotifier.dispose();
  142. _cardBloc.close();
  143. super.dispose();
  144. }
  145. }
  146. class _CellColumn extends StatelessWidget {
  147. final String groupId;
  148. final BoardCellBuilder cellBuilder;
  149. final EditableRowNotifier rowNotifier;
  150. final List<BoardCellEquatable> cells;
  151. const _CellColumn({
  152. required this.groupId,
  153. required this.rowNotifier,
  154. required this.cellBuilder,
  155. required this.cells,
  156. Key? key,
  157. }) : super(key: key);
  158. @override
  159. Widget build(BuildContext context) {
  160. return Column(
  161. mainAxisSize: MainAxisSize.min,
  162. children: _makeCells(context, cells),
  163. );
  164. }
  165. List<Widget> _makeCells(
  166. BuildContext context,
  167. List<BoardCellEquatable> cells,
  168. ) {
  169. final List<Widget> children = [];
  170. // Remove all the cell listeners.
  171. rowNotifier.unbind();
  172. cells.asMap().forEach(
  173. (int index, BoardCellEquatable cell) {
  174. final isEditing = index == 0 ? rowNotifier.isEditing.value : false;
  175. final cellNotifier = EditableCellNotifier(isEditing: isEditing);
  176. if (index == 0) {
  177. // Only use the first cell to receive user's input when click the edit
  178. // button
  179. rowNotifier.bindCell(cell.identifier, cellNotifier);
  180. }
  181. final child = Padding(
  182. key: cell.identifier.key(),
  183. padding: const EdgeInsets.only(left: 4, right: 4),
  184. child: cellBuilder.buildCell(
  185. groupId,
  186. cell.identifier,
  187. cellNotifier,
  188. ),
  189. );
  190. children.add(child);
  191. },
  192. );
  193. return children;
  194. }
  195. }
  196. class _CardMoreOption extends StatelessWidget with CardAccessory {
  197. _CardMoreOption({Key? key}) : super(key: key);
  198. @override
  199. Widget build(BuildContext context) {
  200. return Padding(
  201. padding: const EdgeInsets.all(3.0),
  202. child:
  203. svgWidget('grid/details', color: context.read<AppTheme>().iconColor),
  204. );
  205. }
  206. @override
  207. AccessoryType get type => AccessoryType.more;
  208. }
  209. class _CardEditOption extends StatelessWidget with CardAccessory {
  210. final EditableRowNotifier rowNotifier;
  211. const _CardEditOption({
  212. required this.rowNotifier,
  213. Key? key,
  214. }) : super(key: key);
  215. @override
  216. Widget build(BuildContext context) {
  217. return Padding(
  218. padding: const EdgeInsets.all(3.0),
  219. child: svgWidget(
  220. 'editor/edit',
  221. color: context.read<AppTheme>().iconColor,
  222. ),
  223. );
  224. }
  225. @override
  226. void onTap(BuildContext context) => rowNotifier.becomeFirstResponder();
  227. @override
  228. AccessoryType get type => AccessoryType.edit;
  229. }