11import 'package:app_flowy/plugins/board/application/card/card_bloc.dart' ;
22import 'package:app_flowy/plugins/board/application/card/card_data_controller.dart' ;
3- import 'package:app_flowy/plugins/grid/application/cell/cell_service/cell_service.dart' ;
43import 'package:app_flowy/plugins/grid/presentation/widgets/row/row_action_sheet.dart' ;
54import 'package:flowy_infra/image.dart' ;
65import 'package:flowy_infra/theme.dart' ;
@@ -64,83 +63,103 @@ class _BoardCardState extends State<BoardCard> {
6463 value: _cardBloc,
6564 child: BlocBuilder <BoardCardBloc , BoardCardState >(
6665 buildWhen: (previous, current) {
66+ // Rebuild when:
67+ // 1.If the lenght of the cells is not the same
68+ // 2.isEditing changed
6769 if (previous.cells.length != current.cells.length ||
6870 previous.isEditing != current.isEditing) {
6971 return true ;
7072 }
73+
74+ // 3.Compare the content of the cells. The cells consisits of
75+ // list of [BoardCellEquatable] that extends the [Equatable].
7176 return ! listEquals (previous.cells, current.cells);
7277 },
7378 builder: (context, state) {
7479 return BoardCardContainer (
7580 buildAccessoryWhen: () => state.isEditing == false ,
7681 accessoryBuilder: (context) {
7782 return [
78- _CardEditOption (
79- startEditing: () => rowNotifier.becomeFirstResponder (),
80- ),
83+ _CardEditOption (rowNotifier: rowNotifier),
8184 const _CardMoreOption (),
8285 ];
8386 },
84- onTap: (context) {
85- widget.openCard (context);
86- },
87- child: Column (
88- mainAxisSize: MainAxisSize .min,
89- children: _makeCells (
90- context,
91- state.cells.map ((cell) => cell.identifier).toList (),
92- ),
87+ onTap: (context) => widget.openCard (context),
88+ child: _CellColumn (
89+ groupId: widget.groupId,
90+ rowNotifier: rowNotifier,
91+ cellBuilder: widget.cellBuilder,
92+ cells: state.cells,
9393 ),
9494 );
9595 },
9696 ),
9797 );
9898 }
9999
100+ @override
101+ Future <void > dispose () async {
102+ rowNotifier.dispose ();
103+ _cardBloc.close ();
104+ super .dispose ();
105+ }
106+ }
107+
108+ class _CellColumn extends StatelessWidget {
109+ final String groupId;
110+ final BoardCellBuilder cellBuilder;
111+ final EditableRowNotifier rowNotifier;
112+ final List <BoardCellEquatable > cells;
113+ const _CellColumn ({
114+ required this .groupId,
115+ required this .rowNotifier,
116+ required this .cellBuilder,
117+ required this .cells,
118+ Key ? key,
119+ }) : super (key: key);
120+
121+ @override
122+ Widget build (BuildContext context) {
123+ return Column (
124+ mainAxisSize: MainAxisSize .min,
125+ children: _makeCells (context, cells),
126+ );
127+ }
128+
100129 List <Widget > _makeCells (
101130 BuildContext context,
102- List <GridCellIdentifier > cells,
131+ List <BoardCellEquatable > cells,
103132 ) {
104133 final List <Widget > children = [];
105- rowNotifier.clear ();
134+ // Remove all the cell listeners.
135+ rowNotifier.unbind ();
136+
106137 cells.asMap ().forEach (
107- (int index, GridCellIdentifier cellId) {
108- EditableCellNotifier cellNotifier;
138+ (int index, BoardCellEquatable cell) {
139+ final isEditing = index == 0 ? rowNotifier.isEditing.value : false ;
140+ final cellNotifier = EditableCellNotifier (isEditing: isEditing);
141+
109142 if (index == 0 ) {
110143 // Only use the first cell to receive user's input when click the edit
111144 // button
112- cellNotifier = EditableCellNotifier (
113- isEditing: rowNotifier.isEditing.value,
114- );
115- rowNotifier.insertCell (cellId, cellNotifier);
116- } else {
117- cellNotifier = EditableCellNotifier ();
145+ rowNotifier.bindCell (cell.identifier, cellNotifier);
118146 }
119147
120- Widget child = widget.cellBuilder.buildCell (
121- widget.groupId,
122- cellId,
123- cellNotifier,
124- );
125-
126- child = Padding (
127- key: cellId.key (),
148+ final child = Padding (
149+ key: cell.identifier.key (),
128150 padding: const EdgeInsets .only (left: 4 , right: 4 ),
129- child: child,
151+ child: cellBuilder.buildCell (
152+ groupId,
153+ cell.identifier,
154+ cellNotifier,
155+ ),
130156 );
131157
132158 children.add (child);
133159 },
134160 );
135161 return children;
136162 }
137-
138- @override
139- Future <void > dispose () async {
140- rowNotifier.dispose ();
141- _cardBloc.close ();
142- super .dispose ();
143- }
144163}
145164
146165class _CardMoreOption extends StatelessWidget with CardAccessory {
@@ -164,9 +183,9 @@ class _CardMoreOption extends StatelessWidget with CardAccessory {
164183}
165184
166185class _CardEditOption extends StatelessWidget with CardAccessory {
167- final VoidCallback startEditing ;
186+ final EditableRowNotifier rowNotifier ;
168187 const _CardEditOption ({
169- required this .startEditing ,
188+ required this .rowNotifier ,
170189 Key ? key,
171190 }) : super (key: key);
172191
@@ -183,6 +202,6 @@ class _CardEditOption extends StatelessWidget with CardAccessory {
183202
184203 @override
185204 void onTap (BuildContext context) {
186- startEditing ();
205+ rowNotifier. becomeFirstResponder ();
187206 }
188207}
0 commit comments