Skip to content

Fix: Enable typing in search field when semantics are enabled#37

Merged
cedvdb merged 4 commits intocedvdb:mainfrom
WillCallahan:fix/semantics
Dec 24, 2025
Merged

Fix: Enable typing in search field when semantics are enabled#37
cedvdb merged 4 commits intocedvdb:mainfrom
WillCallahan:fix/semantics

Conversation

@WillCallahan
Copy link
Contributor

@WillCallahan WillCallahan commented Dec 23, 2025

This PR fixes a critical accessibility bug that made the search field in CountrySelector unusable when a screen reader (like VoiceOver or TalkBack) is active. The TextField for search was not focusable, preventing text input.

How to Reproduce the Bug

To understand the bug with the previous code, you can run a widget test with semantics explicitly enabled. This mimics a user enabling a screen reader. The TextField would not be part of the semantics tree and thus would be completely inaccessible to a user relying on assistive technology.

The following code demonstrates how to enable semantics for testing. On a device, this is equivalent to turning on VoiceOver or TalkBack.

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_country_selector/flutter_country_selector.dart';

void main() {
  testWidgets('Demonstrates that the search field is not focusable', (WidgetTester tester) async {
    // Manually enable semantics, equivalent to a user turning on a screen reader.
    WidgetsFlutterBinding.ensureInitialized();
    SemanticsBinding.instance.ensureSemantics();

    await tester.pumpWidget(
      MaterialApp(
        home: CountrySelector(
          onCountrySelected: (_) {},
        ),
      ),
    );

    // In the broken state, the search TextField is not in the semantics tree.
    // Any attempt to find it via semantics nodes or have an accessibility
    // tool focus it would fail.
    //
    // After the fix, the TextField is present in the tree and can be focused.
  });
}

The Problem

The root cause was the TextField within the _SearchBox widget being wrapped by an ExcludeSemantics widget. This explicitly removed the search field and its children from the semantics tree, making it invisible and unusable to accessibility services.

The Solution

The fix is to remove the ExcludeSemantics and its parent Semantics widget that were wrapping the TextField. The TextField widget itself is already configured to handle its own semantics correctly, using the provided InputDecoration.hintText as its accessibility label. This change allows screen readers to discover and interact with the search field as intended.

Testing Strategy

Automated testing for this specific focus issue proved to be unreliable. The standard tester.enterText method does not use the semantics tree to find widgets, so it passed even when the bug was present. Attempts to programmatically check the focus status (via SemanticsFlag.isFocused) were inconsistent across different test runs, especially when the selector was presented as a modal sheet.

To provide a robust way to verify the fix, a new manual test case has been added in test/manual_semantics_test.dart. This test launches the widget and pauses, allowing a developer to perform a real-world check.

To run the manual test:

  1. Execute flutter test test/manual_semantics_test.dart in your terminal.
  2. The test will open the selector and then pause execution.
  3. Enable your platform's screen reader (e.g., VoiceOver on macOS/iOS, TalkBack on Android).
  4. Navigate through the elements on the screen.
  5. Confirm that you can swipe to the search field, have it announced, double-tap to focus it, and begin typing.

Related Issues

Supporting Documentation

Removed conflicting Semantics and ExcludeSemantics widgets from the SearchBox to allow the TextField to correctly expose its own accessibility semantics. This resolves an issue where users could not type into the search field when screen readers or other accessibility tools were active.
@WillCallahan WillCallahan changed the title Fix: Enable typing in search field when semantics are enabled Fix: Use default Flutter Semantics to fix focus issue Dec 23, 2025
@WillCallahan WillCallahan changed the title Fix: Use default Flutter Semantics to fix focus issue Fix: Enable typing in search field when semantics are enabled Dec 23, 2025
@WillCallahan WillCallahan marked this pull request as ready for review December 23, 2025 16:59
@cedvdb
Copy link
Owner

cedvdb commented Dec 23, 2025

thanks

Regarding the test:

Since we are not doing anything semantic related anymore, we let flutter handle it, the test file seems unnecessary, (if there is an issue, it is a flutter issue_. People are lazy, and this will quickly become ghost code because it's not automated, so I'm down to just remove that file.

We could check include node etc, but again, that would be testing flutter, not anything we do here.

@WillCallahan
Copy link
Contributor Author

Will do; I'll remove that

@cedvdb cedvdb merged commit ca1e3b4 into cedvdb:main Dec 24, 2025
@cedvdb
Copy link
Owner

cedvdb commented Dec 24, 2025

release failed because there was no version update in the changelog. I'm going to revert, can you resubmit with an entry in the changelog for this version so that the deploy pipeline can do its job ?

cedvdb added a commit that referenced this pull request Dec 24, 2025
cedvdb added a commit that referenced this pull request Dec 24, 2025
cedvdb added a commit that referenced this pull request Dec 28, 2025
cedvdb added a commit that referenced this pull request Dec 28, 2025
* Reapply "Fix: Enable typing in search field when semantics are enabled (#37)" (#38)

This reverts commit 93ec8ed.

* add changelog entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Semantics not working

2 participants