Flutter - How to change TextField hint color?

后端 未结 3 1260
日久生厌
日久生厌 2021-01-31 14:15

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),
         


        
相关标签:
3条回答
  • 2021-01-31 14:33

    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,
    )
    
    0 讨论(0)
  • 2021-01-31 14:46

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

    @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(),
        );
      }
    
    0 讨论(0)
  • 2021-01-31 14:48

    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,
              ),
            ),
          ),
    
    0 讨论(0)
提交回复
热议问题