我在想,如果有一個特定的顏色,以適用于一種方式enabledBorder的TextField()時候darkTheme被啟用。這是我的TextField():
TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
),
但是,在我的深色主題中使用以下代碼將對每個 enabledBorder 應用邊框著色,出于明顯的原因,我想避免這種情況:
inputDecorationTheme: InputDecorationTheme(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[700]!)
)
),
我還沒有使用常規的主題的時候,所以我不能把它應用到只是希望有相同的顏色施加TextField()。我能做些什么來解決這個問題?
uj5u.com熱心網友回復:
您可以在 TextField 中檢查主題是否為 darkTheme 并相應地指定邊框顏色。首先我們獲取當前的主題資料。將以下代碼放入構建中:
ThemeData themeData = Theme.of(context);
現在我們可以檢查 Brightness 的值來確定是否啟用了深色主題。如果啟用了深色主題,則亮度值將為淺色。所以你的 TextField 將是:
TextField(
obscureText: true,
decoration: InputDecoration(
enabledBorder: Theme.of(context).brightness == Brightness.light ? OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[700]!)
) : InputBorder.none,
labelText: 'Password',
),
),
uj5u.com熱心網友回復:
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是我的深色主題中使用的深色背景色。我知道這會造成一些代碼重復,但我認為這更容易閱讀。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/367327.html
下一篇:回圈進口在飛鏢中很糟糕嗎?
