card.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. direction: PopoverDirection.rightWithCenterAligned,
  80. popupBuilder: (popoverContext) => _handlePopoverBuilder(
  81. context,
  82. popoverContext,
  83. ),
  84. child: BoardCardContainer(
  85. buildAccessoryWhen: () => state.isEditing == false,
  86. accessoryBuilder: (context) {
  87. return [
  88. _CardEditOption(rowNotifier: rowNotifier),
  89. _CardMoreOption(),
  90. ];
  91. },
  92. openAccessory: _handleOpenAccessory,
  93. openCard: (context) => widget.openCard(context),
  94. child: _CellColumn(
  95. groupId: widget.groupId,
  96. rowNotifier: rowNotifier,
  97. cellBuilder: widget.cellBuilder,
  98. cells: state.cells,
  99. ),
  100. ),
  101. );
  102. },
  103. ),
  104. );
  105. }
  106. void _handleOpenAccessory(AccessoryType newAccessoryType) {
  107. accessoryType = newAccessoryType;
  108. switch (newAccessoryType) {
  109. case AccessoryType.edit:
  110. break;
  111. case AccessoryType.more:
  112. popoverController.show();
  113. break;
  114. }
  115. }
  116. Widget _handlePopoverBuilder(
  117. BuildContext context,
  118. BuildContext popoverContext,
  119. ) {
  120. switch (accessoryType!) {
  121. case AccessoryType.edit:
  122. throw UnimplementedError();
  123. case AccessoryType.more:
  124. return GridRowActionSheet(
  125. rowData: context.read<BoardCardBloc>().rowInfo(),
  126. );
  127. }
  128. }
  129. @override
  130. Future<void> dispose() async {
  131. rowNotifier.dispose();
  132. _cardBloc.close();
  133. super.dispose();
  134. }
  135. }
  136. class _CellColumn extends StatelessWidget {
  137. final String groupId;
  138. final BoardCellBuilder cellBuilder;
  139. final EditableRowNotifier rowNotifier;
  140. final List<BoardCellEquatable> cells;
  141. const _CellColumn({
  142. required this.groupId,
  143. required this.rowNotifier,
  144. required this.cellBuilder,
  145. required this.cells,
  146. Key? key,
  147. }) : super(key: key);
  148. @override
  149. Widget build(BuildContext context) {
  150. return Column(
  151. mainAxisSize: MainAxisSize.min,
  152. children: _makeCells(context, cells),
  153. );
  154. }
  155. List<Widget> _makeCells(
  156. BuildContext context,
  157. List<BoardCellEquatable> cells,
  158. ) {
  159. final List<Widget> children = [];
  160. // Remove all the cell listeners.
  161. rowNotifier.unbind();
  162. cells.asMap().forEach(
  163. (int index, BoardCellEquatable cell) {
  164. final isEditing = index == 0 ? rowNotifier.isEditing.value : false;
  165. final cellNotifier = EditableCellNotifier(isEditing: isEditing);
  166. if (index == 0) {
  167. // Only use the first cell to receive user's input when click the edit
  168. // button
  169. rowNotifier.bindCell(cell.identifier, cellNotifier);
  170. }
  171. final child = Padding(
  172. key: cell.identifier.key(),
  173. padding: const EdgeInsets.only(left: 4, right: 4),
  174. child: cellBuilder.buildCell(
  175. groupId,
  176. cell.identifier,
  177. cellNotifier,
  178. ),
  179. );
  180. children.add(child);
  181. },
  182. );
  183. return children;
  184. }
  185. }
  186. class _CardMoreOption extends StatelessWidget with CardAccessory {
  187. _CardMoreOption({Key? key}) : super(key: key);
  188. @override
  189. Widget build(BuildContext context) {
  190. return Padding(
  191. padding: const EdgeInsets.all(3.0),
  192. child:
  193. svgWidget('grid/details', color: context.read<AppTheme>().iconColor),
  194. );
  195. }
  196. @override
  197. AccessoryType get type => AccessoryType.more;
  198. }
  199. class _CardEditOption extends StatelessWidget with CardAccessory {
  200. final EditableRowNotifier rowNotifier;
  201. const _CardEditOption({
  202. required this.rowNotifier,
  203. Key? key,
  204. }) : super(key: key);
  205. @override
  206. Widget build(BuildContext context) {
  207. return Padding(
  208. padding: const EdgeInsets.all(3.0),
  209. child: svgWidget(
  210. 'editor/edit',
  211. color: context.read<AppTheme>().iconColor,
  212. ),
  213. );
  214. }
  215. @override
  216. void onTap(BuildContext context) => rowNotifier.becomeFirstResponder();
  217. @override
  218. AccessoryType get type => AccessoryType.edit;
  219. }