Skip to content

SearchAnchor.bar, onSubmit, doesn't lose focus, leading to a unintended .pop #178719

@lucavenir

Description

@lucavenir

Follows #177454

Steps to reproduce

  1. Click the search bar (by tapping it, you focus it)
  2. See the suggestions appear: Example 1 and Example 2 appear
  3. Type exa in the search bar
  4. Press Enter (submit)
  5. You'll see focus is not lost, and indeed
  6. Press Enter again

Expected results

Pressing enter loses focus, thus pressing Enter again (step 6) is not allowed; therefore, there should be no side effects.

Actual results

The current view is popped, meaning that _controller.closeView(value) is evaluated again; probably, such evaluation leads to a context.pop.

Code sample

Code sample
import "package:flutter/material.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";

void main() {
  runApp(const ProviderScope(child: MySearchBarPage()));
}

class MySearchBarPage extends ConsumerWidget {
  const MySearchBarPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text("My Search Bar Page"),
        ),
        body: const Center(
          child: MySearchAnchorBar(),
        ),
      ),
    );
  }
}

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

  @override
  State<MySearchAnchorBar> createState() => _MySearchAnchorBarState();
}

class _MySearchAnchorBarState extends State<MySearchAnchorBar> {
  late final SearchController _controller;

  @override
  void initState() {
    super.initState();
    _controller = SearchController();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SearchAnchor.bar(
      searchController: _controller,
      barElevation: WidgetStateProperty.all(2),
      barPadding: const WidgetStatePropertyAll(
        EdgeInsets.symmetric(horizontal: 12),
      ),

      viewHeaderTextStyle: const TextStyle(fontSize: 14),
      barTextStyle: WidgetStateProperty.all(
        const TextStyle(fontSize: 14),
      ),
      viewLeading: const Icon(Icons.search),
      viewBarPadding: const EdgeInsets.symmetric(horizontal: 12),
      scrollPadding: EdgeInsets.zero,
      viewPadding: EdgeInsets.zero,
      onSubmitted: (value) {
        _controller.closeView(value);
        // focus is kept on the bar.
      },
      suggestionsBuilder: (context, controller) async {
        final results = await Future.value(<String>["Example1", "Example2"]);

        return [
          for (final element in results)
            ListTile(
              title: Text(
                element,
              ),
              onTap: () {
                print("Selected: $element");
                controller.closeView(element);
              },
            ),
        ];
      },
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
Screen.Recording.2025-11-17.at.09.24.54.mov

Flutter Doctor output

Doctor output
lutter doctor -v
[✓] Flutter (Channel stable, 3.38.1, on macOS 15.6.1 24G90 darwin-arm64, locale en-IT) [201ms]
    • Flutter version 3.38.1 on channel stable at /Users/venir/.local/share/mise/installs/flutter/3.38.1-stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b45fa18946 (5 days ago), 2025-11-12 22:09:06 -0600
    • Engine revision b5990e5ccc
    • Dart version 3.10.0
    • DevTools version 2.51.1
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations,
      enable-native-assets, omit-legacy-version-file, enable-lldb-debugging

[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0-rc1) [2.2s]
    • Android SDK at /Users/venir/Library/Android/sdk
    • Emulator version 35.2.10.0 (build_id 12414864) (CL:N/A)
    • Platform android-36, build-tools 36.0.0-rc1
    • Java binary at: /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java
      This JDK is specified in your Flutter configuration.
      To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version Java(TM) SE Runtime Environment (build 17.0.12+8-LTS-286)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 26.1.1) [464ms]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 17B100
    • CocoaPods version 1.16.2

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

[✓] Connected device (2 available) [5.7s]
    • macOS (desktop) • macos  • darwin-arm64   • macOS 15.6.1 24G90 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 142.0.7444.162

[✓] Network resources [223ms]
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsf: focusFocus traversal, gaining or losing focusf: material designflutter/packages/flutter/material repository.f: routesNavigator, Router, and related APIs.found in release: 3.38Found to occur in 3.38found in release: 3.39Found to occur in 3.39frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages teamworkaround availableThere is a workaround available to overcome the issue

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions