Flutter - How to change TextField hint color?
Asked Answered
A

7

108

I'm a bit confused how to change the hint color of the textfield. Someone can guide me how to do it.Thanks

child: TextField(
   style: TextStyle(fontSize: 20),
   decoration: InputDecoration(
                  hintText: "Password",
                  border: new OutlineInputBorder(
                            borderSide: new BorderSide(
                              color: Colors.teal,
                            ),
                          ),
                   prefixIcon: const Icon(
                            Icons.security,
                            color: Colors.white,
                          ),
                ),
   ),
Abettor answered 8/12, 2018 at 2:57 Comment(0)
C
203

You can do with hintStyle: in InputDecoration

TextField(
        style: TextStyle(fontSize: 20),
        decoration: InputDecoration(
          hintText: "Password",
          hintStyle: TextStyle(fontSize: 20.0, color: Colors.redAccent),
          border: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.teal,
            ),
          ),
          prefixIcon: const Icon(
            Icons.security,
            color: Colors.white,
          ),
        ),
      ),
Calotte answered 8/12, 2018 at 3:42 Comment(2)
What about when the input is focused? change hinttext color to a different color?Afterwards
@VincentJ.Michuki See my answer below.Rejuvenate
R
21

As an addition to the accepted answer, to update the focused hint decoration you have to update the app's Theme. enter image description here

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          primaryColor: Colors.white,
          inputDecorationTheme: const InputDecorationTheme(
            labelStyle: TextStyle(color: Colors.black),
            hintStyle: TextStyle(color: Colors.grey),
          )),
      home: MainScreen(),
    );
  }
Rejuvenate answered 6/2, 2020 at 11:23 Comment(1)
An alternative way, one can use a focus node like so hintStyle: TextStyle(color: focusNode.hasFocus ? Color(0xff7FBBCA) : Colors.black45)Afterwards
T
13

change both hintStyle and labelStyle

TextFormField(
              decoration: InputDecoration(
                hintText: '[email protected]',
                labelText: 'Email',
               hintStyle: TextStyle(color: Colors.white), # change to your color
                labelStyle: TextStyle(color: Colors.white), # change color
             ))
Takeoff answered 16/3, 2021 at 2:46 Comment(0)
E
5

If you want to change the hintColor of all the TextField Widget in the app, you can apply it in the Theme.

example code:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData.light().copyWith(
        hintColor: Colors.orange,
      ),
      home: Scaffold(
        body: Column(children: [
          TextField(
            decoration: InputDecoration(
              hintText: "Email",
            ),
          ),
          TextField(
            decoration: InputDecoration(
              hintText: "Password",
            ),
          ),
        ]),
      ),
    );
  }
}

enter image description here

Ejective answered 11/5, 2021 at 8:40 Comment(0)
H
1

After digging the source code for the InputDecorator used to determine the label color, here's what I found.

TextStyle _getFloatingLabelStyle(ThemeData themeData) {
  final Color color = decoration.errorText != null
    ? decoration.errorStyle?.color ?? themeData.errorColor
    : _getActiveColor(themeData);
  final TextStyle style = themeData.textTheme.subtitle1.merge(widget.baseStyle);
  return style
    .copyWith(color: decoration.enabled ? color : themeData.disabledColor)
    .merge(decoration.labelStyle);
}

Color _getActiveColor(ThemeData themeData) {
  if (isFocused) {
    switch (themeData.brightness) {
      case Brightness.dark:
        return themeData.accentColor;
      case Brightness.light:
        return themeData.primaryColor;
    }
  }
  return themeData.hintColor;
}

In short, to change the hint color, set hintColor using Theme and ThemeData.

Another tip: to change the label color, set the primaryColor light theme, or accentColor for dark theme.

ThemeData.dark().copyWith(
  primaryColor: Colors.red,
  accentColor: Colors.white,
  hintColor: Colors.pink,
)
Homocercal answered 15/5, 2020 at 6:58 Comment(0)
H
0
TextField(
   decoration: InputDecoration(
   hintText: 'your hint',
   hintStyle: Theme.of(context).textTheme.caption.copyWith(
   fontSize: 20,
   fontWeight: FontWeight.w600,
   color: ColorConstants.kTextColor,  <--- // Set Your Own Color
   ),
Heaviside answered 1/5, 2021 at 17:21 Comment(1)
Also, you can check this page for more examples. medium.com/flutter-community/…Rarefied
E
0

Without using the platform library widget in the Cupertino section, it is this way:

cupertino: (_, __) => CupertinoTextFormFieldData(
    style: const TextStyle(
        fontSize: 12,
        color: Colors.black,
    ),
    placeholderStyle: const TextStyle(
        fontSize: 12,
        color: Colors.black,
    ),
    decoration: BoxDecoration(
        border: Border.all(color: Colors.black),
    ),
    textAlign: TextAlign.center,
    placeholder: widget.hint,
),
placeholderStyle: const TextStyle(
    fontSize: 12,
    color: Colors.black,
),
Estray answered 12/9, 2023 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.