如何将验证添加到未启用的textformfeld



im试图在textformfield为空时显示错误文本,这很好,但只有当用户点击文本字段时,他才会看到错误。我想要的是,如果用户点击上传视频,并且标签的textfomrfield为空,那么在未启用时也会显示错误文本。希望有人能帮忙这是我的代码

Container(
width: 170,
height: 65,
margin: EdgeInsets.only(left: 12, right: 10),
child: TextFormField(
controller: hashtagcontroller2,
decoration: InputDecoration(
hintText: 'Hashtag',
prefixText: '#',
contentPadding: EdgeInsets.fromLTRB(12, 8, 8, 8),
errorText: _validatehashtag2 ? 'Hashtag Can't Be Empty' : null,
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
borderSide: BorderSide(
width: 2, color: Colors.black),
),
focusedBorder: OutlineInputBorder(

borderRadius: const BorderRadius.all( 
const Radius.circular(20.0),
),

borderSide: BorderSide(
width: 2, color: Colors.black),
),//prefixIcon: Icon(Icons.h)
),

),
),
``

首先将容器与列绑定,并尝试

Column(
children: [
Container(
width: 170,
height: 65,
margin: EdgeInsets.only(left: 12, right: 10),
child: TextFormField(
controller: hashtagcontroller2,
decoration: InputDecoration(
hintText: 'Hashtag',
prefixText: '#',
contentPadding: EdgeInsets.fromLTRB(12, 8, 8, 8),
errorText: _validatehashtag2 ? 'Hashtag Can't Be Empty' : null,
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
borderSide: BorderSide(
width: 2, color: Colors.black),
),
focusedBorder: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(20.0),
),
borderSide: BorderSide(
width: 2, color: Colors.black),
),//prefixIcon: Icon(Icons.h)
),
),
),
RaisedButton(
onPressed: () {
setState(() {
hashtagcontroller2.text.isEmpty ? _validatehashtag2 = true : _validatehashtag2 = false;
});
},
child: Text('Submit'),
textColor: Colors.white,
color: Colors.blueAccent,
),],),

相关内容

  • 没有找到相关文章