Skip to content

Commit 4887885

Browse files
committed
refactor: add GridCellState and GridFocusNodeCellState
1 parent 81efee5 commit 4887885

File tree

7 files changed

+95
-104
lines changed

7 files changed

+95
-104
lines changed

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class BlankCell extends StatelessWidget {
4848
}
4949
}
5050

51-
abstract class GridCellWidget implements AccessoryWidget, CellContainerFocustable {
51+
abstract class GridCellWidget extends StatefulWidget implements AccessoryWidget, CellContainerFocustable {
52+
GridCellWidget({Key? key}) : super(key: key);
53+
5254
@override
5355
final ValueNotifier<bool> isFocus = ValueNotifier<bool>(false);
5456

@@ -59,6 +61,72 @@ abstract class GridCellWidget implements AccessoryWidget, CellContainerFocustabl
5961
final GridCellRequestBeginFocus requestBeginFocus = GridCellRequestBeginFocus();
6062
}
6163

64+
abstract class GridCellState<T extends GridCellWidget> extends State<T> {
65+
@override
66+
void initState() {
67+
widget.requestBeginFocus.setListener(() => requestBeginFocus());
68+
super.initState();
69+
}
70+
71+
@override
72+
void didUpdateWidget(covariant T oldWidget) {
73+
if (oldWidget != this) {
74+
widget.requestBeginFocus.setListener(() => requestBeginFocus());
75+
}
76+
super.didUpdateWidget(oldWidget);
77+
}
78+
79+
@override
80+
void dispose() {
81+
widget.requestBeginFocus.removeAllListener();
82+
super.dispose();
83+
}
84+
85+
void requestBeginFocus();
86+
}
87+
88+
abstract class GridFocusNodeCellState<T extends GridCellWidget> extends GridCellState<T> {
89+
SingleListenrFocusNode focusNode = SingleListenrFocusNode();
90+
91+
@override
92+
void initState() {
93+
_listenOnFocusNodeChanged();
94+
super.initState();
95+
}
96+
97+
@override
98+
void didUpdateWidget(covariant T oldWidget) {
99+
if (oldWidget != this) {
100+
_listenOnFocusNodeChanged();
101+
}
102+
super.didUpdateWidget(oldWidget);
103+
}
104+
105+
@override
106+
void dispose() {
107+
focusNode.removeAllListener();
108+
focusNode.dispose();
109+
super.dispose();
110+
}
111+
112+
@override
113+
void requestBeginFocus() {
114+
if (focusNode.hasFocus == false && focusNode.canRequestFocus) {
115+
FocusScope.of(context).requestFocus(focusNode);
116+
}
117+
}
118+
119+
void _listenOnFocusNodeChanged() {
120+
widget.isFocus.value = focusNode.hasFocus;
121+
focusNode.setListener(() {
122+
widget.isFocus.value = focusNode.hasFocus;
123+
focusChanged();
124+
});
125+
}
126+
127+
Future<void> focusChanged() async {}
128+
}
129+
62130
class GridCellRequestBeginFocus extends ChangeNotifier {
63131
VoidCallback? _listener;
64132

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/checkbox_cell.dart

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ import 'package:flutter/widgets.dart';
66
import 'package:flutter_bloc/flutter_bloc.dart';
77
import 'cell_builder.dart';
88

9-
class CheckboxCell extends StatefulWidget with GridCellWidget {
9+
class CheckboxCell extends GridCellWidget {
1010
final GridCellContextBuilder cellContextBuilder;
1111
CheckboxCell({
1212
required this.cellContextBuilder,
1313
Key? key,
1414
}) : super(key: key);
1515

1616
@override
17-
State<CheckboxCell> createState() => _CheckboxCellState();
17+
GridCellState<CheckboxCell> createState() => _CheckboxCellState();
1818
}
1919

20-
class _CheckboxCellState extends State<CheckboxCell> {
20+
class _CheckboxCellState extends GridCellState<CheckboxCell> {
2121
late CheckboxCellBloc _cellBloc;
2222

2323
@override
2424
void initState() {
2525
final cellContext = widget.cellContextBuilder.build();
2626
_cellBloc = getIt<CheckboxCellBloc>(param1: cellContext)..add(const CheckboxCellEvent.initial());
27-
_handleRequestFocus();
27+
2828
super.initState();
2929
}
3030

@@ -49,22 +49,14 @@ class _CheckboxCellState extends State<CheckboxCell> {
4949
);
5050
}
5151

52-
@override
53-
void didUpdateWidget(covariant CheckboxCell oldWidget) {
54-
_handleRequestFocus();
55-
super.didUpdateWidget(oldWidget);
56-
}
57-
5852
@override
5953
Future<void> dispose() async {
60-
widget.requestBeginFocus.removeAllListener();
6154
_cellBloc.close();
6255
super.dispose();
6356
}
6457

65-
void _handleRequestFocus() {
66-
widget.requestBeginFocus.setListener(() {
67-
_cellBloc.add(const CheckboxCellEvent.select());
68-
});
58+
@override
59+
void requestBeginFocus() {
60+
_cellBloc.add(const CheckboxCellEvent.select());
6961
}
7062
}

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/date_cell.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class GridCellDelegate {
1818
GridCellDelegate get delegate;
1919
}
2020

21-
class DateCell extends StatefulWidget with GridCellWidget {
21+
class DateCell extends GridCellWidget {
2222
final GridCellContextBuilder cellContextBuilder;
2323
late final DateCellStyle? cellStyle;
2424

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/number_cell.dart

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
77

88
import 'cell_builder.dart';
99

10-
class NumberCell extends StatefulWidget with GridCellWidget {
10+
class NumberCell extends GridCellWidget {
1111
final GridCellContextBuilder cellContextBuilder;
1212

1313
NumberCell({
@@ -16,28 +16,24 @@ class NumberCell extends StatefulWidget with GridCellWidget {
1616
}) : super(key: key);
1717

1818
@override
19-
State<NumberCell> createState() => _NumberCellState();
19+
GridFocusNodeCellState<NumberCell> createState() => _NumberCellState();
2020
}
2121

22-
class _NumberCellState extends State<NumberCell> {
22+
class _NumberCellState extends GridFocusNodeCellState<NumberCell> {
2323
late NumberCellBloc _cellBloc;
2424
late TextEditingController _controller;
25-
late SingleListenrFocusNode _focusNode;
2625
Timer? _delayOperation;
2726

2827
@override
2928
void initState() {
3029
final cellContext = widget.cellContextBuilder.build();
3130
_cellBloc = getIt<NumberCellBloc>(param1: cellContext)..add(const NumberCellEvent.initial());
3231
_controller = TextEditingController(text: contentFromState(_cellBloc.state));
33-
_focusNode = SingleListenrFocusNode();
34-
_listenOnFocusNodeChanged();
3532
super.initState();
3633
}
3734

3835
@override
3936
Widget build(BuildContext context) {
40-
_handleCellRequestFocus(context);
4137
return BlocProvider.value(
4238
value: _cellBloc,
4339
child: MultiBlocListener(
@@ -49,8 +45,8 @@ class _NumberCellState extends State<NumberCell> {
4945
],
5046
child: TextField(
5147
controller: _controller,
52-
focusNode: _focusNode,
53-
onEditingComplete: () => _focusNode.unfocus(),
48+
focusNode: focusNode,
49+
onEditingComplete: () => focusNode.unfocus(),
5450
maxLines: null,
5551
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
5652
decoration: const InputDecoration(
@@ -65,20 +61,12 @@ class _NumberCellState extends State<NumberCell> {
6561

6662
@override
6763
Future<void> dispose() async {
68-
widget.requestBeginFocus.removeAllListener();
6964
_delayOperation?.cancel();
7065
_cellBloc.close();
71-
_focusNode.removeAllListener();
72-
_focusNode.dispose();
7366
super.dispose();
7467
}
7568

7669
@override
77-
void didUpdateWidget(covariant NumberCell oldWidget) {
78-
_listenOnFocusNodeChanged();
79-
super.didUpdateWidget(oldWidget);
80-
}
81-
8270
Future<void> focusChanged() async {
8371
if (mounted) {
8472
_delayOperation?.cancel();
@@ -90,22 +78,6 @@ class _NumberCellState extends State<NumberCell> {
9078
}
9179
}
9280

93-
void _listenOnFocusNodeChanged() {
94-
widget.isFocus.value = _focusNode.hasFocus;
95-
_focusNode.setListener(() {
96-
widget.isFocus.value = _focusNode.hasFocus;
97-
focusChanged();
98-
});
99-
}
100-
101-
void _handleCellRequestFocus(BuildContext context) {
102-
widget.requestBeginFocus.setListener(() {
103-
if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) {
104-
FocusScope.of(context).requestFocus(_focusNode);
105-
}
106-
});
107-
}
108-
10981
String contentFromState(NumberCellState state) {
11082
return state.content.fold((l) => l, (r) => "");
11183
}

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/select_option_cell/select_option_cell.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SelectOptionCellStyle extends GridCellStyle {
2020
});
2121
}
2222

23-
class SingleSelectCell extends StatefulWidget with GridCellWidget {
23+
class SingleSelectCell extends GridCellWidget {
2424
final GridCellContextBuilder cellContextBuilder;
2525
late final SelectOptionCellStyle? cellStyle;
2626

@@ -74,7 +74,7 @@ class _SingleSelectCellState extends State<SingleSelectCell> {
7474
}
7575

7676
//----------------------------------------------------------------
77-
class MultiSelectCell extends StatefulWidget with GridCellWidget {
77+
class MultiSelectCell extends GridCellWidget {
7878
final GridCellContextBuilder cellContextBuilder;
7979
late final SelectOptionCellStyle? cellStyle;
8080

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/text_cell.dart

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class GridTextCellStyle extends GridCellStyle {
1313
});
1414
}
1515

16-
class GridTextCell extends StatefulWidget with GridCellWidget {
16+
class GridTextCell extends GridCellWidget {
1717
final GridCellContextBuilder cellContextBuilder;
1818
late final GridTextCellStyle? cellStyle;
1919
GridTextCell({
@@ -29,13 +29,12 @@ class GridTextCell extends StatefulWidget with GridCellWidget {
2929
}
3030

3131
@override
32-
State<GridTextCell> createState() => _GridTextCellState();
32+
GridFocusNodeCellState<GridTextCell> createState() => _GridTextCellState();
3333
}
3434

35-
class _GridTextCellState extends State<GridTextCell> {
35+
class _GridTextCellState extends GridFocusNodeCellState<GridTextCell> {
3636
late TextCellBloc _cellBloc;
3737
late TextEditingController _controller;
38-
late SingleListenrFocusNode _focusNode;
3938
Timer? _delayOperation;
4039

4140
@override
@@ -44,10 +43,6 @@ class _GridTextCellState extends State<GridTextCell> {
4443
_cellBloc = getIt<TextCellBloc>(param1: cellContext);
4544
_cellBloc.add(const TextCellEvent.initial());
4645
_controller = TextEditingController(text: _cellBloc.state.content);
47-
_focusNode = SingleListenrFocusNode();
48-
49-
_listenOnFocusNodeChanged();
50-
_listenRequestFocus(context);
5146
super.initState();
5247
}
5348

@@ -63,9 +58,9 @@ class _GridTextCellState extends State<GridTextCell> {
6358
},
6459
child: TextField(
6560
controller: _controller,
66-
focusNode: _focusNode,
61+
focusNode: focusNode,
6762
onChanged: (value) => focusChanged(),
68-
onEditingComplete: () => _focusNode.unfocus(),
63+
onEditingComplete: () => focusNode.unfocus(),
6964
maxLines: null,
7065
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
7166
decoration: InputDecoration(
@@ -81,39 +76,12 @@ class _GridTextCellState extends State<GridTextCell> {
8176

8277
@override
8378
Future<void> dispose() async {
84-
widget.requestBeginFocus.removeAllListener();
8579
_delayOperation?.cancel();
8680
_cellBloc.close();
87-
_focusNode.removeAllListener();
88-
_focusNode.dispose();
89-
9081
super.dispose();
9182
}
9283

9384
@override
94-
void didUpdateWidget(covariant GridTextCell oldWidget) {
95-
if (oldWidget != widget) {
96-
_listenOnFocusNodeChanged();
97-
}
98-
super.didUpdateWidget(oldWidget);
99-
}
100-
101-
void _listenOnFocusNodeChanged() {
102-
widget.isFocus.value = _focusNode.hasFocus;
103-
_focusNode.setListener(() {
104-
widget.isFocus.value = _focusNode.hasFocus;
105-
focusChanged();
106-
});
107-
}
108-
109-
void _listenRequestFocus(BuildContext context) {
110-
widget.requestBeginFocus.setListener(() {
111-
if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) {
112-
FocusScope.of(context).requestFocus(_focusNode);
113-
}
114-
});
115-
}
116-
11785
Future<void> focusChanged() async {
11886
if (mounted) {
11987
_delayOperation?.cancel();

0 commit comments

Comments
 (0)