Skip to content

[TextField] FocusNode's onKeyEvent is triggered twice.  #129822

@sabin26

Description

@sabin26

Is there an existing issue for this?

Steps to reproduce

  1. Type a character on the text field.
  2. Watch the console for logs.

Expected results

The onKeyEvent should only be triggered once.

Actual results

The onKeyEvent is triggered twice.

Code sample

Code sample
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'application',
      theme: ThemeData(useMaterial3: true),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    final Map<String, Widget Function()> tabMap = {
      'Tab A': () => const TabAContent(),
      'Tab B': () => const SizedBox.shrink(),
      'Tab C': () => const SizedBox.shrink(),
    };
    final tabs = tabMap.keys.map((final title) => Tab(text: title));
    final views = tabMap.values.map((final e) => e());
    return Scaffold(
      body: DefaultTabController(
        length: tabs.length,
        child: CustomScrollView(
          scrollBehavior: ScrollConfiguration.of(context).copyWith(
            scrollbars: false,
          ),
          slivers: [
            const SliverAppBar(title: Text('Application'), pinned: true),
            SliverFillRemaining(
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    TabBar(tabs: tabs.toList(growable: false)),
                    Expanded(
                      child: TabBarView(
                        children: views.toList(growable: false),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class TabAContent extends StatelessWidget {
  const TabAContent({super.key});

  @override
  Widget build(final BuildContext context) {
    return ListView(
      padding: EdgeInsets.zero,
      children: const [Field()],
    );
  }
}

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

  @override
  State<StatefulWidget> createState() => _FieldState();
}

class _FieldState extends State<Field> {
  late final FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _focusNode = FocusNode(onKeyEvent: (node, event) {
      print('I am called');
      return KeyEventResult.ignored;
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return TextField(focusNode: _focusNode);
  }
}

Screenshots or Video

Screenshots / Video demonstration

focusnode_bug

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 3.12.0, on Microsoft Windows [Version 10.0.19045.3086], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.6.3)
[√] Android Studio (version 2022.2)
[√] VS Code (version 1.79.2)
[√] Connected device (3 available)
[√] Network resources

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: invalidIssue is closed as not valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions