Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change TextField's Underline in Flutter

I'm working on an application using Flutter SDK. When I use a TextField widget, and I focus it, the underline becomes blue. I need to change this color to red, how can I do it?

Screenshot of what I need to change. I want just the underline to change, , not the label color.

Screenshot of what I need to change. (I want the underline to change, not the label color)

like image 360
Tiziano Gioè Avatar asked Feb 09 '18 13:02

Tiziano Gioè


People also ask

How do you change the dropdown underline color in Flutter?

To change underline color use the underline property of the Dropdown widget and then assign the Container widget with height and color property. Inside the Dropdown widget, and add the underline property and assign the Container widget. Inside the Container , add the color property and assign the color.

How do you underline TextField in Flutter?

In Flutter, you can customize the color, thickness, and style (actually, there are only 2 stypes for underline: solid and none) of a TextFIeld's underline by using the parameters from the InputDecoration class listed below: enabledBorder. focusedBorder.


2 Answers

While these other answers may somehow work, you should definitely not use it. That's not the proper way to get a custom theme in Flutter.

A much more elegant solution is as followed :

final theme = Theme.of(context);  return new Theme(   data: theme.copyWith(primaryColor: Colors.red),   child: new TextField(     decoration: new InputDecoration(       labelText: "Hello",       labelStyle: theme.textTheme.caption.copyWith(color: theme.primaryColor),     ),   ), ); 

At the same time, if you just want to show an error (Red), use errorText of InputDecoration instead. It will automatically set the color to red.

like image 143
Rémi Rousselet Avatar answered Oct 01 '22 20:10

Rémi Rousselet


You can also change its color by following ways.

  1. Wrap your TextField in Theme and provide accentColor

    Theme(   data: Theme.of(context).copyWith(accentColor: Colors.red),   child: TextField(), ) 
  2. Using inputDecoration property.

    TextField(   decoration: InputDecoration(     focusedBorder: UnderlineInputBorder(       borderSide: BorderSide(color: Colors.red),     ),   ), ) 
like image 36
CopsOnRoad Avatar answered Oct 01 '22 18:10

CopsOnRoad