Skip to content

Commit

Permalink
fix: focus node doesn't work on mobile (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Jun 21, 2023
1 parent 4f83b6f commit 5b2ea64
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,18 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
);

// clear the selection when the focus is lost.
if (PlatformExtension.isDesktop && !focusNode.hasFocus) {
if (keepEditorFocusNotifier.value > 0) {
return;
if (!focusNode.hasFocus) {
if (PlatformExtension.isDesktop) {
if (keepEditorFocusNotifier.value > 0) {
return;
}
}
final children =
WidgetsBinding.instance.focusManager.primaryFocus?.children;
if (children != null && !children.contains(focusNode)) {
editorState.selection = null;
}
textInputService.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ class _AutoScrollableWidgetState extends State<AutoScrollableWidget> {
builder: builder,
);
} else {
return SingleChildScrollView(
controller: widget.scrollController,
child: Builder(
builder: builder,
return LayoutBuilder(
builder: (context, viewportConstraints) => SingleChildScrollView(
controller: widget.scrollController,
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: viewportConstraints.maxHeight,
),
child: Builder(
builder: builder,
),
),
),
);
}
Expand Down
72 changes: 72 additions & 0 deletions test/customer/text_field_and_editor_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() async {
// column
// - text field
// - editor

// 1. When the text field is focused, the editor's cursor should be disabled.
// 2. When the editor is focused, the text field's cursor should be disabled.
// 3. Tapping a non-first line of the editor should still allow the editor to grab focus.
testWidgets('text field + editor', (tester) async {
final widget = TextFieldAndEditor();
await tester.pumpWidget(widget);
await tester.pumpAndSettle();

final textField = find.byType(TextField);
await tester.tap(textField);
await tester.pumpAndSettle();
expect(widget.focusNode.hasFocus, true);
expect(widget.editorFocusNode.hasFocus, false);

final editor = find.byType(AppFlowyEditor);
await tester.tapAt(tester.getCenter(editor));
await tester.pumpAndSettle();
expect(widget.focusNode.hasFocus, false);
expect(widget.editorFocusNode.hasFocus, true);
});
}

class TextFieldAndEditor extends StatelessWidget {
TextFieldAndEditor({
super.key,
});

final controller = TextEditingController();
final focusNode = FocusNode();
final editorFocusNode = FocusNode();

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextField(
controller: controller,
focusNode: focusNode,
),
Expanded(
child: Container(
width: 500,
decoration: BoxDecoration(
border: Border.all(color: Colors.blue),
),
child: AppFlowyEditor.standard(
focusNode: editorFocusNode,
editorState: EditorState.blank(),
editorStyle: const EditorStyle.mobile(),
),
),
)
],
),
),
),
);
}
}

0 comments on commit 5b2ea64

Please sign in to comment.