card.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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:flowy_infra/image.dart';
  5. import 'package:flowy_infra/theme.dart';
  6. import 'package:flowy_infra_ui/flowy_infra_ui_web.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 'card_container.dart';
  13. class BoardCard extends StatefulWidget {
  14. final String gridId;
  15. final String groupId;
  16. final String fieldId;
  17. final bool isEditing;
  18. final CardDataController dataController;
  19. final BoardCellBuilder cellBuilder;
  20. final void Function(BuildContext) openCard;
  21. const BoardCard({
  22. required this.gridId,
  23. required this.groupId,
  24. required this.fieldId,
  25. required this.isEditing,
  26. required this.dataController,
  27. required this.cellBuilder,
  28. required this.openCard,
  29. Key? key,
  30. }) : super(key: key);
  31. @override
  32. State<BoardCard> createState() => _BoardCardState();
  33. }
  34. class _BoardCardState extends State<BoardCard> {
  35. late BoardCardBloc _cardBloc;
  36. late EditableRowNotifier rowNotifier;
  37. @override
  38. void initState() {
  39. rowNotifier = EditableRowNotifier(isEditing: widget.isEditing);
  40. _cardBloc = BoardCardBloc(
  41. gridId: widget.gridId,
  42. groupFieldId: widget.fieldId,
  43. dataController: widget.dataController,
  44. isEditing: widget.isEditing,
  45. )..add(const BoardCardEvent.initial());
  46. rowNotifier.isEditing.addListener(() {
  47. if (!mounted) return;
  48. _cardBloc.add(BoardCardEvent.setIsEditing(rowNotifier.isEditing.value));
  49. });
  50. super.initState();
  51. }
  52. @override
  53. Widget build(BuildContext context) {
  54. return BlocProvider.value(
  55. value: _cardBloc,
  56. child: BlocBuilder<BoardCardBloc, BoardCardState>(
  57. buildWhen: (previous, current) {
  58. // Rebuild when:
  59. // 1.If the lenght of the cells is not the same
  60. // 2.isEditing changed
  61. if (previous.cells.length != current.cells.length ||
  62. previous.isEditing != current.isEditing) {
  63. return true;
  64. }
  65. // 3.Compare the content of the cells. The cells consisits of
  66. // list of [BoardCellEquatable] that extends the [Equatable].
  67. return !listEquals(previous.cells, current.cells);
  68. },
  69. builder: (context, state) {
  70. return BoardCardContainer(
  71. buildAccessoryWhen: () => state.isEditing == false,
  72. accessoryBuilder: (context) {
  73. return [
  74. _CardEditOption(rowNotifier: rowNotifier),
  75. const _CardMoreOption(),
  76. ];
  77. },
  78. onTap: (context) => widget.openCard(context),
  79. child: _CellColumn(
  80. groupId: widget.groupId,
  81. rowNotifier: rowNotifier,
  82. cellBuilder: widget.cellBuilder,
  83. cells: state.cells,
  84. ),
  85. );
  86. },
  87. ),
  88. );
  89. }
  90. @override
  91. Future<void> dispose() async {
  92. rowNotifier.dispose();
  93. _cardBloc.close();
  94. super.dispose();
  95. }
  96. }
  97. class _CellColumn extends StatelessWidget {
  98. final String groupId;
  99. final BoardCellBuilder cellBuilder;
  100. final EditableRowNotifier rowNotifier;
  101. final List<BoardCellEquatable> cells;
  102. const _CellColumn({
  103. required this.groupId,
  104. required this.rowNotifier,
  105. required this.cellBuilder,
  106. required this.cells,
  107. Key? key,
  108. }) : super(key: key);
  109. @override
  110. Widget build(BuildContext context) {
  111. return Column(
  112. mainAxisSize: MainAxisSize.min,
  113. children: _makeCells(context, cells),
  114. );
  115. }
  116. List<Widget> _makeCells(
  117. BuildContext context,
  118. List<BoardCellEquatable> cells,
  119. ) {
  120. final List<Widget> children = [];
  121. // Remove all the cell listeners.
  122. rowNotifier.unbind();
  123. cells.asMap().forEach(
  124. (int index, BoardCellEquatable cell) {
  125. final isEditing = index == 0 ? rowNotifier.isEditing.value : false;
  126. final cellNotifier = EditableCellNotifier(isEditing: isEditing);
  127. if (index == 0) {
  128. // Only use the first cell to receive user's input when click the edit
  129. // button
  130. rowNotifier.bindCell(cell.identifier, cellNotifier);
  131. }
  132. final child = Padding(
  133. key: cell.identifier.key(),
  134. padding: const EdgeInsets.only(left: 4, right: 4),
  135. child: cellBuilder.buildCell(
  136. groupId,
  137. cell.identifier,
  138. cellNotifier,
  139. ),
  140. );
  141. children.add(child);
  142. },
  143. );
  144. return children;
  145. }
  146. }
  147. class _CardMoreOption extends StatelessWidget with CardAccessory {
  148. const _CardMoreOption({Key? key}) : super(key: key);
  149. @override
  150. Widget build(BuildContext context) {
  151. return Padding(
  152. padding: const EdgeInsets.all(3.0),
  153. child:
  154. svgWidget('grid/details', color: context.read<AppTheme>().iconColor),
  155. );
  156. }
  157. @override
  158. void onTap(BuildContext context) {
  159. GridRowActionSheet(
  160. rowData: context.read<BoardCardBloc>().rowInfo(),
  161. ).show(context, direction: AnchorDirection.bottomWithCenterAligned);
  162. }
  163. }
  164. class _CardEditOption extends StatelessWidget with CardAccessory {
  165. final EditableRowNotifier rowNotifier;
  166. const _CardEditOption({
  167. required this.rowNotifier,
  168. Key? key,
  169. }) : super(key: key);
  170. @override
  171. Widget build(BuildContext context) {
  172. return Padding(
  173. padding: const EdgeInsets.all(3.0),
  174. child: svgWidget(
  175. 'editor/edit',
  176. color: context.read<AppTheme>().iconColor,
  177. ),
  178. );
  179. }
  180. @override
  181. void onTap(BuildContext context) {
  182. rowNotifier.becomeFirstResponder();
  183. }
  184. }