card.dart 7.2 KB

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