card.dart 7.0 KB

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