@@ -366,16 +366,11 @@ class _CodeFieldState extends State<CodeField> {
366366
367367 textStyle = defaultTextStyle.merge (widget.textStyle);
368368
369- // Get Chrome-specific text selection fixes
370- final chromeTextSelectionFixes = getChromeTextSelectionFixes ();
371- final useCustomSelectionStyle = chromeTextSelectionFixes['useCustomSelectionStyle' ] as bool ;
372-
373- // Adjust textStyle to have consistent line height and Chrome-specific fixes
369+ // Adjust textStyle to have consistent line height
370+ // This is a key fix for Chrome selection issues
374371 final adjustedTextStyle = textStyle.copyWith (
375- height: getLineHeight (),
376- leadingDistribution: useCustomSelectionStyle
377- ? chromeTextSelectionFixes['leadingDistribution' ] as TextLeadingDistribution
378- : TextLeadingDistribution .proportional,
372+ height: getLineHeight (), // Use the function instead of constant
373+ leadingDistribution: TextLeadingDistribution .even, // Added for consistent text baselines
379374 );
380375
381376 final codeField = TextField (
@@ -389,17 +384,15 @@ class _CodeFieldState extends State<CodeField> {
389384 scrollController: _codeScroll,
390385 keyboardType: widget.keyboardType,
391386 selectionControls: kIsWeb ? DesktopTextSelectionControls () : MaterialTextSelectionControls (),
392- decoration: InputDecoration (
387+ decoration: const InputDecoration (
393388 isCollapsed: true ,
394- // Add vertical padding - adjust based on selection fixes
395- contentPadding: EdgeInsets .symmetric (
396- vertical: useCustomSelectionStyle ? 14.0 : 16.0 ,
397- ),
389+ // Add more vertical padding to help with line selection
390+ contentPadding: EdgeInsets .symmetric (vertical: 16 ),
398391 disabledBorder: InputBorder .none,
399392 border: InputBorder .none,
400393 focusedBorder: InputBorder .none,
401394 ),
402- textAlignVertical: TextAlignVertical .top,
395+ textAlignVertical: TextAlignVertical .top, // Align text at the top for better matching
403396 cursorColor: widget.cursorColor ?? defaultTextStyle.color,
404397 cursorHeight: widget.cursorHeight,
405398 autocorrect: false ,
@@ -421,7 +414,14 @@ class _CodeFieldState extends State<CodeField> {
421414
422415 final editingField = Theme (
423416 data: Theme .of (context).copyWith (
424- textSelectionTheme: _getTextSelectionTheme (context),
417+ textSelectionTheme: widget.textSelectionTheme ??
418+ TextSelectionThemeData (
419+ // Use a more pronounced and distinguishable selection color
420+ // This helps with Chrome's selection rendering
421+ selectionColor: Theme .of (context).colorScheme.primary.withOpacity (0.5 ),
422+ cursorColor: widget.cursorColor ?? defaultTextStyle.color,
423+ selectionHandleColor: Theme .of (context).colorScheme.primary,
424+ ),
425425 ),
426426 child: LayoutBuilder (
427427 builder: (BuildContext context, BoxConstraints constraints) {
@@ -458,20 +458,14 @@ class _CodeFieldState extends State<CodeField> {
458458 final lineNumberSize = textStyle.fontSize;
459459 final lineNumberColor = widget.gutterStyle.textStyle? .color ?? textStyle.color? .withOpacity (.5 );
460460
461- // Get Chrome-specific text selection fixes
462- final chromeTextSelectionFixes = getChromeTextSelectionFixes ();
463- final useCustomSelectionStyle = chromeTextSelectionFixes['useCustomSelectionStyle' ] as bool ;
464-
465461 // Ensure same text style properties for consistent line height
466462 final lineNumberTextStyle = (widget.gutterStyle.textStyle ?? textStyle).copyWith (
467463 color: lineNumberColor,
468464 fontFamily: textStyle.fontFamily,
469465 fontSize: lineNumberSize,
470- height: getLineHeight (),
466+ height: getLineHeight (), // Use the function instead of constant
471467 // Add additional properties to ensure metrics consistency
472- leadingDistribution: useCustomSelectionStyle
473- ? chromeTextSelectionFixes['leadingDistribution' ] as TextLeadingDistribution
474- : TextLeadingDistribution .proportional,
468+ leadingDistribution: TextLeadingDistribution .even,
475469 );
476470
477471 final gutterStyle = widget.gutterStyle.copyWith (
@@ -555,11 +549,9 @@ class _CodeFieldState extends State<CodeField> {
555549 return renderBox.localToGlobal (Offset .zero);
556550 }
557551
558- // Get Chrome-specific text selection fixes
559- final chromeTextSelectionFixes = getChromeTextSelectionFixes ();
560- final useCustomSelectionStyle = chromeTextSelectionFixes['useCustomSelectionStyle' ] as bool ;
561- final selectionHeightFix = useCustomSelectionStyle ?
562- (chromeTextSelectionFixes['selectionHeightFix' ] as bool ? ?? false ) : false ;
552+ // Instead of calculating line position based on newlines,
553+ // we'll use the TextPainter's getOffsetForCaret method which
554+ // properly accounts for text wrapping
563555
564556 // Create a text painter for the entire text
565557 final fullTextPainter = TextPainter (
@@ -585,21 +577,15 @@ class _CodeFieldState extends State<CodeField> {
585577
586578 // Apply scroll offsets
587579 final double adjustedX = rawOffset.dx - horizontalScrollOffset;
588- double adjustedY = rawOffset.dy - scrollY;
589-
590- // Apply Chrome-specific vertical adjustment if needed
591- if (selectionHeightFix) {
592- // This small offset helps Chrome render selections correctly without changing line height
593- adjustedY += 2.0 ;
594- }
580+ final double adjustedY = rawOffset.dy - scrollY;
595581
596582 // Convert to global coordinates
597583 return renderBox.localToGlobal (Offset (adjustedX, adjustedY));
598584 }
599585
600586 double getLineHeight () {
601- // Use a consistent line height that looks good ( not the special Chrome value)
602- return textStyle.height ?? 1.2 ;
587+ // Default line height multiple if not specified in the style
588+ return getChromeLineHeight () ?? ( textStyle.height ?? 1.2 ) ;
603589 }
604590
605591 void _onPopupStateChanged () {
@@ -654,27 +640,4 @@ class _CodeFieldState extends State<CodeField> {
654640 }
655641 }
656642 }
657-
658- TextSelectionThemeData _getTextSelectionTheme (BuildContext context) {
659- final chromeTextSelectionFixes = getChromeTextSelectionFixes ();
660- final useCustomSelectionStyle = chromeTextSelectionFixes['useCustomSelectionStyle' ] as bool ;
661-
662- // Create a more optimized selection style for Chrome
663- if (useCustomSelectionStyle) {
664- return widget.textSelectionTheme ??
665- TextSelectionThemeData (
666- selectionColor: Theme .of (context).colorScheme.primary.withOpacity (0.3 ),
667- cursorColor: widget.cursorColor ?? textStyle.color,
668- selectionHandleColor: Theme .of (context).colorScheme.primary,
669- );
670- }
671-
672- // Default selection style for other browsers
673- return widget.textSelectionTheme ??
674- TextSelectionThemeData (
675- selectionColor: Theme .of (context).colorScheme.primary.withOpacity (0.5 ),
676- cursorColor: widget.cursorColor ?? textStyle.color,
677- selectionHandleColor: Theme .of (context).colorScheme.primary,
678- );
679- }
680643}
0 commit comments