card.dart 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import 'package:appflowy/plugins/database_view/application/cell/cell_service.dart';
  2. import 'package:appflowy/plugins/database_view/application/row/row_cache.dart';
  3. import 'package:appflowy/plugins/database_view/grid/presentation/widgets/row/action.dart';
  4. import 'package:appflowy_backend/protobuf/flowy-database2/row_entities.pb.dart';
  5. import 'package:appflowy_popover/appflowy_popover.dart';
  6. import 'package:flowy_infra/image.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 'card_bloc.dart';
  12. import 'cells/card_cell.dart';
  13. import 'card_cell_builder.dart';
  14. import 'container/accessory.dart';
  15. import 'container/card_container.dart';
  16. /// Edit a database row with card style widget
  17. class RowCard<CustomCardData> extends StatefulWidget {
  18. final RowPB row;
  19. final String viewId;
  20. final String? groupingFieldId;
  21. /// Allows passing a custom card data object to the card. The card will be
  22. /// returned in the [CardCellBuilder] and can be used to build the card.
  23. final CustomCardData? cardData;
  24. final bool isEditing;
  25. final RowCache rowCache;
  26. /// The [CardCellBuilder] is used to build the card cells.
  27. final CardCellBuilder<CustomCardData> cellBuilder;
  28. /// Called when the user taps on the card.
  29. final void Function(BuildContext) openCard;
  30. /// Called when the user starts editing the card.
  31. final VoidCallback onStartEditing;
  32. /// Called when the user ends editing the card.
  33. final VoidCallback onEndEditing;
  34. /// The [RowCardRenderHook] is used to render the card's cell. Other than
  35. /// using the default cell builder. For example the [SelectOptionCardCell]
  36. final RowCardRenderHook<CustomCardData>? renderHook;
  37. final RowCardStyleConfiguration styleConfiguration;
  38. const RowCard({
  39. required this.row,
  40. required this.viewId,
  41. this.groupingFieldId,
  42. required this.isEditing,
  43. required this.rowCache,
  44. required this.cellBuilder,
  45. required this.openCard,
  46. required this.onStartEditing,
  47. required this.onEndEditing,
  48. this.cardData,
  49. this.styleConfiguration = const RowCardStyleConfiguration(
  50. showAccessory: true,
  51. ),
  52. this.renderHook,
  53. Key? key,
  54. }) : super(key: key);
  55. @override
  56. State<RowCard<CustomCardData>> createState() =>
  57. _RowCardState<CustomCardData>();
  58. }
  59. class _RowCardState<T> extends State<RowCard<T>> {
  60. late CardBloc _cardBloc;
  61. late EditableRowNotifier rowNotifier;
  62. late PopoverController popoverController;
  63. AccessoryType? accessoryType;
  64. @override
  65. void initState() {
  66. rowNotifier = EditableRowNotifier(isEditing: widget.isEditing);
  67. _cardBloc = CardBloc(
  68. viewId: widget.viewId,
  69. groupFieldId: widget.groupingFieldId,
  70. isEditing: widget.isEditing,
  71. row: widget.row,
  72. rowCache: widget.rowCache,
  73. )..add(const RowCardEvent.initial());
  74. rowNotifier.isEditing.addListener(() {
  75. if (!mounted) return;
  76. _cardBloc.add(RowCardEvent.setIsEditing(rowNotifier.isEditing.value));
  77. if (rowNotifier.isEditing.value) {
  78. widget.onStartEditing();
  79. } else {
  80. widget.onEndEditing();
  81. }
  82. });
  83. popoverController = PopoverController();
  84. super.initState();
  85. }
  86. @override
  87. Widget build(BuildContext context) {
  88. return BlocProvider.value(
  89. value: _cardBloc,
  90. child: BlocBuilder<CardBloc, RowCardState>(
  91. buildWhen: (previous, current) {
  92. // Rebuild when:
  93. // 1.If the length of the cells is not the same
  94. // 2.isEditing changed
  95. if (previous.cells.length != current.cells.length ||
  96. previous.isEditing != current.isEditing) {
  97. return true;
  98. }
  99. // 3.Compare the content of the cells. The cells consists of
  100. // list of [BoardCellEquatable] that extends the [Equatable].
  101. return !listEquals(previous.cells, current.cells);
  102. },
  103. builder: (context, state) {
  104. return AppFlowyPopover(
  105. controller: popoverController,
  106. triggerActions: PopoverTriggerFlags.none,
  107. constraints: BoxConstraints.loose(const Size(140, 200)),
  108. margin: const EdgeInsets.all(6),
  109. direction: PopoverDirection.rightWithCenterAligned,
  110. popupBuilder: (popoverContext) => _handlePopoverBuilder(
  111. context,
  112. popoverContext,
  113. ),
  114. child: RowCardContainer(
  115. buildAccessoryWhen: () => state.isEditing == false,
  116. accessoryBuilder: (context) {
  117. if (widget.styleConfiguration.showAccessory == false) {
  118. return [];
  119. } else {
  120. return [
  121. _CardEditOption(rowNotifier: rowNotifier),
  122. _CardMoreOption(),
  123. ];
  124. }
  125. },
  126. openAccessory: _handleOpenAccessory,
  127. openCard: (context) => widget.openCard(context),
  128. child: _CardContent<T>(
  129. rowNotifier: rowNotifier,
  130. cellBuilder: widget.cellBuilder,
  131. styleConfiguration: widget.styleConfiguration,
  132. cells: state.cells,
  133. renderHook: widget.renderHook,
  134. cardData: widget.cardData,
  135. ),
  136. ),
  137. );
  138. },
  139. ),
  140. );
  141. }
  142. void _handleOpenAccessory(AccessoryType newAccessoryType) {
  143. accessoryType = newAccessoryType;
  144. switch (newAccessoryType) {
  145. case AccessoryType.edit:
  146. break;
  147. case AccessoryType.more:
  148. popoverController.show();
  149. break;
  150. }
  151. }
  152. Widget _handlePopoverBuilder(
  153. BuildContext context,
  154. BuildContext popoverContext,
  155. ) {
  156. switch (accessoryType!) {
  157. case AccessoryType.edit:
  158. throw UnimplementedError();
  159. case AccessoryType.more:
  160. return RowActions(
  161. rowData: context.read<CardBloc>().rowInfo(),
  162. );
  163. }
  164. }
  165. @override
  166. Future<void> dispose() async {
  167. rowNotifier.dispose();
  168. _cardBloc.close();
  169. super.dispose();
  170. }
  171. }
  172. class _CardContent<CustomCardData> extends StatelessWidget {
  173. final CardCellBuilder<CustomCardData> cellBuilder;
  174. final EditableRowNotifier rowNotifier;
  175. final List<CellIdentifier> cells;
  176. final RowCardRenderHook<CustomCardData>? renderHook;
  177. final CustomCardData? cardData;
  178. final RowCardStyleConfiguration styleConfiguration;
  179. const _CardContent({
  180. required this.rowNotifier,
  181. required this.cellBuilder,
  182. required this.cells,
  183. required this.cardData,
  184. required this.styleConfiguration,
  185. this.renderHook,
  186. Key? key,
  187. }) : super(key: key);
  188. @override
  189. Widget build(BuildContext context) {
  190. return Column(
  191. mainAxisSize: MainAxisSize.min,
  192. children: _makeCells(context, cells),
  193. );
  194. }
  195. List<Widget> _makeCells(
  196. BuildContext context,
  197. List<CellIdentifier> cells,
  198. ) {
  199. final List<Widget> children = [];
  200. // Remove all the cell listeners.
  201. rowNotifier.unbind();
  202. cells.asMap().forEach(
  203. (int index, CellIdentifier cell) {
  204. final isEditing = index == 0 ? rowNotifier.isEditing.value : false;
  205. final cellNotifier = EditableCardNotifier(isEditing: isEditing);
  206. if (index == 0) {
  207. // Only use the first cell to receive user's input when click the edit
  208. // button
  209. rowNotifier.bindCell(cell, cellNotifier);
  210. }
  211. final child = Padding(
  212. key: cell.key(),
  213. padding: styleConfiguration.cellPadding,
  214. child: cellBuilder.buildCell(
  215. cellId: cell,
  216. cellNotifier: cellNotifier,
  217. renderHook: renderHook,
  218. cardData: cardData,
  219. ),
  220. );
  221. children.add(child);
  222. },
  223. );
  224. return children;
  225. }
  226. }
  227. class _CardMoreOption extends StatelessWidget with CardAccessory {
  228. _CardMoreOption({Key? key}) : super(key: key);
  229. @override
  230. Widget build(BuildContext context) {
  231. return Padding(
  232. padding: const EdgeInsets.all(3.0),
  233. child: svgWidget(
  234. 'grid/details',
  235. color: Theme.of(context).iconTheme.color,
  236. ),
  237. );
  238. }
  239. @override
  240. AccessoryType get type => AccessoryType.more;
  241. }
  242. class _CardEditOption extends StatelessWidget with CardAccessory {
  243. final EditableRowNotifier rowNotifier;
  244. const _CardEditOption({
  245. required this.rowNotifier,
  246. Key? key,
  247. }) : super(key: key);
  248. @override
  249. Widget build(BuildContext context) {
  250. return Padding(
  251. padding: const EdgeInsets.all(3.0),
  252. child: svgWidget(
  253. 'editor/edit',
  254. color: Theme.of(context).iconTheme.color,
  255. ),
  256. );
  257. }
  258. @override
  259. void onTap(BuildContext context) => rowNotifier.becomeFirstResponder();
  260. @override
  261. AccessoryType get type => AccessoryType.edit;
  262. }
  263. class RowCardStyleConfiguration {
  264. final bool showAccessory;
  265. final EdgeInsets cellPadding;
  266. const RowCardStyleConfiguration({
  267. this.showAccessory = true,
  268. this.cellPadding = const EdgeInsets.only(left: 4, right: 4),
  269. });
  270. }