Skip to content

Unexpected behaviour of TextField hintMaxLines property #52008

Closed

Description

@jenshor

I am using the hintMaxLines property of the TextField which unexpectedly increases the TextField height even if the hint is not shown (See images below)

TextField when Hint is displayed:
Screenshot 2020-03-05 at 12 08 59

TextField when Hint is not shown:
Screenshot 2020-03-05 at 12 09 02

Expected TextField when Hint is not shown:
Screenshot 2020-03-05 at 12 09 20

In the last image i forced the hintMaxLines = 1 if a text value was inserted by the user.

Code:

import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeScreen(title: 'Flutter Demo Home Page'),
    );
  }
}

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  TextEditingController controller = TextEditingController();

  bool isEmpty = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Container(
          padding: EdgeInsets.all(16),
          child: TextField(
            keyboardType: TextInputType.multiline,
            maxLines: null,
            controller: controller,
            onChanged: (value) {
              setState(() {
                // Toggle this for expected behavior
                // isEmpty = value.isEmpty;
              });
            },
            decoration: InputDecoration(
                labelText: 'Title',
                hintMaxLines: isEmpty ? 15 : 1,
                hintText:
                  Iterable.generate(400, (index) => 'a').join(),
                    errorText: 'This is an error'),
          ),
        ));
  }
}

Flutter Doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.15.3 19D76, locale en-DE)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
[✓] Android Studio (version 3.5)
[!] IntelliJ IDEA Ultimate Edition (version 2019.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.42.1)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

Activity

  1. added
    a: text inputEntering text in a text field or keyboard related problems
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Mar 5, 2020
  2. justinmc commented on Mar 19, 2020

    @justinmc
    Contributor

    Thanks for the detailed issue. Are you saying that the expected behavior is that the field is very tall when it's empty, but when the user enters a character, it will immediately shrink to one line high?

  3. justinmc commented on Mar 23, 2020

    @justinmc
    Contributor

    Potentially related: #46182

  4. darshankawar commented on May 3, 2022

    @darshankawar
    Member

    This behavior is replicable on latest stable and master using the code sample provided.

    Screenshot 2022-05-03 at 6 20 53 PM

    runnable code sample
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(App());
    
    class App extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: HomeScreen(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class HomeScreen extends StatefulWidget {
      HomeScreen({Key? key, required this.title}) : super(key: key);
    
      final String title;
    
      @override
      _HomeScreenState createState() => _HomeScreenState();
    }
    
    class _HomeScreenState extends State<HomeScreen> {
      TextEditingController controller = TextEditingController();
    
      bool isEmpty = true;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text(widget.title),
            ),
            body: Container(
              padding: EdgeInsets.all(16),
              child: TextField(
                keyboardType: TextInputType.multiline,
                maxLines: null,
                controller: controller,
                onChanged: (value) {
                  setState(() {
                    // Toggle this for expected behavior
                    // isEmpty = value.isEmpty;
                  });
                },
                decoration: InputDecoration(
                    labelText: 'Title',
                    hintMaxLines: isEmpty ? 15 : 1,
                    hintText:
                    Iterable.generate(400, (index) => 'a').join(),
                    errorText: 'This is an error'),
              ),
            ));
      }
    }
    
    
    stable, master flutter doctor -v
    
    [✓] Flutter (Channel stable, 2.10.5, on macOS 12.2.1 21D62 darwin-x64, locale
        en-GB)
        • Flutter version 2.10.5 at /Users/dhs/documents/fluttersdk/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 5464c5bac7 (12 hours ago), 2022-04-18 09:55:37 -0700
        • Engine revision 57d3bac3dd
        • Dart version 2.16.2
        • DevTools version 2.9.2
    
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    [✓] Flutter (Channel master, 2.13.0-0.0.pre.866, on macOS 12.2.1 21D62
        darwin-x64, locale en-GB)
        • Flutter version 2.13.0-0.0.pre.866 at
          /Users/dhs/documents/fluttersdk/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 6246a09d0e (29 minutes ago), 2022-05-03 00:04:07 -0400
        • Engine revision a00140cb28
        • Dart version 2.18.0 (build 2.18.0-78.0.dev)
        • DevTools version 2.12.2
        
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    
  5. added
    r: fixedIssue is closed as already fixed in a newer version
    on Sep 27, 2024
  6. github-actions commented on Oct 11, 2024

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsfound in release: 2.10Found to occur in 2.10found in release: 2.13Found to occur in 2.13frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions