card.dart 9.6 KB

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