Skip to content

TextField: Bug with Dialog #37798

Closed

Description

@yaymalaga

Internal: b/147671610

I'm having a bug with the TextField not showing the selection menu (I want to paste text) if used with a modal, like for example a Dialog. This just happens when it is used with a TextEditingController, if you just comment the line 'controller: _controller,' it is working as expected.

Steps to Reproduce

Use the following minimal example to test the bug. Click OPEN and long-pressed the TextField, then the selection menu will appear. Dismiss the modal by pressing outside, then click again OPEN and repeat the operation: the selection menu doesn't appear.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(),
      title: 'Text Field Test',
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  TextEditingController _controller;

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

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

  void _showDialog(BuildContext context) {
    showDialog<void>(
      context: context,
      builder: (_) => Dialog(
        child: Container(
          padding: const EdgeInsets.all(20),
          child: TextField(
            controller: _controller,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("TextField Test"),
      ),
      body: Center(
        child: FlatButton(
          child: Text("OPEN"),
          onPressed: () => _showDialog(context),
        ),
      ),
    );
  }
}

I'm using master branch up to date:

[✓] Flutter (Channel master, v1.8.4-pre.129, on Linux, locale en_GB.UTF-8)
    • Flutter version 1.8.4-pre.129 at /home/yaymalaga/Android/flutter
    • Framework revision 5756e2dcd0 (9 hours ago), 2019-08-07 04:50:25 -0400
    • Engine revision 9bfa4f53cd
    • Dart version 2.5.0 (build 2.5.0-dev.1.0 c262cbd414)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
    • Android SDK at /home/yaymalaga/Android/Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.1
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1136-b06)
    • All Android licenses accepted.

[✓] Android Studio (version 3.2)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 31.3.1
    • Dart plugin version 181.5656
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1136-b06)

Activity

  1. added
    a: text inputEntering text in a text field or keyboard related problems
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Aug 7, 2019
  2. justinmc commented on Aug 8, 2019

    @justinmc
    Contributor

    I was able to reproduce this using the steps given. Very strange! I'll try to look into this when I have time and see why it's not showing up.

  3. irfantusneem commented on Jan 7, 2020

    @irfantusneem

    It looks like this can be reproduced in other containers even when there is no controller set.

  4. self-assigned this
    on Jan 10, 2020
  5. jason-simmons commented on Jan 10, 2020

    @jason-simmons
    Member

    Reproduced this in the affected app. The toolbar is not showing because EditableTextState has a null _selectionOverlay at the time of the showToolbar call.

    Ordinarily _selectionOverlay would be constructed by a _handleSelectionChanged call triggered by the tap into the text field that sets the caret position. But in this case, the RenderEditable selection is being set to the zero position due to interactions with the text service. When the tap occurs RenderEditable._handleSelectionChange thinks the selection already has the right value and does not make the callback into EditableTextState._handleSelectionChanged. _selectionOverlay is never set, and the toolbar does not show.

    @justinmc One potential workaround would be creating _selectionOverlay in EditableTextState._handleFocusChanged when the text field acquires the focus.

  6. justinmc commented on Jan 10, 2020

    @justinmc
    Contributor

    Thanks for the investigation Jason. I actually can't seem to reproduce this myself! I'm using master and tried on the Android emulator and a physical iOS device. Maybe I need to try a physical Android device.

    I tap Open, then directly long press the field, and I see the selection menu. Then I tap the background to close the modal, tap Open again, and directly long press the field again. The selection menu shows again without a problem. If I print _selectionOverlay in showToolbar in EditableTextState, it is never null.

  7. gliese667c commented on Jan 10, 2020

    @gliese667c

    Same problem on physical iOS device.

  8. justinmc commented on Jan 10, 2020

    @justinmc
    Contributor

    @gliese667c Are you on master?

  9. added this to the February 2020 milestone on Jan 15, 2020
  10. justinmc commented on Jan 16, 2020

    @justinmc
    Contributor

    I was able to reproduce this bug, not by using a Dialog, but due to setting the initial selection to 0,0. I fixed it in #49000. Here's a basic app that reproduces it.

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp();
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(),
          title: 'Text Field Test',
          home: Scaffold(
            body: Center(
              child: TextField(
                controller: TextEditingController.fromValue(
                  TextEditingValue(
                    // Changing the offset to -1,-1 fixes the problem.
                    selection: TextSelection(baseOffset: 0, extentOffset: 0),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }

    If this doesn't cover anyone's problem here, let me know.

  11. github-actions commented on Aug 23, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  12. locked as resolved and limited conversation to collaborators on Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a: text inputEntering text in a text field or keyboard related problemscustomer: mulligan (g3)frameworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packages

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions