Skip to content

TextField should warn on vertical overflow #35706

Open

Description

@fdharamshi

I tried finding in a lot of resources but unfortunately i could not find a way to align the text vertically centre in a textfield. I also tried using suffixIcon instead of suffix but still not luck. Here is my code :

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Icon(
          Icons.menu,
          color: Colors.black,
        ),
        backgroundColor: Colors.white,
        title: Container(
          margin: EdgeInsets.only(bottom: 10),
          child: Image.asset(
            "icons/logo.png",
          ),
        ),
        bottom: PreferredSize(
          child: Padding(
            padding: EdgeInsets.only(
              left: 10,
              right: 10,
              bottom: 10,
            ),
            child: Container(
              height: 40,
              child: TextField(
                textAlignVertical: TextAlignVertical.center,
                textAlign: TextAlign.left,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 13,
                ),
                decoration: InputDecoration(
                    suffixIcon: IconButton(icon: Icon(Icons.search, color: Colors.black,), onPressed: (){}),
                    border: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.black,
                      ),
                      borderRadius: BorderRadius.all(Radius.circular(15)),
                    )
                ),
              ),
            ),
          ),
          preferredSize: Size(MediaQuery.of(context).size.width, 50),
        ),
      ),
      body: Container(
        margin: EdgeInsets.only(top: 11),
        child: Column(
          children: <Widget>[
            Carousel(),
          ],
        ),
      ),
    );
  }
}

class Carousel extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _CarouselState();
  }
}

