32

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)

Tiziano Gioè
  • 683
  • 2
  • 7
  • 8

4 Answers4

17

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),
        ),
      ),
    )
    
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
14

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.

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • Your answer definitely looks better, but it is a bit strange that you have to change the primary color to red and then change all the other styled items back to your real primary color. – Rene Feb 10 '18 at 10:26
  • Yeah I agree. Sadly you currently can't change the underline color without rewriting everything. – Rémi Rousselet Feb 10 '18 at 12:01
  • How can i set selected and default underline color.? Consider there are 4 textfields in the form if i am entering data in textfield1 so color of textfield1 and other textfields. – Mayur Prasad Mar 22 '18 at 14:34
  • @RémiRousselet Can you bit help me to understand what exactly it requires to rewrite to change that underline color to something else. Because I've also stuck in the same kind of situation. Here is my post link -https://stackoverflow.com/questions/49600139/how-to-change-textfield-underline-color – Suresh Apr 01 '18 at 17:06
  • For those who wasn't able to understand Remi's answer, a different answer helped me understand quite a bit: https://stackoverflow.com/questions/49600139/how-to-change-textfield-underline-color – user823447 Apr 21 '19 at 02:59
6

I have used InputDecoration.collapsed to remove the divider and I am changing the color of the bottom border.

If you enter a name the bottom border color is blue and if you enter a number or other special characters then the bottom border color is red

demo

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const EdgeInsets _padding = const EdgeInsets.symmetric(horizontal: 20.0, vertical: 8.0);
  Color borderColor = Colors.blue;
  bool nameFlag = false;

  @override
  void initState() {
    super.initState();
  }

  void validateName(String value) {
    final RegExp nameExp = new RegExp(r'^[A-Za-z ]+$');
    if (!nameExp.hasMatch(value) || value.isEmpty)
      borderColor = Colors.red;
    else
      borderColor = Colors.blue;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Column(children: <Widget>[
        new Flexible(
          child: new Container(
            margin: _padding,
            padding: _padding,
            child: new TextField(
              decoration: new InputDecoration.collapsed(
                hintText: "Enter Name",
              ),
              onChanged: (s) {
                setState(() => validateName(s));
              },
            ),
            decoration: new BoxDecoration(
              color: Colors.white,
              border: new Border(
                bottom: new BorderSide(color: borderColor, style: BorderStyle.solid),
              ),
            ),
          ),
        )
      ]),
    );
  }
}

Let me know if this answers your question :)

Ajay Kumar
  • 15,250
  • 14
  • 54
  • 53
  • Far too complex for what it is. Will be hard to maintain over the long run. – Rémi Rousselet Feb 09 '18 at 22:47
  • @Darky Actually It won't be hard to maintain.You can make a Custom widget as CustomTextFeild which will have all TextField Parameters as well as Some Custom One and use it everywhere. – Ajay Kumar Feb 11 '18 at 07:01
  • Harder than just wrapping your `TextField` in a `Theme`. Especially in alpha where everything iterates pretty quickly (a month ago `inputDecorationTheme` didn't even exists). – Rémi Rousselet Feb 11 '18 at 12:06
  • @Darky Oh yeah! You are right.... Missed your Answer above... Cool why I didn't thought of that :) – Ajay Kumar Feb 12 '18 at 12:35
0

I haven't tried it yet, but I had a look at the docs for you.

Looking at the TextField class you can set an InputDecoration, which in turn has an InputBorder. Setting that to an UnderlineInputBorder with your own BorderSide should do the trick I guess. You can set the color of the BorderSide.

You can also set an InputBorder in the global InputDecorationTheme if you want all textfields to have the same style.

Rene
  • 1,027
  • 10
  • 18
  • children: [ objectTextField = new TextField( decoration: new InputDecoration( border: new UnderlineInputBorder(borderSide: new BorderSide(color: Colors.redAccent)), ....... I'm doing something like this, but it doesen't change anything. – Tiziano Gioè Feb 09 '18 at 13:54
  • Did you also try to set the inputdecorationtheme? It looks like it overrides the decoration color you set: (widget.decoration ?? const InputDecoration()) .applyDefaults(Theme.of(context).inputDecorationTheme); – Rene Feb 09 '18 at 14:15
  • 1
    It doesn't work. For some obscure reasons, `TextField` internally overrides `borderSide` property of `inputDecorationTheme` and builds it from the primary color. – Rémi Rousselet Feb 09 '18 at 23:51
  • Setting `inputDecorationTheme` has worked for me. ``` inputDecorationTheme: InputDecorationTheme( labelStyle: TextStyle(color: Colors.white), helperStyle: TextStyle(color: Colors.white), hintStyle: TextStyle(color: Colors.grey), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.white), ) ), ``` – surenyonjan Dec 13 '18 at 17:24