[英]How to display counter text on TextFormField() only when focused in flutter?
I have the TextFormField()
Widget, I want to display the CounterText but only when the field is focused我有
TextFormField()
小部件,我想显示 CounterText 但仅当该字段被聚焦时
and one more side question How to change the field background color when on focus?还有一个附带问题如何在聚焦时更改字段背景颜色?
code:-代码:-
TextFormField(
decoration: InputDecoration(
labelText: "Weight",
border: OutlineInputBorder(
borderSide: BorderSide(
color:Colors.grey[200],
width: 1,
style: BorderStyle.solid
),
gapPadding: 4.0
),
counterText:"Enter Your Weight",
),
),
I think the easiest way is to create a list of Boolean and update them on that so you know when the TextForm is selected and then display counterText我认为最简单的方法是创建一个布尔列表并更新它们,以便您知道何时选择了 TextForm,然后显示 counterText
Column(
children: <Widget>[
SizedBox(height: 50),
TextFormField(
onTap: () {
setState(() {
selected[0] = true;
selected[1] = false;
});
},
controller: textController,
decoration: InputDecoration(
labelText: "Weight",
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey[200],
width: 1,
style: BorderStyle.solid),
gapPadding: 4.0),
counterText: selected[0] ? "Enter Your Weight" : ' ',
),
),
TextFormField(
onTap: () {
setState(() {
selected[0] = false;
selected[1] = true;
});
},
decoration: InputDecoration(
labelText: "Age",
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey[200],
width: 1,
style: BorderStyle.solid),
gapPadding: 4.0),
counterText: selected[1] ? "Enter Your Age" : ' ',
),
)
],
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.