alexeyinkin commented on code in PR #24674:
URL: https://github.com/apache/beam/pull/24674#discussion_r1049709123


##########
playground/frontend/playground_components/lib/src/widgets/snippet_editor.dart:
##########
@@ -16,31 +16,120 @@
  * limitations under the License.
  */
 
-import 'package:flutter/widgets.dart';
+import 'dart:math';
+
+import 'package:flutter/material.dart';
+import 'package:flutter/scheduler.dart';
+import 'package:flutter_code_editor/flutter_code_editor.dart';
 
 import '../controllers/snippet_editing_controller.dart';
-import 'editor_textarea.dart';
+import '../theme/theme.dart';
 
-class SnippetEditor extends StatelessWidget {
+class SnippetEditor extends StatefulWidget {
   final SnippetEditingController controller;
   final bool isEditable;
-  final bool goToContextLine;
 
-  const SnippetEditor({
+  SnippetEditor({
     required this.controller,
     required this.isEditable,
-    required this.goToContextLine,
-  });
+  }) : super(
+    // When the example is changed, will scroll to the context line again.
+    key: ValueKey(controller.selectedExample),
+  );
+
+  @override
+  State<SnippetEditor> createState() => _SnippetEditorState();
+}
+
+class _SnippetEditorState extends State<SnippetEditor> {
+  bool _didAutoFocus = false;
+  final _focusNode = FocusNode();
+  final _scrollController = ScrollController();
+
+  @override
+  void didChangeDependencies() {
+    super.didChangeDependencies();
+
+    if (!_didAutoFocus) {
+      _didAutoFocus = true;
+      SchedulerBinding.instance.addPostFrameCallback((_) {
+        if (mounted) {
+          _scrollSoCursorIsOnTop();
+        }
+      });
+    }
+  }
+
+  void _scrollSoCursorIsOnTop() {
+    _focusNode.requestFocus();
+
+    final position = max(widget.controller.codeController.selection.start, 0);
+    final characterOffset = _getLastCharacterOffset(
+      text: widget.controller.codeController.text.substring(0, position),
+      style: kLightTheme.extension<BeamThemeExtension>()!.codeRootStyle,
+    );
+
+    _scrollController.jumpTo(
+      min(
+        characterOffset.dy,
+        _scrollController.position.maxScrollExtent,
+      ),
+    );
+  }
+
+  @override
+  void dispose() {
+    _focusNode.dispose();
+    super.dispose();
+  }
 
   @override
   Widget build(BuildContext context) {
-    return EditorTextArea(
-      codeController: controller.codeController,
-      sdk: controller.sdk,
-      enabled: !(controller.selectedExample?.isMultiFile ?? false),
-      example: controller.selectedExample,
-      isEditable: isEditable,
-      goToContextLine: goToContextLine,
+    final ext = Theme.of(context).extension<BeamThemeExtension>()!;
+    final isMultifile = widget.controller.selectedExample?.isMultiFile ?? 
false;
+    final isEnabled = widget.isEditable && !isMultifile;
+
+    return Semantics(
+      container: true,
+      textField: true,
+      multiline: true,
+      enabled: isEnabled,
+      readOnly: isEnabled,
+      label: 'widgets.codeEditor.label',
+      child: FocusScope(
+        node: FocusScopeNode(canRequestFocus: isEnabled),
+        child: CodeTheme(
+          data: ext.codeTheme,
+          child: Container(

Review Comment:
   It requires non-nullable color, ours is nullable.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to