2022-08-12 20:10:56 +08:00
|
|
|
import 'package:app_flowy/plugins/board/application/card/card_bloc.dart';
|
|
|
|
import 'package:app_flowy/plugins/board/application/card/card_data_controller.dart';
|
2022-08-16 17:36:39 +08:00
|
|
|
import 'package:app_flowy/plugins/grid/presentation/widgets/row/row_action_sheet.dart';
|
2022-08-13 11:51:26 +08:00
|
|
|
import 'package:flowy_infra/image.dart';
|
|
|
|
import 'package:flowy_infra/theme.dart';
|
2022-08-16 17:36:39 +08:00
|
|
|
import 'package:flowy_infra_ui/flowy_infra_ui_web.dart';
|
2022-09-06 20:43:49 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-08-10 17:59:28 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2022-08-12 20:10:56 +08:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2022-08-30 20:54:11 +08:00
|
|
|
import 'board_cell.dart';
|
2022-08-12 20:10:56 +08:00
|
|
|
import 'card_cell_builder.dart';
|
2022-08-13 11:51:26 +08:00
|
|
|
import 'card_container.dart';
|
2022-08-10 17:59:28 +08:00
|
|
|
|
2022-08-12 20:10:56 +08:00
|
|
|
class BoardCard extends StatefulWidget {
|
|
|
|
final String gridId;
|
2022-08-25 16:17:19 +08:00
|
|
|
final String groupId;
|
2022-08-26 16:07:51 +08:00
|
|
|
final String fieldId;
|
2022-08-13 16:23:44 +08:00
|
|
|
final bool isEditing;
|
2022-08-12 20:10:56 +08:00
|
|
|
final CardDataController dataController;
|
|
|
|
final BoardCellBuilder cellBuilder;
|
2022-08-16 18:02:39 +08:00
|
|
|
final void Function(BuildContext) openCard;
|
2022-08-12 20:10:56 +08:00
|
|
|
|
|
|
|
const BoardCard({
|
|
|
|
required this.gridId,
|
2022-08-25 16:17:19 +08:00
|
|
|
required this.groupId,
|
2022-08-26 16:07:51 +08:00
|
|
|
required this.fieldId,
|
2022-08-13 16:23:44 +08:00
|
|
|
required this.isEditing,
|
2022-08-12 20:10:56 +08:00
|
|
|
required this.dataController,
|
|
|
|
required this.cellBuilder,
|
2022-08-16 18:02:39 +08:00
|
|
|
required this.openCard,
|
2022-08-12 20:10:56 +08:00
|
|
|
Key? key,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<BoardCard> createState() => _BoardCardState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BoardCardState extends State<BoardCard> {
|
|
|
|
late BoardCardBloc _cardBloc;
|
2022-08-30 20:54:11 +08:00
|
|
|
late EditableRowNotifier rowNotifier;
|
2022-08-12 20:10:56 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2022-09-07 11:33:42 +08:00
|
|
|
rowNotifier = EditableRowNotifier(isEditing: widget.isEditing);
|
2022-08-12 20:10:56 +08:00
|
|
|
_cardBloc = BoardCardBloc(
|
|
|
|
gridId: widget.gridId,
|
2022-09-06 17:52:05 +08:00
|
|
|
groupFieldId: widget.fieldId,
|
2022-08-12 20:10:56 +08:00
|
|
|
dataController: widget.dataController,
|
2022-09-07 11:33:42 +08:00
|
|
|
isEditing: widget.isEditing,
|
2022-08-24 21:06:10 +08:00
|
|
|
)..add(const BoardCardEvent.initial());
|
2022-09-07 11:33:42 +08:00
|
|
|
|
|
|
|
rowNotifier.isEditing.addListener(() {
|
|
|
|
if (!mounted) return;
|
|
|
|
_cardBloc.add(BoardCardEvent.setIsEditing(rowNotifier.isEditing.value));
|
|
|
|
});
|
|
|
|
|
2022-08-12 20:10:56 +08:00
|
|
|
super.initState();
|
|
|
|
}
|
2022-08-10 17:59:28 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-12 20:10:56 +08:00
|
|
|
return BlocProvider.value(
|
|
|
|
value: _cardBloc,
|
|
|
|
child: BlocBuilder<BoardCardBloc, BoardCardState>(
|
2022-08-26 16:07:51 +08:00
|
|
|
buildWhen: (previous, current) {
|
2022-09-18 11:06:04 +08:00
|
|
|
// Rebuild when:
|
|
|
|
// 1.If the lenght of the cells is not the same
|
|
|
|
// 2.isEditing changed
|
2022-09-07 11:33:42 +08:00
|
|
|
if (previous.cells.length != current.cells.length ||
|
|
|
|
previous.isEditing != current.isEditing) {
|
2022-09-06 20:43:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
2022-09-18 11:06:04 +08:00
|
|
|
|
|
|
|
// 3.Compare the content of the cells. The cells consisits of
|
|
|
|
// list of [BoardCellEquatable] that extends the [Equatable].
|
2022-09-06 20:43:49 +08:00
|
|
|
return !listEquals(previous.cells, current.cells);
|
2022-08-26 16:07:51 +08:00
|
|
|
},
|
2022-08-12 20:10:56 +08:00
|
|
|
builder: (context, state) {
|
2022-08-13 11:51:26 +08:00
|
|
|
return BoardCardContainer(
|
2022-09-07 11:33:42 +08:00
|
|
|
buildAccessoryWhen: () => state.isEditing == false,
|
2022-08-13 11:51:26 +08:00
|
|
|
accessoryBuilder: (context) {
|
2022-08-30 20:54:11 +08:00
|
|
|
return [
|
2022-09-18 11:06:04 +08:00
|
|
|
_CardEditOption(rowNotifier: rowNotifier),
|
2022-08-30 20:54:11 +08:00
|
|
|
const _CardMoreOption(),
|
|
|
|
];
|
2022-08-13 11:51:26 +08:00
|
|
|
},
|
2022-09-18 11:06:04 +08:00
|
|
|
onTap: (context) => widget.openCard(context),
|
|
|
|
child: _CellColumn(
|
|
|
|
groupId: widget.groupId,
|
|
|
|
rowNotifier: rowNotifier,
|
|
|
|
cellBuilder: widget.cellBuilder,
|
|
|
|
cells: state.cells,
|
2022-08-13 10:01:40 +08:00
|
|
|
),
|
2022-08-12 20:10:56 +08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-18 11:06:04 +08:00
|
|
|
@override
|
|
|
|
Future<void> dispose() async {
|
|
|
|
rowNotifier.dispose();
|
|
|
|
_cardBloc.close();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CellColumn extends StatelessWidget {
|
|
|
|
final String groupId;
|
|
|
|
final BoardCellBuilder cellBuilder;
|
|
|
|
final EditableRowNotifier rowNotifier;
|
|
|
|
final List<BoardCellEquatable> cells;
|
|
|
|
const _CellColumn({
|
|
|
|
required this.groupId,
|
|
|
|
required this.rowNotifier,
|
|
|
|
required this.cellBuilder,
|
|
|
|
required this.cells,
|
|
|
|
Key? key,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: _makeCells(context, cells),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-26 16:07:51 +08:00
|
|
|
List<Widget> _makeCells(
|
|
|
|
BuildContext context,
|
2022-09-18 11:06:04 +08:00
|
|
|
List<BoardCellEquatable> cells,
|
2022-08-26 16:07:51 +08:00
|
|
|
) {
|
2022-08-29 15:42:08 +08:00
|
|
|
final List<Widget> children = [];
|
2022-09-18 11:06:04 +08:00
|
|
|
// Remove all the cell listeners.
|
|
|
|
rowNotifier.unbind();
|
|
|
|
|
2022-08-29 15:42:08 +08:00
|
|
|
cells.asMap().forEach(
|
2022-09-18 11:06:04 +08:00
|
|
|
(int index, BoardCellEquatable cell) {
|
|
|
|
final isEditing = index == 0 ? rowNotifier.isEditing.value : false;
|
|
|
|
final cellNotifier = EditableCellNotifier(isEditing: isEditing);
|
|
|
|
|
2022-09-06 21:10:41 +08:00
|
|
|
if (index == 0) {
|
2022-09-07 11:33:42 +08:00
|
|
|
// Only use the first cell to receive user's input when click the edit
|
|
|
|
// button
|
2022-09-18 11:06:04 +08:00
|
|
|
rowNotifier.bindCell(cell.identifier, cellNotifier);
|
2022-09-06 21:10:41 +08:00
|
|
|
}
|
|
|
|
|
2022-09-18 11:06:04 +08:00
|
|
|
final child = Padding(
|
|
|
|
key: cell.identifier.key(),
|
2022-08-31 20:21:06 +08:00
|
|
|
padding: const EdgeInsets.only(left: 4, right: 4),
|
2022-09-18 11:06:04 +08:00
|
|
|
child: cellBuilder.buildCell(
|
|
|
|
groupId,
|
|
|
|
cell.identifier,
|
|
|
|
cellNotifier,
|
|
|
|
),
|
2022-08-31 20:21:06 +08:00
|
|
|
);
|
2022-08-31 20:57:34 +08:00
|
|
|
|
2022-08-29 16:32:20 +08:00
|
|
|
children.add(child);
|
2022-08-12 20:10:56 +08:00
|
|
|
},
|
2022-08-29 15:42:08 +08:00
|
|
|
);
|
|
|
|
return children;
|
2022-08-10 17:59:28 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-13 11:51:26 +08:00
|
|
|
|
|
|
|
class _CardMoreOption extends StatelessWidget with CardAccessory {
|
|
|
|
const _CardMoreOption({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-30 20:54:11 +08:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(3.0),
|
|
|
|
child:
|
|
|
|
svgWidget('grid/details', color: context.read<AppTheme>().iconColor),
|
|
|
|
);
|
2022-08-13 11:51:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onTap(BuildContext context) {
|
2022-08-16 17:36:39 +08:00
|
|
|
GridRowActionSheet(
|
|
|
|
rowData: context.read<BoardCardBloc>().rowInfo(),
|
|
|
|
).show(context, direction: AnchorDirection.bottomWithCenterAligned);
|
2022-08-13 11:51:26 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 20:54:11 +08:00
|
|
|
|
|
|
|
class _CardEditOption extends StatelessWidget with CardAccessory {
|
2022-09-18 11:06:04 +08:00
|
|
|
final EditableRowNotifier rowNotifier;
|
2022-08-30 20:54:11 +08:00
|
|
|
const _CardEditOption({
|
2022-09-18 11:06:04 +08:00
|
|
|
required this.rowNotifier,
|
2022-08-30 20:54:11 +08:00
|
|
|
Key? key,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(3.0),
|
|
|
|
child: svgWidget(
|
|
|
|
'editor/edit',
|
|
|
|
color: context.read<AppTheme>().iconColor,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onTap(BuildContext context) {
|
2022-09-18 11:06:04 +08:00
|
|
|
rowNotifier.becomeFirstResponder();
|
2022-08-30 20:54:11 +08:00
|
|
|
}
|
|
|
|
}
|