Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertical Alignment of Icon in a TextField #42651

Closed
Reprevise opened this issue Oct 14, 2019 · 17 comments
Closed

Vertical Alignment of Icon in a TextField #42651

Reprevise opened this issue Oct 14, 2019 · 17 comments
Assignees
Labels
a: text input Entering text in a text field or keyboard related problems c: new feature Nothing broken; request for a new capability customer: crowd Affects or could affect many people, though not necessarily a specific customer. f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects

Comments

@Reprevise
Copy link

Use case

I have a form and below it, I have an expandable details field (it's a shame that I can't do it with a TextFormField but that's for another time). I vertically aligned the text to the top using textAlignVertical: TextAlignVertical.top but I can't do the same for the prefixIcon.

Here's the code I used to make this field:

    Widget detailsField = SizedBox(
      height: 100,
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 8),
        child: TextField(
          keyboardType: TextInputType.multiline,
          expands: true,
          minLines: null,
          maxLines: null,
          decoration: InputDecoration(
            border: OutlineInputBorder(),
            hintText: "Details",
            prefixIcon: Icon(Icons.subject, color: Theme.of(context).primaryIconTheme.color),
          ),
          textAlignVertical: TextAlignVertical.top,
        ),
      ),
    );

vertical align icon

Proposal

It would be great if there was a property when you style the prefix or suffix icon that can vertically align it in an expandable text field so that the text and icon aren't misaligned.

@BondarenkoStas BondarenkoStas added framework flutter/packages/flutter repository. See also f: labels. c: new feature Nothing broken; request for a new capability labels Oct 14, 2019
@HansMuller HansMuller added the f: material design flutter/packages/flutter/material repository. label Oct 16, 2019
@Menelphor
Copy link

Menelphor commented Feb 7, 2020

#42936 does not mention this issue at all.

Would be awesome to have for suffix icon as well

@Reprevise

This comment has been minimized.

@NiteshKumarPal
Copy link

I am also facing this issue, I have also the requirement to set prefix icon to be top-aligned with its text. I request to the core team, please add this feature in this API.

@ghost
Copy link

ghost commented Jun 5, 2020

I solved this problem by adding a Padding to the Icon.
prefixIcon: Padding( padding: const EdgeInsets.fromLTRB(0, 0, 0, 80), child: Icon(Icons.message), )

@guidezpl guidezpl added the a: text input Entering text in a text field or keyboard related problems label Jun 18, 2020
@hewa-jalal
Copy link

using textAlignVertical: TextAlignVertical.center easily fix it.

@comerc
Copy link

comerc commented Sep 29, 2020

I want vertical alignment for prefixIcon & suffixIcon like Telegram:

Снимок экрана от 2020-09-29 08-07-18

@vitormnoel
Copy link

im also having this issue, padding worked for me.

prefixIcon: Padding(
padding: const EdgeInsets.all(8)..

8 because my container have height: 38

@alizera
Copy link

alizera commented Dec 12, 2020

Any update?

@Purvik
Copy link

Purvik commented Feb 9, 2021

Is there any solution?

@MuhammadEbraheem
Copy link

any news about this issue?

@emrerdem1
Copy link

emrerdem1 commented Mar 3, 2021

Default suffixIcon implementation in the doc

image

Here is my solution:

// suffixIcon use Center widget by default, this is to pin it to top.
 final clearIcon = Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
       IconButton(
          icon: Icon(
            Icons.clear,
            color: Theme.of(context).hintColor,
          ),
          onPressed: () => _yourController.clear(),
        )
      ],
    );
...
 TextFormField(
      maxLines: 9,
      decoration: InputDecoration(
       // Reduce default spaces assigned to icon. It is 48x48 by default.
        suffixIconConstraints: BoxConstraints.tight(
          Size.fromWidth(40),),
        suffixIcon:
            _yourController.text.isNotEmpty ? clearIcon : null,
      ),
      controller: _yourController,
    )

Simple prefix or suffix have the same baseline as texts, so using them also solves this misalignment issue. But do not forget they are designed to add paddings initially, not to build icons. So Icon used in suffix can also tweak the alignment of text when you try to extend clickable area of icon by paddings. You need to play around with it in that case.

@pratikgtam
Copy link

pratikgtam commented Jun 14, 2021

I manage to solve this by adding proper bottom padding

                    bottom: (maxLines - 1) * 18.0),
  prefixIcon: Padding(
                padding: EdgeInsets.only(
                    left: 25,
                    right: 18,
                    //https://github.com/flutter/flutter/issues/42651 The issue is opened in github. However, I manged to solve this problem with proper bottom padding.
                    bottom: (maxLines - 1) * 18.0),
                child: Icon(
                  prefixIcon,
                  color: AppColors.appPrimaryColorLight,
                ),
              ),

@hirensshinde
Copy link

Any Update on this topic?

@TaiTrien
Copy link

I just fix this issue. Just only use prefixIcon property instead of prefix property and use TextAlignVertical.center for textAlignVertical
TextField( textAlignVertical: TextAlignVertical.center, decoration: InputDecoration( contentPadding: EdgeInsets.zero, prefixIcon: Icon( Icons.add, size: 24, color: Colors.grey, ), hintText: 'Typing...', ), ),

@cedvdb
Copy link
Contributor

cedvdb commented Oct 15, 2021

Note that using prefixIcon instead of prefix , the alignment seems to be very sensible to any decoration in the input. It depends on the different paddings, the border width, is there a label or not, is there margin or not, and even the platform, is it ios or android or web.

Any solution using paddings, margins, etc won't work for other users with different values. This is especially problematic when building a widget library where the user is expected to have any arbitrary value.

The solution needs to come on the flutter side where the prefixIcon can be aligned with the text in the same vein prefix can be.

@TahaTesser TahaTesser self-assigned this Oct 18, 2021
@TahaTesser TahaTesser added the customer: crowd Affects or could affect many people, though not necessarily a specific customer. label Oct 18, 2021
@TahaTesser TahaTesser moved this from In progress to PR submitted in Nevercode Oct 26, 2021
@TahaTesser
Copy link
Member

TahaTesser commented Oct 27, 2021

This is now documented in #91998, you can get the desired alignment using the Align widget. with widthFactor & heightFactor , closing.

Nevercode automation moved this from PR submitted to Done (PR merged) Oct 27, 2021
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: text input Entering text in a text field or keyboard related problems c: new feature Nothing broken; request for a new capability customer: crowd Affects or could affect many people, though not necessarily a specific customer. f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
Status: Done (PR merged)
Nevercode
  
Done (PR merged)
Development

No branches or pull requests