refactor: GridCellRequestFocusNotifier only accepts single listener

This commit is contained in:
appflowy 2022-04-30 09:29:43 +08:00
parent 371f75343c
commit 6b5126b12e
6 changed files with 41 additions and 34 deletions

View File

@ -57,6 +57,24 @@ abstract class GridCellWidget extends HoverWidget {
} }
class GridCellRequestFocusNotifier extends ChangeNotifier { class GridCellRequestFocusNotifier extends ChangeNotifier {
VoidCallback? _listener;
@override
void addListener(VoidCallback listener) {
if (_listener != null) {
removeListener(_listener!);
}
_listener = listener;
super.addListener(listener);
}
void removeAllListener() {
if (_listener != null) {
removeListener(_listener!);
}
}
void notify() { void notify() {
notifyListeners(); notifyListeners();
} }

View File

@ -57,6 +57,7 @@ class _CheckboxCellState extends State<CheckboxCell> {
@override @override
Future<void> dispose() async { Future<void> dispose() async {
widget.requestFocus.removeAllListener();
_cellBloc.close(); _cellBloc.close();
super.dispose(); super.dispose();
} }

View File

@ -23,7 +23,6 @@ class _NumberCellState extends State<NumberCell> {
late NumberCellBloc _cellBloc; late NumberCellBloc _cellBloc;
late TextEditingController _controller; late TextEditingController _controller;
late FocusNode _focusNode; late FocusNode _focusNode;
VoidCallback? _focusListener;
Timer? _delayOperation; Timer? _delayOperation;
@override @override
@ -70,9 +69,7 @@ class _NumberCellState extends State<NumberCell> {
@override @override
Future<void> dispose() async { Future<void> dispose() async {
if (_focusListener != null) { widget.requestFocus.removeAllListener();
widget.requestFocus.removeListener(_focusListener!);
}
_delayOperation?.cancel(); _delayOperation?.cancel();
_cellBloc.close(); _cellBloc.close();
_focusNode.dispose(); _focusNode.dispose();
@ -96,17 +93,10 @@ class _NumberCellState extends State<NumberCell> {
} }
void _listenCellRequestFocus(BuildContext context) { void _listenCellRequestFocus(BuildContext context) {
if (_focusListener != null) { widget.requestFocus.addListener(() {
widget.requestFocus.removeListener(_focusListener!);
}
focusListener() {
if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) { if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) {
FocusScope.of(context).requestFocus(_focusNode); FocusScope.of(context).requestFocus(_focusNode);
} }
} });
_focusListener = focusListener;
widget.requestFocus.addListener(focusListener);
} }
} }

View File

@ -157,7 +157,6 @@ class _SelectOptionCell extends StatelessWidget {
); );
} }
final cellContext = cellContextBuilder.build() as GridSelectOptionCellContext;
return Stack( return Stack(
alignment: AlignmentDirectional.center, alignment: AlignmentDirectional.center,
fit: StackFit.expand, fit: StackFit.expand,
@ -166,6 +165,7 @@ class _SelectOptionCell extends StatelessWidget {
InkWell( InkWell(
onTap: () { onTap: () {
onFocus(true); onFocus(true);
final cellContext = cellContextBuilder.build() as GridSelectOptionCellContext;
SelectOptionCellEditor.show(context, cellContext, () => onFocus(false)); SelectOptionCellEditor.show(context, cellContext, () => onFocus(false));
}, },
), ),

View File

@ -36,7 +36,8 @@ class _GridTextCellState extends State<GridTextCell> {
late TextCellBloc _cellBloc; late TextCellBloc _cellBloc;
late TextEditingController _controller; late TextEditingController _controller;
late FocusNode _focusNode; late FocusNode _focusNode;
VoidCallback? _focusListener;
VoidCallback? _focusNodeListener;
Timer? _delayOperation; Timer? _delayOperation;
@override @override
@ -87,32 +88,29 @@ class _GridTextCellState extends State<GridTextCell> {
); );
} }
void _listenCellRequestFocus(BuildContext context) {
if (_focusListener != null) {
widget.requestFocus.removeListener(_focusListener!);
}
focusListener() {
if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) {
FocusScope.of(context).requestFocus(_focusNode);
}
}
_focusListener = focusListener;
widget.requestFocus.addListener(focusListener);
}
@override @override
Future<void> dispose() async { Future<void> dispose() async {
if (_focusListener != null) { widget.requestFocus.removeAllListener();
widget.requestFocus.removeListener(_focusListener!);
}
_delayOperation?.cancel(); _delayOperation?.cancel();
_cellBloc.close(); _cellBloc.close();
_focusNode.dispose(); _focusNode.dispose();
super.dispose(); super.dispose();
} }
@override
void didUpdateWidget(covariant GridTextCell oldWidget) {
// TODO: implement didUpdateWidget
super.didUpdateWidget(oldWidget);
}
void _listenCellRequestFocus(BuildContext context) {
widget.requestFocus.addListener(() {
if (_focusNode.hasFocus == false && _focusNode.canRequestFocus) {
FocusScope.of(context).requestFocus(_focusNode);
}
});
}
Future<void> focusChanged() async { Future<void> focusChanged() async {
if (mounted) { if (mounted) {
_delayOperation?.cancel(); _delayOperation?.cancel();

View File

@ -153,7 +153,7 @@ class _RowCells extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<RowBloc, RowState>( return BlocBuilder<RowBloc, RowState>(
buildWhen: (previous, current) => previous.cellDataMap.length != current.cellDataMap.length, buildWhen: (previous, current) => previous.cellDataMap != current.cellDataMap,
builder: (context, state) { builder: (context, state) {
return IntrinsicHeight( return IntrinsicHeight(
child: Row( child: Row(