class _CarouselState extends State<Carousel> {
  List<String> urls = [];

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 10),
      child: Stack(
        children: <Widget>[
          Image.network(
              "someImageUrlHere."),
          Positioned(
            bottom: 5,
            width: MediaQuery.of(context).size.width - 20,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text("•"),
                Text("•"),
                Text("•"),
                Text("•"),
                Text("•"),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

What could be the issue that is causing this problem ? and how can i solve this issue ?

Activity

  1. added
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jul 7, 2019
  2. vagrantrobbie commented on Jul 9, 2019

    @vagrantrobbie

    I think this a was more of a StackOverflow question.

    I've modified your PreferredSize Widget. Added contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 10.0), and adjusted TextStyle height. I think you just fiddle around with the height, ie line height, until it sits right for the particular font

    PreferredSize(
              child: Padding(
                padding: EdgeInsets.only(
                  left: 10,
                  right: 10,
                  bottom: 10,
                ),
                child: Container(
                  height: 40.0,
                  child: TextField(
                    textAlignVertical: TextAlignVertical.center,
                    textAlign: TextAlign.left,
                    maxLines: 1,
                    style: TextStyle(
                        fontSize: 13,
                        height: 1.15
                    ),
                    decoration: InputDecoration(
                        contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 10.0),
                        suffixIcon: IconButton(
                            icon: Icon(
                              Icons.search,
                              color: Colors.black,
                            ),
                            onPressed: () {}),
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: Colors.black,
                          ),
                          borderRadius: BorderRadius.all(Radius.circular(15)),
                        )),
                  ),
                ),
              ),
              preferredSize: Size(MediaQuery.of(context).size.width, 50),
            )
    
  3. LongCatIsLooong commented on Jul 17, 2019

    @LongCatIsLooong
    Contributor

    There needs to be some visual indication in debug mode, similar to what we have for overflowing content, to warn the user when something in the InputDecorator overflows.

  4. vagrantrobbie commented on Jul 17, 2019

    @vagrantrobbie

    I haven't looked at how the TextField Widget works exactly but I image their is a Stack Widget in there with Overflow clip.

    I don't think you really need a visual indicator here as it would be expected behaviour to have the inside clip to the bounds, if my assumption is correct.

  5. kf6gpe commented on Jan 6, 2020

    @kf6gpe
    Contributor

    Leaving open per @LongCatIsLooong comment; I think that's a good adviosry change.,

  6. Daniate commented on Feb 8, 2020

    @Daniate

    I had the same problem. When I set the border to InputBorder.none, it becomes correct.
    image
    image

  7. Daniel-BD commented on May 20, 2020

    @Daniel-BD

    textAlignVertical seems to be at least somewhat broken. I can't get it to work or even make any valuable difference...

    It's impossible to get the text centered by using TextAlignVertical.center and using different values for y: makes little difference, I can only push the text further down...

    This is my TextField with TextAlignVertical.center
    Skärmavbild 2020-05-20 kl  16 21 23

    This is with TextAlignVertical(y: -1.0)
    Skärmavbild 2020-05-20 kl  16 24 34

    This is with TextAlignVertical(y: 1.0)
    image

    As you can see, the value of 0 (.center) and -1.0 is the same, while 1.0 pushes it further down. What is actually needed in order to center the text is to have it be pushed UP, which I can't do.

    It may be that I'm doing something wrong, but then I think the API should be made more intuitive. I've hade many struggles with aligning text in TextField before, and judging by posts on StackOverflow, a lot of people are doing weird hacks to get TextField text alignment to a place they want it.

    This is my code:

    return TextField(
          focusNode: focus,
          controller: controller,
          textAlign: TextAlign.center,
          textAlignVertical: TextAlignVertical(y: -1.0),
          cursorColor: focused,
          style: style,
          decoration: InputDecoration(
            hintStyle: hintStyle,
            hintText: 'Enter Room Code',
            enabledBorder: unFocusedBorder,
            focusedBorder: focusedBorder,
          ),
        );
    
    final style = TextStyle(
        fontSize: 40,
      );
    
    final hintStyle = TextStyle(
        fontSize: 22,
      );
    
    final unFocusedBorder = OutlineInputBorder(
        borderRadius: const BorderRadius.all(
          const Radius.circular(8),
        ),
        borderSide: BorderSide(
          width: 2.0,
          color: textColor,
        ),
      );
    
    final focusedBorder = OutlineInputBorder(
        borderRadius: const BorderRadius.all(
          const Radius.circular(20),
        ),
        borderSide: BorderSide(
          width: 4.0,
          color: focused,
        ),
      );
    

    Flutter doctor:

    [✓] Flutter (Channel stable, v1.17.0, on Mac OS X 10.14.6 18G95, locale sv-SE)
        • Flutter version 1.17.0 at /Library/flutter
        • Framework revision e6b34c2b5c (3 weeks ago), 2020-05-02 11:39:18 -0700
        • Engine revision 540786dd51
        • Dart version 2.8.1
    
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
        • Android SDK at /Users/daniel/Library/Android/sdk
        • Platform android-28, build-tools 28.0.3
        • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
        • All Android licenses accepted.
    
    [!] Xcode - develop for iOS and macOS (Xcode 11.0)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 11.0, Build version 11A420a
        ! CocoaPods 1.7.2 out of date (1.8.0 is recommended).
            CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin
            usage on the Dart side.
            Without CocoaPods, plugins will not work on iOS or macOS.
            For more info, see https://flutter.dev/platform-plugins
          To upgrade:
            sudo gem install cocoapods
    
    [✓] Android Studio (version 3.6)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin version 45.0.1
        • Dart plugin version 192.7761
        • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    
  8. Daniel-BD commented on May 20, 2020

    @Daniel-BD

    After some more testing, it seems like having a different font size for hintStyle and style is causing this problem... If they're the same size, then it seems to be working. Is this something that can be fixed somehow?

    Edit:

    After some more tinkering, this is how I solved having the text be centered, while still having two different styles for hintStyle and style:

    First the style property on TextField:

    style: controller.text.isEmpty ? hintStyle : style
    

    and the same for hintStyle in the InputDecoration

    hintStyle: controller.text.isEmpty ? hintStyle : style,
    

    And then this on the TextField's onChanged property:

    onChanged: (_) {
              if (controller.text.isNotEmpty) {
                Future.delayed(Duration(milliseconds: 100)).then((value) => setState(() {}));
              } else {
                setState(() {});
              }
            },
    

    Yeah, it's a weird hack, just like I said I saw a lot of on StackOverflow in the previous post :)

  9. added
    a: text inputEntering text in a text field or keyboard related problems
    on Jun 18, 2020
  10. TahaTesser commented on Jul 16, 2020

    @TahaTesser
    Contributor
    flutter doctor -v
    [✓] Flutter (Channel dev, 1.20.0-7.1.pre, on Mac OS X 10.15.6 19G73, locale
        en-GB)
        • Flutter version 1.20.0-7.1.pre at /Users/taha/Code/flutter_dev
        • Framework revision 7736f3bc90 (6 days ago), 2020-07-10 16:33:05 -0700
        • Engine revision d48085141c
        • Dart version 2.9.0 (build 2.9.0-21.2.beta)
    
     
    [✓] Android toolchain - develop for Android devices (Android SDK version
        30.0.0)
        • Android SDK at /Users/taha/Code/sdk
        • Platform android-30, build-tools 30.0.0
        • ANDROID_HOME = /Users/taha/Code/sdk
        • Java binary at: /Applications/Android
          Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6222593)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 11.6)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 11.6, Build version 11E708
        • CocoaPods version 1.9.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google
          Chrome
    
    [✓] Android Studio (version 4.0)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin version 47.1.2
        • Dart plugin version 193.7361
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6222593)
    
    [✓] VS Code (version 1.47.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.12.2
    
    [✓] Connected device (5 available)
        • SM M305F (mobile)      • 32003c30dc19668f          • android-arm64  •
          Android 10 (API 29)
        • Taha’s iPhone (mobile) • 00008020-001059882212002E • ios            •
          iOS 13.6
        • macOS (desktop)        • macos                     • darwin-x64     •
          Mac OS X 10.15.6 19G73
        • Web Server (web)       • web-server                • web-javascript •
          Flutter Tools
        • Chrome (web)           • chrome                    • web-javascript •
          Google Chrome 83.0.4103.116
    
    • No issues found!
    
  11. changed the title [-]Vertically Centre Align Text in TextField Flutter[/-] [+]Visual indication in debug mode when Text in TextField isn't Center Align[/+] on Jul 16, 2020
  12. 24 remaining items

  13. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on May 7, 2025
  14. added
    P3Issues that are less important to the Flutter project
    on May 8, 2025
  15. LongCatIsLooong commented on May 8, 2025

    @LongCatIsLooong
    Contributor

    I think what happened is that the InputDecorator layout algorithm prioritize content padding over the text content even in a single line text field.
    So the top content padding pushes the text down when there isn't enough vertical space, making it look off-centered.

    Options I can think of:

    1. We could change the layout algorithm such that it prioritize the text over the content padding (for example, vertically center the text within the available space, since RenderEditable does not vertically clip the text in single-line mode it would appear as if the content padding is reduced). This will affect many golden tests.
    2. Add a debugInputDecoratorOverflow flag so which shows a visual indicator similar to the Row/Column overflow indicator when turned on.

    I'm leaning towards option 1 since the overflow warning is usually not very actionable (dynamically adjusting the vertical padding based on the amount of vertical space available would require LayoutBuilder or a custom RenderBox). I'll introduce an opt-in flag to indicate that in a single-line text field the vertical content padding is maintained only when there is enough vertical space, and see if the default value of the flag can be changed to opt-out.

  16. gnprice commented on May 8, 2025

    @gnprice
    Member

    On seeing an overflow warning, I'd think the way to act on it would be:

    • If the padding is important, then increase the height.
    • If the short height is important, then reduce the padding.

    I agree it'd usually be messy if the developer needed to dynamically adjust the padding. What's the situation where that would be needed, though?

    The main thing that comes to mind is that one might do that as a way of controlling the vertical alignment. But I believe that's what textAlignVertical is for.

  17. flutter-triage-bot commented on Sep 26, 2025

    @flutter-triage-bot

    This issue is assigned to @LongCatIsLooong but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

  18. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Sep 26, 2025
  19. flutter-triage-bot commented on Dec 12, 2025

    @flutter-triage-bot

    This issue was assigned to @LongCatIsLooong but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed.

  20. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on Dec 12, 2025
  21. added
    P2Important issues not at the top of the work list
    and removed
    P3Issues that are less important to the Flutter project
    on Dec 18, 2025
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

    P2Important issues not at the top of the work lista: debuggingDebugging, breakpoints, expression evaluationa: qualityA truly polished experiencea: text inputEntering text in a text field or keyboard related problemsfound in release: 1.20Found to occur in 1.20frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-text-inputOwned by Text Input teamtriaged-text-inputTriaged by Text Input team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions