如何更改文本字段中字符串输入的颜色?

问题描述 投票:0回答:5

我正在使用我的应用程序的样式,我无法更改

TextField
输入的颜色,没有任何属性可以更改它。

 Theme(
            data: new ThemeData(
              hintColor: Colors.white
            ),
            child:
        TextField(
          focusNode: _focusUsername,
          controller: _controller,
          decoration: InputDecoration(
            border: InputBorder.none,
            fillColor: Colors.grey,
            filled: true,
            hintText: 'Username',
          ))),
flutter dart
5个回答
200
投票

您可以指定

TextStyle

TextField(
  style: TextStyle(color: Colors.white),
  ...
)

58
投票

在下面的示例中,文本为“红色”,文本字段的背景为“橙色”。

TextField(
  style: TextStyle(color: Colors.red),
  decoration: InputDecoration(fillColor: Colors.orange, filled: true),
)

你是这个意思吗?

如果你想通过应用程序的主题来通用地做到这一点,这确实很棘手。大概会是这样的:

theme: ThemeData(
    textTheme: TextTheme(
      bodyText1: TextStyle(color: Colors.black),
      bodyText2: TextStyle(color: Colors.black),
      button: TextStyle(color: Colors.black),
      caption: TextStyle(color: Colors.black),
      subtitle1: TextStyle(color: Colors.red), // <-- that's the one
      headline1: TextStyle(color: Colors.black),
      headline2: TextStyle(color: Colors.black),
      headline3: TextStyle(color: Colors.black),
      headline4: TextStyle(color: Colors.black),
      headline5: TextStyle(color: Colors.black),
      headline6: TextStyle(color: Colors.black),
    ),
    inputDecorationTheme: InputDecorationTheme(
      fillColor: Colors.orange,
      filled: true,
    )
)

20
投票

要在 ThemeData 中更改它,我使用了:

ThemeData(
      textTheme: TextTheme(subtitle1: TextStyle(color: Colors.grey)),

3
投票

对于任何想要在主题级别执行此操作的人,大多数文档都指向

InputDecorationTheme
。它提供了
hintText
borders
等的样式...但没有提供设置用户在
Textfield
中输入的文本的默认颜色的方法。

要更新@Feu关于主题的答案,

subtitle1
已弃用,
TextTheme
中的当前(2023)属性是
titleMedium

ThemeData(
    textTheme: const TextTheme(
      titleMedium: TextStyle(color: Colors.black, fontSize: 20), // default TextField input style
    ),
  );

0
投票

我在主题中所做的更改解决了问题。

ThemeData(
   textTheme: const TextTheme(
          bodyLarge: TextStyle(color: Colors.black, fontSize: 20),
       ),
);
© www.soinside.com 2019 - 2024. All rights reserved.