chore: handle edit state

This commit is contained in:
appflowy 2022-09-06 21:10:41 +08:00
parent 86e2091ccf
commit 951206db71
4 changed files with 31 additions and 19 deletions

View File

@ -1,5 +1,6 @@
import 'package:app_flowy/plugins/grid/application/prelude.dart';
import 'package:flowy_infra/notifier.dart';
import 'package:flutter/material.dart';
abstract class FocusableBoardCell {
set becomeFocus(bool isFocus);
@ -10,7 +11,16 @@ class EditableCellNotifier {
final Notifier resignFirstResponder = Notifier();
EditableCellNotifier();
final ValueNotifier<bool> isCellEditing;
EditableCellNotifier({bool isEditing = false})
: isCellEditing = ValueNotifier(isEditing);
void dispose() {
becomeFirstResponder.dispose();
resignFirstResponder.dispose();
isCellEditing.dispose();
}
}
class EditableRowNotifier {
@ -20,6 +30,11 @@ class EditableRowNotifier {
GridCellIdentifier cellIdentifier,
EditableCellNotifier notifier,
) {
final id = EditableCellId.from(cellIdentifier);
_cells[id]?.dispose();
notifier.isCellEditing.addListener(() {});
_cells[EditableCellId.from(cellIdentifier)] = notifier;
}
@ -36,6 +51,9 @@ class EditableRowNotifier {
}
void clear() {
for (final notifier in _cells.values) {
notifier.dispose();
}
_cells.clear();
}

View File

@ -9,7 +9,6 @@ import 'define.dart';
class BoardTextCell extends StatefulWidget with EditableCell {
final String groupId;
final bool isFocus;
@override
final EditableCellNotifier? editableNotifier;
final GridCellControllerBuilder cellControllerBuilder;
@ -18,7 +17,6 @@ class BoardTextCell extends StatefulWidget with EditableCell {
required this.groupId,
required this.cellControllerBuilder,
this.editableNotifier,
this.isFocus = false,
Key? key,
}) : super(key: key);
@ -39,21 +37,16 @@ class _BoardTextCellState extends State<BoardTextCell> {
_cellBloc = BoardTextCellBloc(cellController: cellController)
..add(const BoardTextCellEvent.initial());
_controller = TextEditingController(text: _cellBloc.state.content);
focusWhenInit = widget.isFocus;
if (widget.isFocus) {
focusWhenInit = widget.editableNotifier?.isCellEditing.value ?? false;
if (focusWhenInit) {
focusNode.requestFocus();
}
focusNode.addListener(() {
if (!focusNode.hasFocus) {
focusWhenInit = false;
widget.editableNotifier?.isCellEditing.value = false;
_cellBloc.add(const BoardTextCellEvent.enableEdit(false));
if (focusWhenInit) {
setState(() {
focusWhenInit = false;
});
}
}
});
_bindEditableNotifier();

View File

@ -96,17 +96,20 @@ class _BoardCardState extends State<BoardCard> {
rowNotifier.clear();
cells.asMap().forEach(
(int index, GridCellIdentifier cellId) {
final cellNotifier = EditableCellNotifier();
EditableCellNotifier cellNotifier;
if (index == 0) {
cellNotifier = EditableCellNotifier(isEditing: widget.isEditing);
rowNotifier.insertCell(cellId, cellNotifier);
} else {
cellNotifier = EditableCellNotifier();
}
Widget child = widget.cellBuilder.buildCell(
widget.groupId,
cellId,
index == 0 ? widget.isEditing : false,
cellNotifier,
);
if (index == 0) {
rowNotifier.insertCell(cellId, cellNotifier);
}
child = Padding(
key: cellId.key(),
padding: const EdgeInsets.only(left: 4, right: 4),

View File

@ -23,7 +23,6 @@ class BoardCellBuilder {
Widget buildCell(
String groupId,
GridCellIdentifier cellId,
bool isEditing,
EditableCellNotifier cellNotifier,
) {
final cellControllerBuilder = GridCellControllerBuilder(
@ -69,7 +68,6 @@ class BoardCellBuilder {
return BoardTextCell(
groupId: groupId,
cellControllerBuilder: cellControllerBuilder,
isFocus: isEditing,
editableNotifier: cellNotifier,
key: key,
);