Skip to content

[TextField] The computation of the intrinsic width of a TextField should include the hint width only when visible #93337

Closed

Description

@ramsestom

Currently, when a TextField has decoration with an hint text, the width of this hint element is always integrated into the computation of the intrinsic width of the TextField widget, even if this hint is not displayed (= when the TextField text is not empty). This do not really make sense. As a consequence of this, when wrapping a TextField with an hint into an IntrinsicWidth widget or when setting a suffixText decoration to the TextField, the result is not the one you would expect as the minimal width of the text field would be constrained by the hint width.
This should be fixed by integrating the width of the hint text into the computation of the TextField intrinsic width only when its text is empty (=when the hint is actually displayed).

Activity

  1. changed the title [-]The computation of the intrinsic width of a TextField should integrate the hint width only when visible[/-] [+]The computation of the intrinsic width of a TextField should include the hint width only when visible[/+] on Nov 9, 2021
  2. maheshj01 commented on Nov 10, 2021

    @maheshj01
    Member

    Hi @ramsestom, Thanks for filing the issue. Can you please provide reproducible steps to replicate the issue along with the output of flutter doctor -v.

    Thanks

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 10, 2021
  4. ramsestom commented on Nov 10, 2021

    @ramsestom
    Author

    Just try this:

    IntrinsicWidth(
        child: TextField(
        decoration: InputDecoration(
            hintText: 'this is some hint',
            suffixText: 'sufix',
        ),
    ),
    

    in the text field type some short text (like 'test') and you will see that the field width remains the width of the hint text (it will only increase when the text you enter is longer than the hint text). If you remove the hintText property, it works as expected.

    flutter doctor -v
    [√] Flutter (Channel beta, 2.6.0-5.2.pre, on Microsoft Windows [version 10.0.19043.1288], locale fr-FR)
        • Flutter version 2.6.0-5.2.pre at C:\Development\Flutter\SDK
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 400608f101 (8 weeks ago), 2021-09-15 15:50:26 -0700
        • Engine revision 1d521d89d8
        • Dart version 2.15.0 (build 2.15.0-82.2.beta)
    
    [!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at C:\Development\Android\android-sdk
        • Platform android-31, build-tools 31.0.0
        • ANDROID_HOME = C:\Development\Android\android-sdk
        • Java binary at: C:\Program Files\Android\Android Studio1\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
        ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
    
    [√] Chrome - develop for the web
        • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
    
    [√] Android Studio (version 2020.3)
        • Android Studio at C:\Program Files\Android\Android Studio1
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    
    [√] VS Code, 64-bit edition (version 1.61.2)
        • VS Code at C:\Program Files\Microsoft VS Code
        • Flutter extension version 3.28.0
    
    [√] Connected device (3 available)
        • Android SDK built for x86 64 (mobile) • emulator-5554 • android-x64    • Android 7.0 (API 24) (emulator)
        • Chrome (web)                          • chrome        • web-javascript • Google Chrome 94.0.4606.81
        • Edge (web)                            • edge          • web-javascript • Microsoft Edge 95.0.1020.40
    
    ! Doctor found issues in 1 category.
    
  5. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 10, 2021
  6. maheshj01 commented on Nov 10, 2021

    @maheshj01
    Member

    Thanks for the info @ramsestom, I am quite not sure I understand your proposal

    I can see the textfield width increasing (as expected) when it goes beyond the hint

    counter.2021-11-10.12-59-26.mp4

    And I also see the minimal width of textfield is hintwidth +suffix text width so I don't see a problem here. can you please explain your proposal or issue?

    counter.2021-11-10.13-05-37.mp4
  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 10, 2021
  8. ramsestom commented on Nov 10, 2021

    @ramsestom
    Author

    Like I said, type some short text into the textfield (so the hint text disapear and is replaced by the text you typed). In this case the text field would not have the correct width (it will keep the same width as when the hint text was displayed, even if the text in it is now shorter). You can see this in you first video. The textfield starts changing its width only when the typed text is longer than the hint one (wich is not what is expected. As soon as the hint text dissapear, the textfield should shrink to fit its content as it is inside an IntrinsicWidth widget.)

  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 10, 2021
  10. maheshj01 commented on Nov 10, 2021

    @maheshj01
    Member

    Like I said, type some short text into the textfield (so the hint text disapear and is replaced by the text you typed). In this case the text field would not have the correct width (it will keep the same width as when the hint text was displayed, even if the text in it is now shorter).

    Ah, I see thanks for the clarification @ramsestom. The textfield when wrapped in a IntrinsicWidth widget its width should be based on the width of the text in the textfield (when hint is not shown) but currently its constant until the entered text width is greater than the hint.

    e.g in the below image textfield should have shrinked since it is within the IntrinsicWidth widget, but instead it still has constant width of hintText

    image

    flutter doctor -v
    [✓] Flutter (Channel stable, 2.5.3, on macOS 12.0.1 21A559 darwin-arm, locale en-GB)
        • Flutter version 2.5.3 at /Users/mahesh/Documents/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 18116933e7 (4 weeks ago), 2021-10-15 10:46:35 -0700
        • Engine revision d3ea636dc5
        • Dart version 2.14.4
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-31, build-tools 31.0.0
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 13.1, Build version 13A1030d
        • CocoaPods version 1.10.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 60.1.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.61.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.28.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-arm64   • macOS 12.0.1 21A559 darwin-arm
        • Chrome (web)    • chrome • web-javascript • Google Chrome 95.0.4638.69
    
    • No issues found!
    
    [✓] Flutter (Channel master, 2.6.0-12.0.pre.655, on macOS 12.0.1 21A559 darwin-arm, locale en-GB)
        • Flutter version 2.6.0-12.0.pre.655 at /Users/mahesh/Documents/flutter_master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 5ce05e6ec5 (34 hours ago), 2021-11-08 23:43:04 -0500
        • Engine revision 162d8508f8
        • Dart version 2.15.0 (build 2.15.0-285.0.dev)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-31, build-tools 31.0.0
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • CocoaPods version 1.10.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 60.1.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.61.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.28.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-arm64   • macOS 12.0.1 21A559 darwin-arm
        • Chrome (web)    • chrome • web-javascript • Google Chrome 95.0.4638.69
    
    • No issues found!
    
    code sample
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            // This is the theme of your application.
            //
            // Try running your application with "flutter run". You'll see the
            // application has a blue toolbar. Then, without quitting the app, try
            // changing the primarySwatch below to Colors.green and then invoke
            // "hot reload" (press "r" in the console where you ran "flutter run",
            // or simply save your changes to "hot reload" in a Flutter IDE).
            // Notice that the counter didn't reset back to zero; the application
            // is not restarted.
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key, required this.title}) : super(key: key);
    
      // This widget is the home page of your application. It is stateful, meaning
      // that it has a State object (defined below) that contains fields that affect
      // how it looks.
    
      // This class is the configuration for the state. It holds the values (in this
      // case the title) provided by the parent (in this case the App widget) and
      // used by the build method of the State. Fields in a Widget subclass are
      // always marked "final".
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          // This call to setState tells the Flutter framework that something has
          // changed in this State, which causes it to rerun the build method below
          // so that the display can reflect the updated values. If we changed
          // _counter without calling setState(), then the build method would not be
          // called again, and so nothing would appear to happen.
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        // This method is rerun every time setState is called, for instance as done
        // by the _incrementCounter method above.
        //
        // The Flutter framework has been optimized to make rerunning build methods
        // fast, so that you can just rebuild anything that needs updating rather
        // than having to individually change instances of widgets.
        return Scaffold(
          appBar: AppBar(
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: Text(widget.title),
          ),
          body: Center(
            // Center is a layout widget. It takes a single child and positions it
            // in the middle of the parent.
            child: Column(
              // Column is also a layout widget. It takes a list of children and
              // arranges them vertically. By default, it sizes itself to fit its
              // children horizontally, and tries to be as tall as its parent.
              //
              // Invoke "debug painting" (press "p" in the console, choose the
              // "Toggle Debug Paint" action from the Flutter Inspector in Android
              // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
              // to see the wireframe for each widget.
              //
              // Column has various properties to control how it sizes itself and
              // how it positions its children. Here we use mainAxisAlignment to
              // center the children vertically; the main axis here is the vertical
              // axis because Columns are vertical (the cross axis would be
              // horizontal).
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'You have pushed the button this many times:',
                ),
                IntrinsicWidth(
                    child: TextField(
                  decoration: InputDecoration(
                    hintText: 'this is some hint',
                    suffixText: 'sufix',
                  ),
                )),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
    
  11. added
    a: text inputEntering text in a text field or keyboard related problems
    and removed
    in triagePresently being triaged by the triage team
    on Nov 10, 2021
  12. 5 remaining items

  13. Hari-07 commented on Nov 2, 2024

    @Hari-07
    Member

    @PoojaB26 Managed to make this situation work by removing the hint text altogether from the textfield (hintText:null), and having hint text be a separate text element thats next in the row that shows only when controller.text is empty

  14. Hari-07 commented on Nov 2, 2024

    @Hari-07
    Member

    This is a P2 that's been open for more than 2 years, any ETA?

  15. self-assigned this
    on Dec 19, 2024
  16. moved this from To do to In progress in Nevercodeon Dec 20, 2024
  17. moved this from In progress to PR submitted in Nevercodeon Jan 9, 2025
  18. moved this from PR submitted to Done (PR merged) in Nevercodeon Jan 24, 2025
  19. added
    r: fixedIssue is closed as already fixed in a newer version
    on Jan 24, 2025
  20. github-actions commented on Feb 7, 2025

    @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.

  21. locked as resolved and limited conversation to collaborators on Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsfound in release: 2.5Found to occur in 2.5found in release: 2.6Found to occur in 2.6frameworkflutter/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-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

Type

No type

Projects

  • Status
    Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions