在material 3之后,默认文本字段边界和标签 colored颜色 (聚焦时)更改为灰色.因此,我需要如下设置我的ThemeData.如果文本字段为空,则其中会有一个浅灰色的提示,这是所需的行为.当我聚焦文本字段时,文本字段顶部的标签现在为红色.当我取消焦点并且标签文本的 colored颜色 保持红色,而边框 colored颜色 为浅灰色时,问题就会出现.

我想要的行为是标签 colored颜色 变成浅灰色,而不是红色.我知道我可以通过编程实现这一点,但我的应用程序上有几个文本字段,这使得它不切实际.让我们注意的是,在material 2上,只需设置原色,它就能按预期工作.

      theme: ThemeData(
        brightness: Brightness.light,
        primarySwatch: Colors.red,
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.red,
            foregroundColor: Colors.white,
          ),
        ),
        inputDecorationTheme: InputDecorationTheme(
          floatingLabelStyle: TextStyle(color: Colors.red),
          border: OutlineInputBorder(borderRadius: BorderRadius.circular(10), borderSide: BorderSide()),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(10),
            borderSide: BorderSide(color: Colors.red, width: 2),
          ),
        ),
        textSelectionTheme: TextSelectionThemeData(cursorColor: Colors.red),
      ),

推荐答案

这个问题很有趣,我想我应该添加enableFloatingLabelStyle,但与此同时,我想到的是使用MaterialStateTextStyle来获得所需的结果.

      theme: ThemeData(
        brightness: Brightness.light,
        primarySwatch: Colors.red,
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.red,
            foregroundColor: Colors.white,
          ),
        ),
        inputDecorationTheme: InputDecorationTheme(
          // floatingLabelStyle: const TextStyle(color: Colors.red),
          // use material state to change the color of the floating label
          floatingLabelStyle: MaterialStateTextStyle.resolveWith((states) {
            if (states.contains(MaterialState.focused)) {
              return const TextStyle(color: Colors.red);
            }
            return const TextStyle(color: Colors.grey);
          }),
          //
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: const BorderSide()),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(10),
            borderSide: const BorderSide(color: Colors.red, width: 2),
          ),
        ),
        textSelectionTheme:
            const TextSelectionThemeData(cursorColor: Colors.red),
      ),

Flutter相关问答推荐

下拉图标未居中

Fighter:基于ChangeNotifier状态动态更新AppBar中的图标

我如何才能动态地将小部件排成一排进行排列呢?

SupabaseFlutter 集成问题:无法更新行

try 使用fl_Chart of Ffltter,但在导入它时遇到以下错误:Error:';TextScaler';is;t a type

应为Map String,dynamic类型的值,但得到的值为type()= Map String,dynamic'

Flutter 渲染HTML和数学(LaTeX和Katex)

在创建显示此错误的Flutter 深度链接时

将自定义容器附加到屏幕底部

尽管设置了对齐方式,如何对齐列中的行?

无法在 Flutter 项目中安装最新版本的 image_picker

Flutter列顶部和底部出现不必要的边距问题

Flutter - 展开图标无法正常工作

如何(部分)更新 riverpod 中的 AsyncValue?

底部工作表顶部的一块 Flutter

如何在 flutter 中同时使用 ButtonStyle() 和 ElevatedButton.styleFrom() ?

如何从 Flutter Slider Widget 中移除发光效果?

如何在 cupertino switch flutter 中添加文本

如何按值对列表中的 map 进行排序

如何在 flutter 中制作这种类型的扩展瓦片