仅在启用 darkTheme 时将 borderSide 颜色应用于 TextField
Applying borderSide color to TextField only when darkTheme is enabled
我想知道是否有办法在 darkTheme
启用时将特定颜色应用于 TextField()
的 enabledBorder
。这是我的 TextField()
:
TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
),
但是,在我的深色主题中使用以下代码将为每个 enabledBorder 应用边框着色,出于显而易见的原因我想避免这样做:
inputDecorationTheme: InputDecorationTheme(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[700]!)
)
),
我也不想在使用常规主题时应用相同的颜色,所以我不能只将它应用到TextField()
。我该怎么做才能解决这个问题?
您可以在 TextField 中检查主题是否为 darkTheme 并相应地设置边框颜色。
首先我们得到当前的主题数据。将以下代码放入 build:
ThemeData themeData = Theme.of(context);
现在我们可以检查亮度的值来确定是否启用深色主题。如果启用深色主题,则亮度值将为亮。所以您的 TextField 将是:
TextField(
obscureText: true,
decoration: InputDecoration(
enabledBorder: Theme.of(context).brightness == Brightness.light ? OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[700]!)
) : InputBorder.none,
labelText: 'Password',
),
),
Suvash 的回答启发了我以下代码:
TextField(
obscureText: true,
decoration: Theme.of(context).scaffoldBackgroundColor == bgColorDark
? InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[700]!)
),
border: OutlineInputBorder(),
labelText: 'Password',
)
: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
),
请注意,bgColorDark
是我的深色主题中使用的深色背景色。我知道这会造成一些代码重复,但我认为这样更容易阅读。
我想知道是否有办法在 darkTheme
启用时将特定颜色应用于 TextField()
的 enabledBorder
。这是我的 TextField()
:
TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
),
但是,在我的深色主题中使用以下代码将为每个 enabledBorder 应用边框着色,出于显而易见的原因我想避免这样做:
inputDecorationTheme: InputDecorationTheme(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[700]!)
)
),
我也不想在使用常规主题时应用相同的颜色,所以我不能只将它应用到TextField()
。我该怎么做才能解决这个问题?
您可以在 TextField 中检查主题是否为 darkTheme 并相应地设置边框颜色。 首先我们得到当前的主题数据。将以下代码放入 build:
ThemeData themeData = Theme.of(context);
现在我们可以检查亮度的值来确定是否启用深色主题。如果启用深色主题,则亮度值将为亮。所以您的 TextField 将是:
TextField(
obscureText: true,
decoration: InputDecoration(
enabledBorder: Theme.of(context).brightness == Brightness.light ? OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[700]!)
) : InputBorder.none,
labelText: 'Password',
),
),
Suvash 的回答启发了我以下代码:
TextField(
obscureText: true,
decoration: Theme.of(context).scaffoldBackgroundColor == bgColorDark
? InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[700]!)
),
border: OutlineInputBorder(),
labelText: 'Password',
)
: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
),
请注意,bgColorDark
是我的深色主题中使用的深色背景色。我知道这会造成一些代码重复,但我认为这样更容易阅读。