[英]How to change border color in Flutter according to the value?
我有一個帶有某種顏色邊框的容器小部件。 如果 TextField 中輸入的值大於或小於某個值,我希望能夠更改邊框的顏色。 實現這一目標的最佳方法是什么?
_color
變量:Color _color = Colors.purple;
_color
變量分配給Container
的邊框: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
border: Border.all(
width: 5.0,
// assign the color to the border color
color: _color,
),
),
),
TextField
的onChanged
回調中測試您的條件是否滿足並根據您的需要更改_color
: TextField(
onChanged: (newValue) {
if (newValue.length > 5) { // test for your condition
setState(() {
_color = Colors.red; // change the color
});
} else {
setState(() {
_color = Colors.blue; // change the color if the condition is not met
});
}
},
),
這是您的 TextField 控制器;
final yourTextFieldControler = TextEditingController();
只需從控制器獲取值並更改為 int。
Container(
decoration: BoxDecoration(
border: Border.all(color: yourTextFieldControler.text.toInt > 5 ? Colors.red:Colors.blueAccent)
),
child: Text("My Awesome Border"),
)
這是你的 TextField
TextField(
controler : yourTextFieldControler,
onChanged: (newValue) {
setState((){})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.