Skip to content

[Web] With semantics enabled, a focused TextField loses focus when a semantics node is inserted above it #195289

Description

@nogringo

Steps to reproduce

  1. Run the sample below with flutter run -d chrome.
  2. The field has the focus. Type a few characters within 5 seconds.
  3. After 5 seconds, keep typing.

The sample calls SemanticsBinding.instance.ensureSemantics(). Without that line, everything works. An app without it gets the same bug as soon as a user activates Flutter web's hidden "Enable accessibility" button.

Expected results

The characters typed after 5 seconds appear in the field.

Actual results

When the sample inserts a semantics node above the field, the field loses the focus: its FocusNode.hasFocus turns false, document.activeElement is <body>, and nothing typed afterwards appears.

In a real app, a Tooltip is enough to trigger it. On a page pushed on a go_router ShellRoute navigator, the route's semantics node is merged into the navigator's node while it has no sibling. Hovering an IconButton with a tooltip adds the tooltip's node as a sibling, the route's node is created above the page, and the focused field of that page loses the focus. The sample replaces that setup with a Semantics(container:) that turns on after 5 seconds.

What I think happens: SemanticsObject.updateChildren moves the DOM element of the field's existing ancestor into the new node with append or insertBefore. Moving a focused element in the DOM blurs it with no relatedTarget, and the focus lands on <body>. ViewFocusBinding then reports the view as unfocused, and View parks the focus on the root scope.

Workaround: restore the focus before the engine sees the blur.

// Called once at startup, on the web only.
for (final type in ['blur', 'focusout']) {
  web.EventStreamProvider<web.FocusEvent>(type)
      .forTarget(web.window, useCapture: true)
      .listen((event) {
    final field = event.target as web.HTMLElement;
    if (field.getAttribute('data-semantics-role') != 'text-field') return;
    if (event.relatedTarget != null ||
        !web.document.hasFocus() ||
        !field.isConnected) {
      return;
    }
    event.stopImmediatePropagation();
    scheduleMicrotask(
      () => field.focus(web.FocusOptions(preventScroll: true)),
    );
  });
}

Code sample

Code sample
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SemanticsBinding.instance.ensureSemantics();
  runApp(
    const MaterialApp(
      home: Scaffold(
        body: Padding(padding: EdgeInsets.all(24), child: Sample()),
      ),
    ),
  );
}

class Sample extends StatefulWidget {
  const Sample({super.key});

  @override
  State<Sample> createState() => _SampleState();
}

class _SampleState extends State<Sample> {
  bool _container = false;

  @override
  void initState() {
    super.initState();
    // Inserts a semantics node above the field, as a tooltip showing can.
    Timer(const Duration(seconds: 5), () => setState(() => _container = true));
  }

  @override
  Widget build(BuildContext context) {
    return Semantics(
      container: _container,
      explicitChildNodes: true,
      child: const TextField(
        autofocus: true,
        decoration: InputDecoration(hintText: 'Keep typing'),
      ),
    );
  }
}

Screenshots or Video

No response

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.47.7, on macOS 27.0.1 26A434 darwin-arm64, locale en-US)
    • Flutter version 3.47.7 on channel stable at /Users/gringo/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision abaf9c5237 (2 weeks ago), 2026-10-08 09:51:21 -0700
    • Engine revision deb287481e
    • Dart version 3.13.5
    • DevTools version 2.60.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 27.0.1 26A434 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 155.0.8059.85

Activity

  1. added
    in triage
    on Oct 26, 2026
  2. suryaKommana2662 commented on Oct 26, 2026

    @suryaKommana2662
    Contributor

    @nogringo Thanks for the report. I was able to reproduce the issue on Flutter Web using Chrome. After the semantics node is inserted above the focused TextField, the field loses focus. Routing this to the accessibility team for further investigation.

  3. added
    a: text input
    a: accessibility
    has reproducible steps
    workaround available
    fyi-web
    team-accessibility
    and removed
    in triage
    on Oct 26, 2026
  4. flutter-zl commented on Oct 28, 2026

    @flutter-zl
    Contributor
    Reproduced on Chrome with a recent master build.
    
    Demo: https://flutter-demo-83-before.web.app
    
    1. Click "Start". The field gets focus and a 5 second countdown starts.
    2. Keep typing through the countdown.
    
    When the `Semantics(container:)` above the field turns on, `FocusNode.hasFocus` becomes false, `document.activeElement` becomes `BODY`, and typing stops. The page shows both values live. Click "Start"
    again to repeat.
    
  5. added
    triaged-web
    P2
    on Oct 28, 2026
  6. removed
    fyi-web
    triaged-web
    on Oct 28, 2026
  7. flutter-triage-bot commented on Oct 28, 2026

    @flutter-triage-bot

    The triaged-web label is irrelevant if there is no team-web label or fyi-web label.

  8. added
    team-web
    triaged-web
    on Oct 28, 2026
  9. removed
    team-web
    triaged-web
    team-accessibility
    on Oct 28, 2026
  10. flutter-triage-bot commented on Oct 28, 2026

    @flutter-triage-bot

    Issue is assigned to multiple teams (web, accessibility). Please ensure the issue has only one team-* label at a time. Use fyi-* labels to have another team look at the issue without reassigning it.

  11. added
    triaged-web
    team-web
    on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2a: accessibilitya: text inputhas reproducible stepsteam-webtriaged-webworkaround available

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions